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