2 # Filename: grml-debootstrap
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 ################################################################################
8 # http://www.debian.org/releases/stable/i386/index.html.en
10 set -e # exit on any error
14 VERSION="$(dpkg --list $PN 2>/dev/null| awk '/^i/ {print $3}')"
15 VERSION="${VERSION:-unknown}"
16 MNTPOINT="/mnt/debootstrap.$$"
22 # inside the chroot system locales might not be available, so use minimum:
26 # make sure interactive mode is only executed when
27 # using an empty configuration file or option --interactive
33 echo "$PN - wrapper around debootstrap for installing Debian
39 -m, --mirror <URL> Mirror which should be used for apt-get/aptitude.
40 -i, --iso <mnt> Mountpoint where a Debian ISO is mounted to, for use
41 instead of fetching packages from a mirror.
42 -r, --release <name> Release of new Debian system (default: squeeze).
43 -t, --target <target> Target partition (/dev/...) or directory where the
44 system should be installed to.
45 -p, --mntpoint <mnt> Mountpoint used for mounting the target system,
46 has no effect if -t is given and represents a directory.
47 --debopt <params> Extra parameters passed to the debootstrap command.
48 --interactive Use interactive mode (frontend).
49 --nodebootstrap Skip debootstrap, only do configuration to the target.
50 --grub <device> Target for grub installation. Usage example: /dev/sda
51 --arch <arch> Architecture to use. Currently only i386 is supported.
52 --filesystem <fs> Filesystem that should be used when target is a partition
53 or Virtual Machine (see --vmfile).
54 --insecure Do not download and check Release file signatures.
55 --force Do not prompt for user acknowledgement.
57 Options for Virtual Machine deployment:
59 --vmfile Set up a Virtual Machine instead of installing to
60 a partition or directory, to be combined with --target,
61 like: --vmfile --target /mnt/sda1/qemu.img
62 --vmsize <size> Use specified size for size of VM file (default: 2G).
63 Syntax as supported by qemu-img, like: --vmsize 3G
65 Configuration options:
67 -c, --config <file> Use specified configuration file, defaults to
68 /etc/debootstrap/config
69 -d, --confdir <path> Place of config files for debootstrap, defaults
71 --packages <file> Install packages defined in specified list file.
72 --nopackages Skip installation of packages defined in
73 /etc/debootstrap/packages
74 --debconf <file> Pre-seed packages using specified pre-seed db file.
75 --keep_src_list Do not overwrite user provided apt sources.list.
76 --hostname <name> Hostname of Debian system.
77 --password <pwd> Use specified password as password for user root.
78 --bootappend <line> Add specified appendline to kernel whilst booting.
79 --chroot-scripts <d> Execute chroot scripts from specified directory.
80 --pre-scripts <dir> Execute scripts from specified directory (before chroot-scripts).
81 --scripts <dir> Execute scripts from specified directory (after chroot-scripts).
85 -v, --verbose Increase verbosity.
86 -h, --help Print this usage information and exit.
87 -V, --version Show summary of options and exit.
89 Usage examples can be found in the grml-debootstrap manpage.
90 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
94 if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
96 echo 'Please notice that this script requires root permissions!'
115 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
116 printf " ${GOOD}*${NORMAL} $*"
122 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
123 printf " ${BAD}*${NORMAL} $*\n"
129 local retval="${1:-0}"
131 if [ $retval -gt 0 ]; then
132 printf " ${BAD}-> Failed (rc=${retval})${NORMAL}\n"
137 if [ "$(id -u 2>/dev/null)" != 0 ] ; then
138 echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
145 which $arg >/dev/null 2>&1 || RC="$arg"
147 if [ -n "$RC" ] ; then
148 echo "$RC not installed"
155 # make sure we have what we need {{{
156 check4progs debootstrap dialog || exit 1
159 # source main configuration file {{{
160 if [ -r /etc/debootstrap/config ] ; then
161 . /etc/debootstrap/config
165 # cmdline handling {{{
166 # source external command line parameter-processing script
167 if [ -r ./cmdlineopts.clp ] ; then
169 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
170 . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
172 eerror "Error: cmdline function file not found, exiting."
177 # == business-logic of command line parameter-processing
179 # source configuration file in <confdir> if supplied. {{{
180 [ "$_opt_confdir" ] && {
181 CONFFILES=$_opt_confdir
182 einfo "Using config files under $CONFFILES/."
183 if ! [ -r "$CONFFILES/config" ] ; then
184 eerror "Error: config file $CONFFILES/config not found."; eend 1; exit 1
186 if ! . "$CONFFILES/config" ; then
187 eerror "Error reading config file $CONFFILES/config" ; eend 1 ; exit 1
189 # restore the command line parameter value
190 CONFFILES=$_opt_confdir
194 [ "$_opt_mirror" ] && MIRROR=$_opt_mirror
195 [ "$_opt_iso" ] && ISO=$_opt_iso
196 [ "$_opt_release" ] && RELEASE=$_opt_release
197 [ "$_opt_target" ] && TARGET=$_opt_target
198 [ "$_opt_vmfile" ] && VIRTUAL=1
199 [ "$_opt_vmsize" ] && VMSIZE=$_opt_vmsize
200 [ "$_opt_mntpoint" ] && MNTPOINT=$_opt_mntpoint
201 [ "$_opt_debopt" ] && DEBOOTSTRAP_OPT=$_opt_debopt
202 [ "$_opt_interactive" ] && INTERACTIVE=1
203 [ "$_opt_config" ] && CONFIGFILE=$_opt_config
204 [ "$_opt_filesystem" ] && MKFS="mkfs.$_opt_filesystem"
205 [ "$_opt_packages_set" ] && PACKAGES='yes'
206 [ "$_opt_nopackages" ] && PACKAGES=''
207 [ "$_opt_debconf_set" ] && DEBCONF='yes'
208 [ "$_opt_scripts_set" ] && SCRIPTS='yes'
209 [ "$_opt_pre_scripts_set" ] && PRE_SCRIPTS='yes'
210 [ "$_opt_chroot_scripts_set" ] && CHROOT_SCRIPTS='yes'
211 [ "$_opt_keep_src_list" ] && KEEP_SRC_LIST='yes'
212 [ "$_opt_hostname" ] && HOSTNAME=$_opt_hostname
213 [ "$_opt_password" ] && ROOTPASSWORD=$_opt_password
214 [ "$_opt_bootappend" ] && BOOT_APPEND=$_opt_bootappend
215 [ "$_opt_grub" ] && GRUB=$_opt_grub
216 [ "$_opt_arch" ] && ARCH=$_opt_arch
217 [ "$_opt_insecure" ] && SECURE='false'
218 [ "$_opt_force" ] && FORCE=$_opt_force
219 [ "$_opt_verbose" ] && VERBOSE="-v"
221 [ "$_opt_help" ] && {
227 [ "$_opt_version" ] && {
228 einfo "$PN - version $VERSION"
229 einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
235 # check for root permissions {{{
236 if ! check4root ; then
237 echo "For usage instructions please execute '$PN --help'."
242 # make sure we have what we need {{{
243 if [ -n "$VIRTUAL" ] ; then
244 check4progs kpartx mksh qemu-img || exit 1
248 # source specified configuration file {{{
249 if [ -n "$CONFIGFILE" ] ; then
250 einfo "Reading specified config file $CONFIGFILE."
251 if ! . "$CONFIGFILE" ; then
252 eerror "Error reading config file $CONFIGFILE" ; eend 1 ; exit 1
257 # backwards compability checks {{{
258 if [ -n "$GROOT" ] ; then
259 eerror "Error: you seem to have \$GROOT configured."
260 eerror "This variable is no longer supported, please visit the"
261 eerror "grml-debootstrap documentation for details."
266 if echo "$GRUB" | grep -q '^hd' ; then
267 eerror "Error: this syntax for the grub configuration variable is no longer supported."
268 eerror "Please do not use hd... any longer but /dev/sdX instead."
277 dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
278 Do you want to continue installing Debian using this frontend?" 0 0
285 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
287 grep -v 'Extended$' | \
288 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
290 if [ -z "$AVAILABLE_PARTITIONS" ] ; then
291 dialog --title "$PN" --trim \
292 --msgbox "Sorry, no partitions found. Please configure your
293 harddisks (see /proc/partitions) using a tool like fdisk,
294 cfdisk, gpart, gparted,..." 0 0
298 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
299 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet])"
302 TARGET=$(dialog --title "$PN" --single-quoted --stdout \
303 --menu "Please select the target partition:" 0 0 0 \
308 # ask for bootmanager {{{
309 prompt_for_bootmanager()
313 if echo "$TARGET" | grep -q "/dev/md" ; then
314 MBRPART="all disks of Software RAID $TARGET"
316 # figure out whole disk device
318 for device in /dev/disk/by-id/*
320 [ $(readlink -f $device) = ${TARGET} ] || continue
324 [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
325 if [ -e "$MBRDISK" ]; then
326 MBRDISK=$(readlink -f $MBRDISK)
328 # fall back to old behaviour
329 MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$//')
332 MBRPART="MBR of $MBRDISK"
335 for device in cciss/c0d0 sda hda; do
336 if [ /dev/$device != ${MBRDISK} ]; then
337 grep -q $device /proc/partitions && \
338 ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
341 ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
345 GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
346 --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
347 mbr "install bootmanager into $MBRPART" \
348 nowhere "do not install bootmanager at all" \
349 ${ADDITIONAL_PARAMS})
350 [ $? -eq 0 ] || bailout 3
355 # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
356 if echo "$TARGET" | grep -q "/dev/md" ; then
358 if [ -n "$SELECTED_PARTITIONS" ] ; then
359 GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
380 # ask for Debian release {{{
383 [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='squeeze'
384 RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
385 "Please enter the Debian release you would like to use for installation:" \
387 lenny Debian/old-stable \
388 squeeze Debian/stable \
389 wheezy Debian/testing \
390 sid Debian/unstable)"
394 # ask for hostname {{{
395 prompt_for_hostname()
397 HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
398 "Please enter the hostname you would like to use for installation:" \
403 # ask for password {{{
404 prompt_for_password()
408 while [ "$ROOTPW1" != "$ROOTPW2" ]; do
409 ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
410 "Please enter the password for the root account:" 10 60)
411 ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
412 "Please enter the password for the root account again for \
413 confirmation:" 10 60)
415 if [ "$ROOTPW1" != "$ROOTPW2" ]; then
416 $(dialog --stdout --title "${PN}" --ok-label \
417 "Retry" --msgbox "Passwords do not match!" 10 60)
420 ROOTPASSWORD="$ROOTPW1"
424 # ask for Debian mirror {{{
427 [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
429 CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
430 --menu "Where do you want to install from?" 0 0 0 \
431 net "install via network (downloading from mirror)" \
432 local "install from local directory/mirror"
435 if [ "$CHOOSE_MIRROR" = 'net' ] ; then
436 [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
437 MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
438 "Please enter Debian mirror you would like to use for installing packages." \
440 else # CHOOSE_MIRROR == local
441 [ -n "$ISO" ] || ISO='/mnt/mirror'
442 ISO="$(dialog --stdout --title "${PN}" --inputbox \
443 "Please enter directory name you would like to use for installing packages." \
449 # software raid setup {{{
450 config_swraid_setup()
454 # Currently we support only raid1:
457 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
458 # --menu "Which RAID level do you want to use?" 0 0 0 \
459 # raid1 "Software RAID level 1" \
460 # raid5 "Software RAID level 5")
461 #[ $? -eq 0 ] || bailout 20
463 MD_LIST=$(for i in $(seq 0 9) ; do
464 awk '{print $4}' /proc/partitions | grep -q md$i || \
465 echo "/dev/md$i /dev/md$i"
468 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
469 --menu "Which device do you want to use for ${RAIDLEVEL}?
471 Notice: activated devices will not be listed for security reasons. Anyway, please make sure the selected device is not in use already!" 0 0 0 \
473 [ $? -eq 0 ] || bailout 20
475 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
477 grep -v 'Extended$' | \
478 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
479 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
480 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
481 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
484 dialog --title "$PN" --separate-output \
485 --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
486 $PARTITION_LIST 2>$TMPFILE
488 SELECTED_PARTITIONS="$(cat $TMPFILE)"
491 for i in $(cat $TMPFILE) ; do
492 NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
495 # force metadata version 0.90 for lenny so old grub can boot from this array.
497 if [ $RELEASE = "lenny" ]; then
498 METADATA_VERSION="-e0"
503 # TODO: better error handling?
504 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
505 --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
509 if [ "$RC" = 0 ] ; then
510 dialog --title "$PN" --msgbox \
511 "Creating $TARGET was successful." 0 0
512 rm -f "$TMPFILE" "$ERRORFILE"
514 dialog --title "$PN" --msgbox \
515 "There was an error setting up $TARGET:
520 rm -f "$TMPFILE" "$ERRORFILE"
528 if dialog --stdout --title "$PN" \
529 --defaultno --yesno "Do you want to configure Software RAID?
531 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
537 # user should recheck his configuration {{{
538 # support full automatic installation:
540 dialog --timeout 10 --title "$PN" \
541 --yesno "Do you want to stop at this stage?
543 Notice: you are running ${PN} in non-interactive mode.
544 ${PN} will install Debian ${RELEASE} on ${TARGET}.
545 Last chance to quit. Timeout of 10 seconds running....
547 Do you want to stop now?" 0 0 2>/dev/null
550 # make sure the user is aware of the used configuration {{{
553 if [ -n "$AUTOINSTALL" ] ; then
554 if checkforrun ; then
555 eerror "Exiting as requested" ; eend 0
558 elif [ -n "$INTERACTIVE" ] ; then
560 INFOTEXT="Please recheck configuration before execution:
562 [ -n "$TARGET" ] && INFOTEXT="$INFOTEXT
564 [ -n "$GRUB" ] && INFOTEXT="$INFOTEXT
566 [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
567 Using release: $RELEASE"
568 [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
569 Using hostname $HOSTNAME"
570 [ -n "$MIRROR" ] && INFOTEXT="$INFOTEXT
571 Using mirror: $MIRROR"
572 [ -n "$ISO" ] && INFOTEXT="$INFOTEXT
577 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
579 dialog --title "$PN" --no-collapse \
580 --yesno "$INFOTEXT" 0 0
582 else # if not running automatic installation display configuration and prompt for execution:
583 einfo "$PN - Please recheck configuration before execution:"
585 echo " Target: $TARGET"
587 # do not display if MNTPOINT is the default one
588 case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo " Mount point: $MNTPOINT" ;; esac
590 if [ -n "$VIRTUAL" ] ; then
591 echo " Install grub: yes"
593 [ -n "$GRUB" ] && echo " Install grub: $GRUB" || echo " Install grub: no"
596 [ -n "$RELEASE" ] && echo " Using release: $RELEASE"
597 [ -n "$MIRROR" ] && echo " Using mirror: $MIRROR"
598 [ -n "$HOSTNAME" ] && echo " Using hostname: $HOSTNAME"
599 [ -n "$ISO" ] && echo " Using ISO: $ISO"
600 if [ -n "$VIRTUAL" ] ; then
601 echo " Deploying as Virtual Machine."
602 [ -n "$VMSIZE" ] && echo " Using Virtual Disk file with size of ${VMSIZE}."
606 echo " Important! Continuing will delete all data from ${TARGET}!"
608 if [ -n "$FORCE" ] ; then
609 einfo "Skip user acknowledgement as requested via --force option."
612 einfon "Is this ok for you? [y/N] "
614 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
615 eerror "Exiting as requested." ; eend 1
623 # interactive mode {{{
634 prompt_for_bootmanager
643 # run interactive mode if we didn't get the according configuration yet
644 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
645 # only target might be unset, so make sure the INTERACTIVE flag is set as well
653 # finally make sure at least $TARGET is set [the partition for the new system] {{{
654 if [ -n "$TARGET" ] ; then
655 SHORT_TARGET="${TARGET##*/}"
657 eerror "Please adjust $CONFFILES/config or..."
658 eerror "... use the interactive version for configuration before running ${0}" ; eend 1
664 if [ -z "$STAGES" ] ; then
665 STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
666 [ -d "$STAGES" ] || mkdir -p "$STAGES"
669 if [ -r "$STAGES"/grml-debootstrap ] ; then
670 if grep -q done $STAGES/grml-debootstrap ; then
671 eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
672 eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
677 # partition handling {{{
681 set_target_directory(){
682 # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
689 # make sure we normalise the path to an absolute directory name so something like:
690 # mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
692 TARGET="$(readlink -f $TARGET)"
695 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
702 # architecture setup {{{
703 if [ -n "$ARCH" ] ; then
704 ARCHCMD="--arch $ARCH"
705 ARCHINFO=" (${ARCH})"
707 ARCH="$(dpkg --print-architecture)"
708 ARCHCMD="--arch $ARCH"
709 ARCHINFO=" (${ARCH})"
715 if [ "$SECURE" = 'yes' ] ; then
716 if [ -e '/etc/apt/trusted.gpg' ] ; then
717 KEYRING="--keyring /etc/apt/trusted.gpg"
719 eerror "Could not find /etc/apt/trusted.gpg."
722 ewarn "Not checking Release signatures!"
726 # make sure we have the right syntax when using an iso image {{{
727 if [ -n "$ISO" ] ; then
740 # helper functions {{{
741 # we want to exit smoothly and clean:
743 # make sure $TARGET is not mounted when exiting grml-debootstrap
744 if [ -n "$MNTPOINT" ] ; then
745 if grep -q $MNTPOINT /proc/mounts ; then
746 # make sure nothing is left inside chroot so we can unmount it
747 [ -x "$MNTPOINT"/etc/init.d/ssh ] && "$MNTPOINT"/etc/init.d/ssh stop
748 [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
749 # ugly, but make sure we really don't leave anything (/proc /proc is intended)
750 for ARG in /sys /proc /proc ; do
751 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true
753 umount "$MNTPOINT"/dev >/dev/null 2>&1 || true
755 [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1 || true
757 if [ -n "$DIRECTORY" ] ; then
758 einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
760 einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
763 if [ -n "$STAGES" ] ; then
764 echo -n "Removing stages directory ${STAGES}: "
765 rm -rf "$STAGES" && echo done
768 # remove directory only if we used the default with process id inside the name
769 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
770 einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
775 if [ -n "${ORIG_TARGET}" ] ; then
776 einfo "Removing loopback mount of file ${ORIG_TARGET}."
777 kpartx -d "${ORIG_TARGET}" ; eend $?
780 [ -n "$1" ] && EXIT="$1" || EXIT="1"
781 [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
785 trap bailout HUP INT QUIT TERM
787 # we want to execute all the functions only once, simple check for it:
789 if [ -n "$2" ] ; then
790 echo "$2" > "${STAGES}/${1}"
792 elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
793 ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
794 ewarn " To reexecute it clean up the according directory inside $STAGES" ; eend 0
800 # create filesystem {{{
802 if [ -n "$DIRECTORY" ] ; then
803 einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
805 if grep -q "$TARGET" /proc/mounts ; then
806 eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
810 if [ -n "$MKFS" ] ; then
811 einfo "Running $MKFS on $TARGET"
812 $MKFS $TARGET ; RC=$?
814 # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
815 # will fail to detect the uuid in the chroot
816 if echo "$TARGET" | grep -q "/dev/md" ; then
817 blockdev --rereadpt "${TARGET}"
818 elif ! [ -n "$VIRTUAL" ] ; then
819 blockdev --rereadpt "${TARGET%%[0-9]*}"
821 # give the system 2 seconds, otherwise we might run into
822 # race conditions :-/
825 eval $(blkid -o udev $TARGET 2>/dev/null)
826 [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
835 # modify filesystem settings {{{
837 if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
838 einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
845 # mount the new partition or if it's a directory do nothing at all {{{
847 if [ -n "$DIRECTORY" ] ; then
848 einfo "Running grml-debootstrap on a directory, nothing to mount."
850 if grep -q $TARGET /proc/mounts ; then
851 ewarn "$TARGET already mounted, continuing anyway." ; eend 0
853 if ! [ -d "${MNTPOINT}" ] ; then
854 [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
856 einfo "Mounting $TARGET to $MNTPOINT"
858 mount -o rw,suid,dev $TARGET $MNTPOINT
862 if [ -n "$ISODIR" ] ; then
863 einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
864 mkdir -p "$MNTPOINT/$ISODIR"
865 mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
871 # prepare VM image for usage with debootstrap {{{
873 if [ -z "$VIRTUAL" ] ; then
874 return 0 # be quite by intention
877 if [ -b "$TARGET" ] ; then
878 eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
883 ORIG_TARGET="$TARGET" # store for later reuse
885 qemu-img create -f raw "${TARGET}" "${VMSIZE}"
886 echo 4 66 | mksh /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
887 dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
888 parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
890 DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
891 if [ -z "${DEVINFO}" ] ; then
892 eerror "Error setting up loopback device." ; eend 1
896 # hopefully this always works as expected
897 LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
898 BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/') # '253:0'
899 LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
900 export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
902 blockdev --rereadpt "${LOOP}"
904 if [ -z "$TARGET" ] ; then
905 eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
911 # make VM image bootable and unmount it {{{
913 if [ -z "${VIRTUAL}" ] ; then
917 if ! mount "${TARGET}" "${MNTPOINT}" ; then
918 eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
922 einfo "Installing Grub as bootloader."
923 mount -t proc none "${MNTPOINT}"/proc
924 mount -t sysfs none "${MNTPOINT}"/sys
925 mount --bind /dev "${MNTPOINT}"/dev
927 mkdir -p "${MNTPOINT}/boot/grub"
928 if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
929 eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
933 cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
934 chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
935 dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
936 rm -f "${MNTPOINT}/tmp/core.img"
938 einfo "Updating grub configuration file."
939 chroot "${MNTPOINT}" update-grub
941 umount "${MNTPOINT}"/proc
942 umount "${MNTPOINT}"/sys
943 umount "${MNTPOINT}"/dev
945 einfo "Adjusting grub.cfg for successful boot sequence."
946 # ugly but needed to boot grub acordingly
947 sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
948 sed -i "s; root=/dev/.*; root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
951 kpartx -d "${ORIG_TARGET}" >/dev/null
955 # install main chroot {{{
956 debootstrap_system() {
957 if [ "$_opt_nodebootstrap" ]; then
958 einfo "Skipping debootstrap as requested."
962 if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
963 einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
964 if [ -n "$MIRROR" ] ; then
965 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
966 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
968 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
969 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
973 eerror "Error: $MNTPOINT not mounted, can not continue."
979 # prepare chroot via chroot-script {{{
981 einfo "Preparing chroot system"
983 # provide variables to chroot system
984 CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
985 touch $CHROOT_VARIABLES
986 chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
987 echo "# Configuration of ${PN}" > $CHROOT_VARIABLES
988 [ -n "$ARCH" ] && echo "ARCH=$ARCH" >> $CHROOT_VARIABLES
989 [ -n "$GRUB" ] && echo "GRUB=$GRUB" >> $CHROOT_VARIABLES
990 [ -n "$HOSTNAME" ] && echo "HOSTNAME=$HOSTNAME" >> $CHROOT_VARIABLES
991 [ -n "$INSTALL_NOTES" ] && echo "INSTALL_NOTES=$INSTALL_NOTES" >> $CHROOT_VARIABLES
992 [ -n "$ISODIR" ] && echo "ISODIR=$ISO" >> $CHROOT_VARIABLES
993 [ -n "$ISO" ] && echo "ISO=$ISO" >> $CHROOT_VARIABLES
994 [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=$KEEP_SRC_LIST" >> $CHROOT_VARIABLES
995 [ -n "$MIRROR" ] && echo "MIRROR=$MIRROR" >> $CHROOT_VARIABLES
996 [ -n "$PACKAGES" ] && echo "PACKAGES=$PACKAGES" >> $CHROOT_VARIABLES
997 [ -n "$RM_APTCACHE" ] && echo "RM_APTCACHE=$RM_APTCACHE" >> $CHROOT_VARIABLES
998 [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
999 [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=$SELECTED_PARTITIONS" >> $CHROOT_VARIABLES
1000 [ -n "$TARGET" ] && echo "TARGET=$TARGET" >> $CHROOT_VARIABLES
1001 [ -n "$TARGET_UUID" ] && echo "TARGET_UUID=$TARGET_UUID" >> $CHROOT_VARIABLES
1003 cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
1004 chmod 755 $MNTPOINT/bin/chroot-script
1005 [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
1007 # make sure we have our files for later use via chroot-script
1008 cp $VERBOSE $CONFFILES/config $MNTPOINT/etc/debootstrap/
1009 # make sure we adjust the configuration variables accordingly:
1010 sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
1011 sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#" $MNTPOINT/etc/debootstrap/config
1012 sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#" $MNTPOINT/etc/debootstrap/config
1015 if [ -n "$INSTALL_NOTES" ] ; then
1016 [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
1019 # package selection:
1020 cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
1021 $MNTPOINT/etc/debootstrap/packages
1023 # debconf preseeding:
1024 _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
1025 [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
1026 cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
1028 # copy scripts that should be executed inside the chroot:
1029 _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
1030 [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
1031 mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
1032 cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
1035 # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
1036 cp $VERBOSE $CHROOT_VARIABLES $MNTPOINT/etc/debootstrap/variables
1038 cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
1040 # make sure we can access network [relevant for cdebootstrap]
1041 [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
1043 # provide system's /etc/hosts to the target:
1044 if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
1045 cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
1048 # setup default locales
1049 [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen $MNTPOINT/etc/locale.gen
1051 # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
1052 ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
1054 # copy any existing files to chroot
1055 [ -d $CONFFILES/bin ] && cp $VERBOSE -a -L $CONFFILES/bin/* $MNTPOINT/bin/
1056 [ -d $CONFFILES/boot ] && cp $VERBOSE -a -L $CONFFILES/boot/* $MNTPOINT/boot/
1057 [ -d $CONFFILES/etc ] && cp $VERBOSE -a -L $CONFFILES/etc/* $MNTPOINT/etc/
1058 [ -d $CONFFILES/sbin ] && cp $VERBOSE -a -L $CONFFILES/sbin/* $MNTPOINT/sbin/
1059 [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
1060 [ -d $CONFFILES/usr ] && cp $VERBOSE -a -L $CONFFILES/usr/* $MNTPOINT/usr/
1061 [ -d $CONFFILES/var ] && cp $VERBOSE -a -L $CONFFILES/var/* $MNTPOINT/var/
1063 # copy local network setup to chroot
1064 if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
1065 [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
1066 cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
1069 # install config file providing some example entries
1070 if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1071 cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1078 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1079 execute_pre_scripts() {
1080 # make sure we have $MNTPOINT available for our scripts
1082 if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1083 [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1084 for script in ${pre_scripts}/* ; do
1085 if [ -x "$script" ] ; then
1086 einfo "Executing pre-script $script"
1094 # execute all scripts in /etc/debootstrap/scripts/ {{{
1096 # make sure we have $MNTPOINT available for our scripts
1098 if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1099 [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1100 for script in ${scripts}/* ; do
1101 if [ -x "$script" ] ; then
1102 einfo "Executing script $script"
1110 # execute chroot-script {{{
1112 if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1116 if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1117 einfo "Executing chroot-script now"
1118 mount --bind /dev "$MNTPOINT"/dev
1119 chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1120 umount "$MNTPOINT"/dev
1123 eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1129 # unmount $MNTPOINT {{{
1132 # display installation notes:
1133 if [ -n "$INSTALL_NOTES" ] ; then
1134 [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1137 if [ -n "$ISODIR" ] ; then
1138 if grep -q "$ISODIR" /proc/mounts ; then
1139 einfo "Unmount $MNTPOINT/$ISODIR"
1140 umount "$MNTPOINT/$ISODIR"
1145 if grep -q "$MNTPOINT" /proc/mounts ; then
1146 if [ -n "$PARTITION" ] ; then
1147 einfo "Unmount $MNTPOINT"
1155 # execute filesystem check {{{
1157 if [ "$FSCK" = 'yes' ] ; then
1158 [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1159 einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1166 # now execute all the functions {{{
1167 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1168 preparechroot execute_pre_scripts chrootscript execute_scripts \
1169 umount_chroot finalize_vm fscktool ; do
1170 if stage "${i}" ; then
1171 $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
1177 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
1178 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
1180 # Remove temporary mountpoint again
1181 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
1182 einfo "Removing directory ${MNTPOINT}" ; rmdir "$MNTPOINT" ; eend $?
1186 # end dialog of autoinstallation {{{
1187 if [ -n "$AUTOINSTALL" ] ; then
1188 if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1189 Automatically rebooting in 10 seconds.
1191 Choose Cancel to skip rebooting." 10 60 10 ; then
1192 noeject noprompt reboot
1195 einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1199 ## END OF FILE #################################################################
1200 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=3