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: Wed Aug 27 01:46:13 CEST 2008 [mika]
8 ################################################################################
9 # http://www.debian.org/releases/stable/i386/index.html.en
11 set -e # exit on any error
16 MNTPOINT="/mnt/debootstrap.$$"
18 # inside the chroot system locales might not be available, so use minimum:
22 # make sure interactive mode is only executed when
23 # using an empty configuration file or option --interactive
27 # source core functions {{{
28 . /etc/grml/lsb-functions
29 . /etc/grml/script-functions
34 echo "$PN - wrapper around debootstrap for installing Debian
38 -h|--help Print this usage information and exit.
39 -v|--version Show summary of options and exit.
41 -c|--config <configfile> Use specified configuration file,
42 defaults to /etc/debootstrap/config
43 -i|--iso <mnt> Mountpoint where a Debian ISO is mounted to,
44 for use instead of fetching packages from a mirror
45 -m|--mirror <URL> Mirror which should be used for apt-get/aptitude.
46 -p|--mntpoint <mnt> Mountpoint used for mounting the target system.
47 -r|--release <release> Release of new Debian system (default: stable)
48 -t|--target <target> Target partition (/dev/...) or directory.
50 --boot_append <appendline> Add specified appendline to kernel whilst booting
51 --groot <device> Root device for usage in grub, corresponds with
52 \$TARGET in grub syntax, like hd0,0 for /dev/sda1
53 --grub <device> Target for grub installation. Use grub syntax
54 for specifying, like hd0 for /dev/sda
55 --interactive Use interactive mode (frontend)
56 --password <pwd> Use specified password as password for user root.
58 Send bugreports to the grml-team: bugs@grml.org || http://grml.org/bugs/
62 if [ "$1" = '-h' ] || [ "$1" == '-help' ] ; then
64 echo 'Please notice that this script requires root permissions!'
69 # make sure we have what we need {{{
70 check4progs debootstrap dialog || exit 1
74 # source configuration file {{{
75 if [ -r /etc/debootstrap/config ] ; then
76 if [ -n "$CONFIGFILE" ] ; then
77 if ! . "$CONFIGFILE" ; then
78 eerror "Error reading config file $CONFIGFILE" ; eend 1 ; exit 1
81 . /etc/debootstrap/config
86 # cmdline handling {{{
88 while [ "$#" -gt "0" ] ; do
112 [ -n "$MIRROR" ] && unset MIRROR
137 einfo "$PN - version $VERSION"
138 einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
143 eerror "Syntax error."
155 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
157 grep -v 'Extended$' | \
158 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
160 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
161 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
162 echo "$i $(vol_id --type $i 2>/dev/null || echo [no_filesystem_yet])"
165 TARGET=$(dialog --title "$PN" --single-quoted --stdout \
166 --menu "Please select the target partition:" 0 0 0 \
171 # ask for bootmanager {{{
172 prompt_for_bootmanager()
175 for device in sda hda; do
176 if [ /dev/$device != ${TARGET%[0-9]} ]; then
177 grep -q $device /proc/partitions && \
178 ADDITIONAL_PARAMS=:$device:"install bootmanager grub into MBR of /dev/${device}"
181 ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
185 [[ $TARGET == *md* ]] && MBRPART=$TARGET || MBRPART="${TARGET%[0-9]}"
187 GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
188 --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
189 mbr "install bootmanager into MBR of $MBRPART" \
190 partition "install bootmanager into partition $TARGET" \
191 nowhere "do NOT install bootmanager at all" \
192 ${ADDITIONAL_PARAMS})
193 [ $? -eq 0 ] || bailout 3
199 # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
200 if [[ $TARGET == *md* ]] ; then
201 BOOT_PARTITION=${TARGET}
203 BOOT_PARTITION=${TARGET%[0-9]}
207 BOOT_PARTITION="$TARGET"
211 BOOT_PARTITION="/dev/hda"
215 BOOT_PARTITION="/dev/sda"
221 BOOT_PARTITION="$GETMBR"
227 # ask for Debian mirror {{{
230 MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
231 "Please enter Debian mirror you would like to use for installing packages." \
232 0 0 http://ftp.de.debian.org/debian)"
236 # get grub's syntax for /dev/ice
237 # usage example: 'grubdevice /dev/hda2' returns '(hd0,1)'
239 if [ -z "$1" ] ; then
240 echo "Usage: grubdevice <device>">&2
245 device_map=/boot/grub/device.map
248 if ! [ -f "$device_map" ] ; then
249 echo 'quit' | grub --device-map="$device_map" 1>/dev/null 2>&1
252 # taken from d-i's trunk/packages/arch/i386/grub-installer/grub-installer:
253 tmp_disk=`echo "$device" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
254 -e 's%\(fd[0-9]*\)$%\1%' \
255 -e 's%/part[0-9]*$%/disc%' \
256 -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
257 tmp_part=`echo "$device" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \
258 -e 's%.*/fd[0-9]*$%%' \
259 -e 's%.*/floppy/[0-9]*$%%' \
260 -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
261 -e 's%.*c[0-7]d[0-9]*p*%%'`
262 tmp_drive=$(grep -v '^#' $device_map | grep "$tmp_disk *$" | sed 's%.*\([hf]d[0-9][a-g0-9,]*\).*%\1%')
263 GRUB="$(echo "$tmp_drive")"
266 /dev/[sh]d[a-z]) # we expect something like 'hd0'
267 GROOT="$(echo "$tmp_drive")"
269 *) # we expect something like 'hd0,0'
270 # make sure we don't install into MBR if $USE_MBR is not set
271 if [ -z "$USE_MBR" ] ; then
272 GROOT="$(echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%")"
274 GROOT="$(echo "$tmp_drive" | sed "s%,[0-9]%%")"
280 # interactive mode {{{
284 prompt_for_bootmanager
289 echo "debug: BOOT_PARTITION=$BOOT_PARTITION"
290 echo "debug: TARGET=$TARGET"
291 echo "debug: MIRROR=$MIRROR"
292 echo "debug: GROOT=$GROOT"
297 # run interactive mode if we didn't get the according configuration yet
298 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
303 # finally make sure at least $TARGET is set [the partition for the new system] {{{
304 if [ -n "$TARGET" ] ; then
305 SHORT_TARGET="${TARGET##*/}"
307 eerror "Please adjust /etc/debootstrap/config or..."
308 eerror "... use the interactive version for configuration before running ${0}" ; eend 1
314 if [ -z "$STAGES" ] ; then
315 STAGES="/etc/debootstrap/stages_${SHORT_TARGET}"
316 [ -d "$STAGES" ] || mkdir -p "$STAGES"
319 if [ -r "$STAGES"/grml-debootstrap ] ; then
320 if grep -q done $STAGES/grml-debootstrap ; then
321 eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
322 eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
327 # partition handling {{{
336 # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
348 # architecture setup {{{
349 if [ -n "$ARCH" ] ; then
350 ARCHCMD="--arch $ARCH"
351 ARCHINFO=" (${ARCH})"
353 ARCH="$(dpkg --print-architecture)"
354 ARCHCMD="--arch $ARCH"
355 ARCHINFO=" (${ARCH})"
359 # make sure we have the right syntax when using an iso image {{{
360 if [ -n "$ISO" ] ; then
373 # provide variables to chroot system {{{
374 CHROOT_VARIABLES="/etc/debootstrap/variables_${SHORT_TARGET}"
375 touch $CHROOT_VARIABLES
376 chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
377 [ -n "$ARCH" ] && echo "ARCH=$ARCH" > $CHROOT_VARIABLES
378 [ -n "$GRUB" ] && echo "GRUB=$GRUB" >> $CHROOT_VARIABLES
379 [ -n "$GROOT" ] && echo "GROOT=$GROOT" >> $CHROOT_VARIABLES
380 [ -n "$TARGET" ] && echo "TARGET=$TARGET" >> $CHROOT_VARIABLES
381 [ -n "$ISO" ] && echo "ISO=$ISO" >> $CHROOT_VARIABLES
382 [ -n "$ISODIR" ] && echo "ISODIR=$ISO" >> $CHROOT_VARIABLES
383 [ -n "$MIRROR" ] && echo "MIRROR=$MIRROR" >> $CHROOT_VARIABLES
384 [ -n "$MIRROR" ] && echo "CHROOTMIRROR=$MIRROR" >> $CHROOT_VARIABLES
385 [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
388 # helper functions {{{
389 # we want to exit smoothly and clean:
391 # make sure $TARGET is not mounted when exiting grml-debootstrap
392 if [ -n "$MNTPOINT" ] ; then
393 if grep -q $MNTPOINT /proc/mounts ; then
394 # make sure nothing is left inside chroot so we can unmount it
395 [ -x "$MNTPOINT"/etc/init.d/ssh ] && "$MNTPOINT"/etc/init.d/ssh stop
396 [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
397 # ugly, but make sure we really don't leav anything
398 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /sys 1>/dev/null 2>&1
399 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a 1>/dev/null 2>&1
400 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
401 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
402 [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" 1>/dev/null 2>&1
403 einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
405 # remove directory only if we used the default with process id inside the name
406 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
407 einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
412 [ -n "$1" ] && EXIT="$1" || EXIT="1"
413 [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
417 trap bailout 1 2 3 15
419 # we want to execute all the functions only once, simple check for it:
421 if [ -n "$2" ] ; then
422 echo "$2" > "${STAGES}/${1}"
424 elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
425 ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
427 ewarn "To reexecute it clean up the according directory inside $STAGES" ; eend 0
434 # user should recheck his configuration {{{
435 # support full automatic installation:
437 dialog --timeout 10 --title "$PN" \
438 --yesno "Do you want to stop at this stage?
440 Notice: you are running grml-debootstrap in non-interactive mode.
441 grml-debootstrap will install Debian ${RELEASE} on ${TARGET}.
442 Last chance to quit. Timeout of 10 seconds running....
444 Do you want to stop now?" 0 0 2>/dev/null
447 if [ -n "$AUTOINSTALL" ] ; then
448 if checkforrun ; then
449 eerror "Exiting as requested" ; eend 0
452 else # if not running automatic installation display configuration and prompt for execution:
453 einfo "$PN - Please recheck configuration before execution:"
455 echo " Target: $TARGET"
456 case "$MNTPOINT" in "$TARGET") ;; *) echo " Mount point: $MNTPOINT" ;; esac
457 [ -n "$GRUB" ] && echo " Install grub to: $GRUB"
458 [ -n "$GROOT" ] && echo " Use root partition in grub: $GROOT"
459 [ -n "$RELEASE" ] && echo " Using release: $RELEASE"
460 [ -n "$MIRROR" ] && echo " Using mirror: $MIRROR"
461 [ -n "$ISO" ] && echo " Using iso: $ISO"
462 case "$MNTPOINT" in "$TARGET") ;; *) echo " Important! Continuing will delete all data from ${TARGET}!" ;; esac
464 einfon "Is this ok for you? [y/N] "
466 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
467 eerror "Exiting as requested." ; eend 1
473 # create filesystem {{{
475 if [ -n "$MKFS" ] ; then
476 einfo "Running $MKFS on $TARGET"
483 # modify filesystem settings {{{
485 if [ -n "$TUNE2FS" ] ; then
486 einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
493 # mount the new partition or if it's a directory do nothing at all {{{
495 if [ -n "$DIRECTORY" ] ; then
496 einfo "Running grml-debootstrap on a directory, nothing to mount."
498 if grep -q $TARGET /proc/mounts ; then
499 eerror "$TARGET already mounted, exiting."
501 [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
502 einfo "Mounting $TARGET to $MNTPOINT"
503 mount -o rw,suid,dev $TARGET $MNTPOINT
507 if [ -n "$ISODIR" ] ; then
508 einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
509 mkdir -p "$MNTPOINT/$ISODIR"
510 mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
516 # install main chroot {{{
517 debootstrap_system() {
518 if ! grep -q $MNTPOINT /proc/mounts ; then
521 if grep -q $MNTPOINT /proc/mounts ; then
522 einfo "Running $DEBOOTSTRAP for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
523 [ -n "$MIRROR" ] && $DEBOOTSTRAP $ARCHCMD $RELEASE $MNTPOINT $MIRROR || \
524 $DEBOOTSTRAP $ARCHCMD $RELEASE $MNTPOINT $ISO
527 eerror "Error: $MNTPOINT not mounted, can not continue."
533 # prepare chroot via chroot-script {{{
535 einfo "Preparing chroot system"
536 cp $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
537 chmod 755 $MNTPOINT/bin/chroot-script
538 mkdir $MNTPOINT/etc/debootstrap/
540 # make sure we have our files for later use via chroot-script
541 cp /etc/debootstrap/config $MNTPOINT/etc/debootstrap/
542 cp /etc/debootstrap/packages $MNTPOINT/etc/debootstrap/packages
543 cp $CHROOT_VARIABLES $MNTPOINT/etc/debootstrap/variables # do NOT use $CHROOT_VARIABLES inside chroot!
545 cp -a /etc/debootstrap/extrapackages/ $MNTPOINT/etc/debootstrap/
547 # make sure we can access network [relevant for cdebootstrap]
548 [ -f "$MNTPOINT/etc/resolv.conf" ] || cp /etc/resolv.conf $MNTPOINT/etc/resolv.conf
550 # setup default locales
551 [ -n "$LOCALES" ] && cp /etc/debootstrap/locale.gen $MNTPOINT/etc/locale.gen
553 # copy any existing existing files to chroot
554 [ -d /etc/debootstrap/boot ] && cp -a /etc/debootstrap/boot/* $MNTPOINT/boot/
555 [ -d /etc/debootstrap/etc ] && cp -a /etc/debootstrap/etc/* $MNTPOINT/etc/
556 [ -d /etc/debootstrap/share ] && cp -a /etc/debootstrap/share/* $MNTPOINT/share/
557 [ -d /etc/debootstrap/usr ] && cp -a /etc/debootstrap/usr/* $MNTPOINT/usr/
558 [ -d /etc/debootstrap/var ] && cp -a /etc/debootstrap/var/* $MNTPOINT/var/
560 # copy local network setup to chroot
561 if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
562 [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
563 cp /etc/network/interfaces $MNTPOINT/etc/network/interfaces
570 # execute chroot-script {{{
572 if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
575 if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
576 einfo "Executing chroot-script now"
577 chroot "$MNTPOINT" /bin/chroot-script
580 eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
586 # install booloader grub {{{
588 if [ -z "$GRUB" -o -z "$GROOT" ] ; then
589 echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
591 einfo "Installing grub on ${GRUB}:"
592 [ -x /usr/sbin/grub-install ] && GRUBINSTALL=/usr/sbin/grub-install || GRUBINSTALL=/sbin/grub-install
593 $GRUBINSTALL --root-directory="$MNTPOINT" "(${GRUB})"
599 # unmount $MNTPOINRT {{{
601 if [ -n "$ISODIR" ] ; then
602 if grep -q "$ISODIR" /proc/mounts ; then
603 einfo "Unmount $MNTPOINT/$ISODIR"
604 umount "$MNTPOINT/$ISODIR"
608 if grep -q "$MNTPOINT" /proc/mounts ; then
609 if [ -n "$PARTITION" ] ; then
610 einfo "Unmount $MNTPOINT"
618 # execute filesystem check {{{
620 if [ "$FSCK" = 'yes' ] ; then
621 [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
622 einfo "Checking filesystem on $TARGET using $FSCKTOOL"
629 # now execute all the functions {{{
630 for i in mkfs tunefs mount_target debootstrap_system preparechroot \
631 chrootscript grub_install umount_chroot fscktool ; do
632 if stage "${i}" ; then
633 $i && stage "${i}" done || bailout 2 "i"
639 echo done > $STAGES/grml-debootstrap
642 # end dialog of autoinstallation {{{
643 if [ -n "$AUTOINSTALL" ] ; then
644 dialog --title "$PN" --msgbox \
645 "Finished execution of ${0}.
646 Enjoy your Debian system." 6 60
648 einfo "Finished execution of $PN - enjoy your Debian system." ; eend 0
652 ## END OF FILE #################################################################
653 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=3