Fix typo in grml-repos code; use new stages in chroot-script as well now
[grml-debootstrap.git] / grml-debootstrap
1 #!/bin/sh
2 # Filename:      grml-bootstrap
3 # Purpose:       wrapper around debootstrap for installing plain Debian via grml
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Mon Apr 16 20:39:13 CEST 2007 [mika]
8 ################################################################################
9 # http://www.debian.org/releases/stable/i386/index.html.en
10
11 set -e # exit on any error
12
13 VERSION='0.7'
14
15 # source core functions {{{
16 . /etc/grml/lsb-functions
17 . /etc/grml/script-functions
18 # }}}
19
20 # make sure we have what we need {{{
21 check4progs debootstrap || exit 1
22 check4root || exit 1
23 # }}}
24
25 # without config file it won't work {{{
26 if [ -r /etc/debootstrap/config ] ; then
27    . /etc/debootstrap/config
28 else
29    eerror "/etc/debootstrap/config could not be read, exiting." ; eend 1
30    exit 1
31 fi
32 # }}}
33
34 # cmdline handling {{{
35 usage() {
36   einfo "$0 - version $VERSION"
37   echo  "   A wrapper around debootstrap for installing plain Debian via grml"
38   echo
39   einfo "Usage: $0 [options]"
40   echo  "   Adjust /etc/debootstrap/config and invoke $0 afterwards or use the cmdline option:"
41   echo
42   einfo "Valid options:"
43   echo  "
44     -h|--help                   Print this usage information and exit.
45     -v|--version                Show summary of options and exit.
46
47     -t|--target <target>        Target partition (/dev/...) or directory.
48     -r|--release <release>      Specify release of new Debian system. Supported relases: sarge, etch, lenny and sid.
49     -m|--mirror <URL>           Specify mirror which should be used for apt-get/aptitude.
50     -p|--mntpoint <mnt>         Specify mountpoint that should be used for mounting the target system.
51     --groot <device>            Specify root device for usage in grub (corresponds with \$TARGET).
52     --grub <device>             Where do you want to install grub to? Use grub syntax for specifying.
53     --password <pwd>            Use specified password as password for user root. Use with caution.
54     --boot_append <appendline>  Add specified appendline to kernel whilst booting
55
56 "
57 }
58
59 while [ "$#" -gt "0" ] ; do
60     case $1 in
61         -v|--version)
62             einfo "$0 - version $VERSION"
63             einfo "Send bug reports to Michael Prokop <mika@grml.org>."
64             eend 0
65             exit 0
66             ;;
67         -t|--target)
68             shift
69             TARGET=$1
70             ;;
71         --grub)
72             shift
73             GRUB=$1
74             ;;
75         --groot)
76             shift
77             GROOT=$1
78             ;;
79         --release)
80             shift
81             RELEASE=$1
82             ;;
83         -p|--mntpoint)
84             shift
85             MNTPOINT=$1
86             ;;
87         --password)
88             shift
89             ROOTPASSWORD=$1
90             ;;
91         -m|--mirror)
92             shift
93             MIRROR=$1
94             CHROOTMIRROR=$1
95             ;;
96         -h|--help)
97             usage ; eend 0
98             eend 0
99             exit 0
100             ;;
101         *)
102             eerror "Syntax error."
103             usage ; eend 1
104             exit 1
105             ;;
106     esac
107     shift
108 done
109 # }}}
110
111 # set/check variables {{{
112
113 # inside the chroot system the locales might not be available, so use minimum:
114 export LANG=C
115 export LC_ALL=C
116
117 if [ -z "$STAGES" ] ; then
118    STAGES='/etc/debootstrap/stages'
119    [ -d "$STAGES" ] || mkdir -p "$STAGES"
120 fi
121
122 if [ -r $STAGES/grml-debootstrap ] ; then
123    if grep -q done $STAGES/grml-debootstrap ; then
124       eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
125       eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
126    fi
127 fi
128
129 PARTITION=''
130 DIRECTORY=''
131
132 case $TARGET in
133   /dev/*)
134     PARTITION=1
135     ;;
136   *)
137     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
138     DIRECTORY=1
139     MNTPOINT="$TARGET"
140     MKFS=''
141     TUNE2FS=''
142     FSCK=''
143     GRUB=''
144     GROOT=''
145     ;;
146 esac
147
148 if [ -n "$ARCH" ] ; then
149    ARCHCMD="--arch $ARCH"
150    ARCHINFO=" (${ARCH})"
151 else
152    ARCH="$(dpkg --print-architecture)"
153    ARCHCMD="--arch $ARCH"
154    ARCHINFO=" (${ARCH})"
155 fi
156
157 # provide variables to chroot system
158 touch /etc/debootstrap/variables
159 chmod 600 /etc/debootstrap/variables # make sure nobody except root can read it
160 [ -n "$ARCH" ]   && echo "ARCH=$ARCH"     >  /etc/debootstrap/variables
161 [ -n "$GRUB" ]   && echo "GRUB=$GRUB"     >> /etc/debootstrap/variables
162 [ -n "$GROOT" ]  && echo "GROOT=$GROOT"   >> /etc/debootstrap/variables
163 [ -n "$TARGET" ] && echo "TARGET=$TARGET" >> /etc/debootstrap/variables
164 [ -n "$MIRROR" ] && echo "MIRROR=$MIRROR" >> /etc/debootstrap/variables
165 [ -n "$MIRROR" ] && echo "CHROOTMIRROR=$MIRROR" >> /etc/debootstrap/variables
166 [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> /etc/debootstrap/variables
167
168 # make sure at least $TARGET is set [the partition for the new system]
169 if [ -z "$TARGET" ] ; then
170    eerror "Please adjust /etc/debootstrap/config before running ${0}" ; eend 1
171    exit 1
172 fi
173 # }}}
174
175 # helper functions {{{
176 # we want to exit smoothly and clean:
177 bailout(){
178   # make sure $TARGET is not mounted when exiting grml-debootstrap
179   if [ -n "$MNTPOINT" ] ; then
180      if grep -q $MNTPOINT /proc/mounts ; then
181         # make sure nothing is left inside chroot so we can unmount it
182         [ -x "$MNTPOINT"/etc/init.d/ssh   ] && "$MNTPOINT"/etc/init.d/ssh stop
183         [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
184         # ugly, but make sure we really don't leav anything
185         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /sys  1>/dev/null 2>&1
186         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a    1>/dev/null 2>&1
187         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
188         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
189         einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
190      fi
191   fi
192   [ -n "$1" ] && EXIT="$1" || EXIT="1"
193   [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
194   exit "$EXIT"
195 }
196 trap bailout 1 2 3 15
197
198 # we want to execute all the functions only once, simple check for it:
199 stage() {
200   if [ -n "$2" ] ; then
201      echo "$2" > "$STAGES/$1"
202      return 0
203   elif grep -q done "$STAGES/$1" 2>/dev/null ; then
204      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
205      return 1
206   fi
207 }
208 # }}}
209
210 # user should recheck his configuration {{{
211 # support full automatic installation:
212 checkforrun() {
213    dialog --timeout 10 --title "$0" \
214           --yesno "Do you want to stop at this stage?
215
216 Notice: you are running grml-debootstrap in non-interactive mode.
217 grml-debootstrap will install Debian ${RELEASE} on ${TARGET}.
218 Last chance to quit. Timeout of 10 seconds running....
219
220 Do you want to stop now?" 0 0 2>/dev/null
221 }
222
223 if [ -n "$AUTOINSTALL" ] ; then
224    if checkforrun ; then
225       eerror "Exiting as requested" ; eend 0
226       exit 1
227    fi
228 else # if not running automatic installation display configuration and prompt for execution:
229    einfo "$0 - Please recheck configuration before execution:"
230    echo "
231       Target:           $TARGET"
232       case "$MNTPOINT" in "$TARGET") ;; *) echo "   Mount-point:      $MNTPOINT" ;; esac
233       [ -n "$GRUB" ]   && echo "   Install grub to:  $GROOT / $GRUB"
234       [ -n "$MIRROR" ] && echo "   Using mirror:     $MIRROR"
235       case "$MNTPOINT" in "$TARGET") ;; *) echo "  Important! Continuing will delete all data from ${TARGET}!" ;; esac
236       echo
237    einfon "Is this ok for you? [y/N] "
238    read a
239    if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
240       eerror "Exiting as requested." ; eend 1
241       exit 1
242    fi
243 fi
244 # }}}
245
246 # create filesystem {{{
247 mkfs() {
248   if [ -n "$MKFS" ] ; then
249      einfo "Running $MKFS on $TARGET"
250      $MKFS $TARGET
251      eend $?
252   fi
253 }
254 # }}}
255
256 # modify filesystem settings {{{
257 tunefs() {
258   if [ -n "$TUNE2FS" ] ; then
259      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
260      $TUNE2FS $TARGET
261      eend $?
262   fi
263 }
264 # }}}
265
266 # mount the new partition or if it's a directory do nothing at all {{{
267 mount_target() {
268   if [ -n "$DIRECTORY" ] ; then
269      einfo "Running grml-debootstrap on a directory, nothing to mount."
270   else
271      if grep -q $TARGET /proc/mounts ; then
272         eerror "$TARGET already mounted, exiting."
273      else
274        [ -n "$MNTPOINT" ] || MNTPOINT='/mnt/test'
275        [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
276        einfo "Mounting $TARGET to $MNTPOINT"
277        mount -o rw,suid,dev $TARGET $MNTPOINT
278        eend $?
279      fi
280   fi
281 }
282 # }}}
283
284 # install main chroot {{{
285 debootstrap_system() {
286   if ! grep -q $MNTPOINT /proc/mounts ; then
287           mount_target
288   fi
289   if grep -q $MNTPOINT /proc/mounts ; then
290      einfo "Running $DEBOOTSTRAP for release ${RELEASE}${ARCHINFO} using mirror $MIRROR"
291      $DEBOOTSTRAP $ARCHCMD $RELEASE $MNTPOINT $MIRROR
292      eend $?
293   else
294      eerror "Error: $MNTPOINT not mounted, can not continue."
295      eend 1
296   fi
297 }
298 # }}}
299
300 # prepare chroot via chroot-script {{{
301 preparechroot() {
302   einfo "Preparing chroot system"
303   cp $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
304   chmod 755 $MNTPOINT/bin/chroot-script
305   mkdir $MNTPOINT/etc/debootstrap/
306
307   # make sure we have our files for later use via chroot-script
308   cp /etc/debootstrap/config          $MNTPOINT/etc/debootstrap/
309   cp /etc/debootstrap/packages        $MNTPOINT/etc/debootstrap/packages
310   cp /etc/debootstrap/variables       $MNTPOINT/etc/debootstrap/variables
311
312   cp -a /etc/debootstrap/extrapackages/ $MNTPOINT/etc/debootstrap/
313
314   # make sure we can access network [relevant for cdebootstrap]
315   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp /etc/resolv.conf $MNTPOINT/etc/resolv.conf
316
317   # setup default locales
318   [ -n "$LOCALES" ] && cp /etc/debootstrap/locale.gen  $MNTPOINT/etc/locale.gen
319
320   # copy any existing existing files to chroot
321   [ -d /etc/debootstrap/boot  ] && cp -a /etc/debootstrap/boot/*  $MNTPOINT/boot/
322   [ -d /etc/debootstrap/etc   ] && cp -a /etc/debootstrap/etc/*   $MNTPOINT/etc/
323   [ -d /etc/debootstrap/share ] && cp -a /etc/debootstrap/share/* $MNTPOINT/share/
324   [ -d /etc/debootstrap/usr   ] && cp -a /etc/debootstrap/usr/*   $MNTPOINT/usr/
325   [ -d /etc/debootstrap/var   ] && cp -a /etc/debootstrap/var/*   $MNTPOINT/var/
326   eend 0
327 }
328 # }}}
329
330 # execute chroot-script {{{
331 chrootscript() {
332   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
333      mount_target
334   fi
335   if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
336      einfo "Executing chroot-script now"
337      chroot "$MNTPOINT" /bin/chroot-script
338      eend $?
339   else
340      eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
341      eend 1
342   fi
343 }
344 # }}}
345
346 # install booloader grub {{{
347 grub_install() {
348   if [ -z "$GRUB" -o -z "$GROOT" ] ; then
349      echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
350   else
351      einfo "Installing grub on ${GRUB}:"
352      [ -x /usr/sbin/grub-install ] && GRUBINSTALL=/usr/sbin/grub-install || GRUBINSTALL=/sbin/grub-install
353      $GRUBINSTALL --root-directory="$MNTPOINT" "(${GRUB})"
354      eend $?
355   fi
356 }
357 # }}}
358
359 # unmount $MNTPOINRT {{{
360 umount_chroot() {
361   if [ -n "$PARTITION" ] ; then
362      einfo "Unmount $MNTPOINT"
363      umount $MNTPOINT
364      eend $?
365   fi
366 }
367 # }}}
368
369 # execute filesystem check {{{
370 fscktool() {
371   if [ "$FSCK" = 'yes' ] ; then
372      [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
373      einfo "Checking filesystem on $TARGET using $FSCKTOOL"
374      $FSCKTOOL $TARGET
375      eend $?
376   fi
377 }
378 # }}}
379
380 # now execute all the functions {{{
381 for i in mkfs tunefs mount_target debootstrap_system preparechroot \
382          chrootscript grub_install umount_chroot fscktool ; do
383     if stage $i ; then
384        $i && stage $i done || bailout 2 "i"
385     fi
386 done
387 # }}}
388
389 # stages {{{
390   echo done > $STAGES/grml-debootstrap
391 # }}}
392
393 if [ -n "$AUTOINSTALL" ] ; then
394    dialog --title "$0" --msgbox \
395           "Finished execution of ${0}.
396 Enjoy your Debian system." 6 60
397 else
398    einfo "Finished execution of $0 - enjoy your Debian system." ; eend 0
399 fi
400
401 ## END OF FILE #################################################################
402 # vim: ai tw=100 expandtab foldmethod=marker