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