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
31 # source core functions {{{
32 . /etc/grml/lsb-functions
33 . /etc/grml/script-functions
38 echo "$PN - wrapper around debootstrap for installing Debian
44 -m, --mirror <URL> Mirror which should be used for apt-get/aptitude.
45 -i, --iso <mnt> Mountpoint where a Debian ISO is mounted to, for use
46 instead of fetching packages from a mirror.
47 -r, --release <name> Release of new Debian system (default: squeeze).
48 -t, --target <target> Target partition (/dev/...) or directory where the
49 system should be installed to.
50 -p, --mntpoint <mnt> Mountpoint used for mounting the target system,
51 has no effect if -t is given and represents a directory.
52 --debopt <params> Extra parameters passed to the debootstrap command.
53 --interactive Use interactive mode (frontend).
54 --nodebootstrap Skip debootstrap, only do configuration to the target.
55 --grub <device> Target for grub installation. Usage example: /dev/sda
56 --arch <arch> Architecture to use. Currently only i386 is supported.
57 --filesystem <fs> Filesystem that should be used when target is a partition
58 or Virtual Machine (see --vmfile).
59 --insecure Do not download and check Release file signatures.
60 --force Do not prompt for user acknowledgement.
62 Options for Virtual Machine deployment:
64 --vmfile Set up a Virtual Machine instead of installing to
65 a partition or directory, to be combined with --target,
66 like: --vmfile --target /mnt/sda1/qemu.img
67 --vmsize <size> Use specified size for size of VM file (default: 2G).
68 Syntax as supported by qemu-img, like: --vmsize 3G
70 Configuration options:
72 -c, --config <file> Use specified configuration file, defaults to
73 /etc/debootstrap/config
74 -d, --confdir <path> Place of config files for debootstrap, defaults
76 --packages <file> Install packages defined in specified list file.
77 --nopackages Skip installation of packages defined in
78 /etc/debootstrap/packages
79 --debconf <file> Pre-seed packages using specified pre-seed db file.
80 --keep_src_list Do not overwrite user provided apt sources.list.
81 --hostname <name> Hostname of Debian system.
82 --password <pwd> Use specified password as password for user root.
83 --bootappend <line> Add specified appendline to kernel whilst booting.
84 --chroot-scripts <d> Execute chroot scripts from specified directory.
85 --pre-scripts <dir> Execute scripts from specified directory (before chroot-scripts).
86 --scripts <dir> Execute scripts from specified directory (after chroot-scripts).
90 -v, --verbose Increase verbosity.
91 -h, --help Print this usage information and exit.
92 -V, --version Show summary of options and exit.
94 Usage examples can be found in the grml-debootstrap manpage.
95 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
99 if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
101 echo 'Please notice that this script requires root permissions!'
106 # make sure we have what we need {{{
107 check4progs debootstrap dialog || exit 1
110 # source main configuration file {{{
111 if [ -r /etc/debootstrap/config ] ; then
112 . /etc/debootstrap/config
116 # cmdline handling {{{
117 # source external command line parameter-processing script
118 if [ -r ./cmdlineopts.clp ] ; then
120 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
121 . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
123 echo "Error: cmdline function file not found, exiting.">&2
127 # == business-logic of command line parameter-processing
129 # source configuration file in <confdir> if supplied. {{{
130 [ "$_opt_confdir" ] && {
131 CONFFILES=$_opt_confdir
132 einfo "Using config files under $CONFFILES/."
133 if ! [ -r "$CONFFILES/config" ] ; then
134 eerror "Error: config file $CONFFILES/config not found."; eend 1; exit 1
136 if ! . "$CONFFILES/config" ; then
137 eerror "Error reading config file $CONFFILES/config" ; eend 1 ; exit 1
139 # restore the command line parameter value
140 CONFFILES=$_opt_confdir
144 [ "$_opt_mirror" ] && MIRROR=$_opt_mirror
145 [ "$_opt_iso" ] && ISO=$_opt_iso
146 [ "$_opt_release" ] && RELEASE=$_opt_release
147 [ "$_opt_target" ] && TARGET=$_opt_target
148 [ "$_opt_vmfile" ] && VIRTUAL=1
149 [ "$_opt_vmsize" ] && VMSIZE=$_opt_vmsize
150 [ "$_opt_mntpoint" ] && MNTPOINT=$_opt_mntpoint
151 [ "$_opt_debopt" ] && DEBOOTSTRAP_OPT=$_opt_debopt
152 [ "$_opt_interactive" ] && INTERACTIVE=1
153 [ "$_opt_config" ] && CONFIGFILE=$_opt_config
154 [ "$_opt_filesystem" ] && MKFS="mkfs.$_opt_filesystem"
155 [ "$_opt_packages_set" ] && PACKAGES='yes'
156 [ "$_opt_nopackages" ] && PACKAGES=''
157 [ "$_opt_debconf_set" ] && DEBCONF='yes'
158 [ "$_opt_scripts_set" ] && SCRIPTS='yes'
159 [ "$_opt_pre_scripts_set" ] && PRE_SCRIPTS='yes'
160 [ "$_opt_chroot_scripts_set" ] && CHROOT_SCRIPTS='yes'
161 [ "$_opt_keep_src_list" ] && KEEP_SRC_LIST='yes'
162 [ "$_opt_hostname" ] && HOSTNAME=$_opt_hostname
163 [ "$_opt_password" ] && ROOTPASSWORD=$_opt_password
164 [ "$_opt_bootappend" ] && BOOT_APPEND=$_opt_bootappend
165 [ "$_opt_grub" ] && GRUB=$_opt_grub
166 [ "$_opt_arch" ] && ARCH=$_opt_arch
167 [ "$_opt_insecure" ] && SECURE='false'
168 [ "$_opt_force" ] && FORCE=$_opt_force
169 [ "$_opt_verbose" ] && VERBOSE="-v"
171 [ "$_opt_help" ] && {
177 [ "$_opt_version" ] && {
178 einfo "$PN - version $VERSION"
179 einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
185 # check for root permissions {{{
186 if ! check4root ; then
187 echo "For usage instructions please execute '$PN --help'."
192 # make sure we have what we need {{{
193 if [ -n "$VIRTUAL" ] ; then
194 check4progs kpartx mksh qemu-img || exit 1
198 # source specified configuration file {{{
199 if [ -n "$CONFIGFILE" ] ; then
200 einfo "Reading specified config file $CONFIGFILE."
201 if ! . "$CONFIGFILE" ; then
202 eerror "Error reading config file $CONFIGFILE" ; eend 1 ; exit 1
207 # backwards compability checks {{{
208 if [ -n "$GROOT" ] ; then
209 echo "Error: you seem to have \$GROOT configured." >&2
210 echo "This variable is no longer supported, please visit the" >&2
211 echo "grml-debootstrap documentation for details." >&2
215 if echo "$GRUB" | grep -q '^hd' ; then
216 echo "Error: this syntax for the grub configuration variable is no longer supported." >&2
217 echo "Please do not use hd... any longer but /dev/sdX instead." >&2
225 dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
226 Do you want to continue installing Debian using this frontend?" 0 0
233 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
235 grep -v 'Extended$' | \
236 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
238 if [ -z "$AVAILABLE_PARTITIONS" ] ; then
239 dialog --title "$PN" --trim \
240 --msgbox "Sorry, no partitions found. Please configure your
241 harddisks (see /proc/partitions) using a tool like fdisk,
242 cfdisk, gpart, gparted,..." 0 0
246 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
247 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet])"
250 TARGET=$(dialog --title "$PN" --single-quoted --stdout \
251 --menu "Please select the target partition:" 0 0 0 \
256 # ask for bootmanager {{{
257 prompt_for_bootmanager()
261 if echo "$TARGET" | grep -q "/dev/md" ; then
262 MBRPART="all disks of Software RAID $TARGET"
264 # figure out whole disk device
266 for device in /dev/disk/by-id/*
268 [ $(readlink -f $device) = ${TARGET} ] || continue
272 [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
273 if [ -e "$MBRDISK" ]; then
274 MBRDISK=$(readlink -f $MBRDISK)
276 # fall back to old behaviour
277 MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$//')
280 MBRPART="MBR of $MBRDISK"
283 for device in cciss/c0d0 sda hda; do
284 if [ /dev/$device != ${MBRDISK} ]; then
285 grep -q $device /proc/partitions && \
286 ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
289 ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
293 GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
294 --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
295 mbr "install bootmanager into $MBRPART" \
296 nowhere "do not install bootmanager at all" \
297 ${ADDITIONAL_PARAMS})
298 [ $? -eq 0 ] || bailout 3
303 # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
304 if echo "$TARGET" | grep -q "/dev/md" ; then
306 if [ -n "$SELECTED_PARTITIONS" ] ; then
307 GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
328 # ask for Debian release {{{
331 [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='squeeze'
332 RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
333 "Please enter the Debian release you would like to use for installation:" \
335 lenny Debian/old-stable \
336 squeeze Debian/stable \
337 wheezy Debian/testing \
338 sid Debian/unstable)"
342 # ask for hostname {{{
343 prompt_for_hostname()
345 HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
346 "Please enter the hostname you would like to use for installation:" \
351 # ask for password {{{
352 prompt_for_password()
356 while [ "$ROOTPW1" != "$ROOTPW2" ]; do
357 ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
358 "Please enter the password for the root account:" 10 60)
359 ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
360 "Please enter the password for the root account again for \
361 confirmation:" 10 60)
363 if [ "$ROOTPW1" != "$ROOTPW2" ]; then
364 $(dialog --stdout --title "${PN}" --ok-label \
365 "Retry" --msgbox "Passwords do not match!" 10 60)
368 ROOTPASSWORD="$ROOTPW1"
372 # ask for Debian mirror {{{
375 [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
377 CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
378 --menu "Where do you want to install from?" 0 0 0 \
379 net "install via network (downloading from mirror)" \
380 local "install from local directory/mirror"
383 if [ "$CHOOSE_MIRROR" = 'net' ] ; then
384 [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
385 MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
386 "Please enter Debian mirror you would like to use for installing packages." \
388 else # CHOOSE_MIRROR == local
389 [ -n "$ISO" ] || ISO='/mnt/mirror'
390 ISO="$(dialog --stdout --title "${PN}" --inputbox \
391 "Please enter directory name you would like to use for installing packages." \
397 # software raid setup {{{
398 config_swraid_setup()
402 # Currently we support only raid1:
405 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
406 # --menu "Which RAID level do you want to use?" 0 0 0 \
407 # raid1 "Software RAID level 1" \
408 # raid5 "Software RAID level 5")
409 #[ $? -eq 0 ] || bailout 20
411 MD_LIST=$(for i in $(seq 0 9) ; do
412 awk '{print $4}' /proc/partitions | grep -q md$i || \
413 echo "/dev/md$i /dev/md$i"
416 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
417 --menu "Which device do you want to use for ${RAIDLEVEL}?
419 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 \
421 [ $? -eq 0 ] || bailout 20
423 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
425 grep -v 'Extended$' | \
426 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
427 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
428 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
429 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
432 dialog --title "$PN" --separate-output \
433 --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
434 $PARTITION_LIST 2>$TMPFILE
436 SELECTED_PARTITIONS="$(cat $TMPFILE)"
439 for i in $(cat $TMPFILE) ; do
440 NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
443 # force metadata version 0.90 for lenny so old grub can boot from this array.
445 if [ $RELEASE = "lenny" ]; then
446 METADATA_VERSION="-e0"
451 # TODO: better error handling?
452 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
453 --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
457 if [ "$RC" = 0 ] ; then
458 dialog --title "$PN" --msgbox \
459 "Creating $TARGET was successful." 0 0
460 rm -f "$TMPFILE" "$ERRORFILE"
462 dialog --title "$PN" --msgbox \
463 "There was an error setting up $TARGET:
468 rm -f "$TMPFILE" "$ERRORFILE"
476 if dialog --stdout --title "$PN" \
477 --defaultno --yesno "Do you want to configure Software RAID?
479 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
485 # user should recheck his configuration {{{
486 # support full automatic installation:
488 dialog --timeout 10 --title "$PN" \
489 --yesno "Do you want to stop at this stage?
491 Notice: you are running ${PN} in non-interactive mode.
492 ${PN} will install Debian ${RELEASE} on ${TARGET}.
493 Last chance to quit. Timeout of 10 seconds running....
495 Do you want to stop now?" 0 0 2>/dev/null
498 # make sure the user is aware of the used configuration {{{
501 if [ -n "$AUTOINSTALL" ] ; then
502 if checkforrun ; then
503 eerror "Exiting as requested" ; eend 0
506 elif [ -n "$INTERACTIVE" ] ; then
508 INFOTEXT="Please recheck configuration before execution:
510 [ -n "$TARGET" ] && INFOTEXT="$INFOTEXT
512 [ -n "$GRUB" ] && INFOTEXT="$INFOTEXT
514 [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
515 Using release: $RELEASE"
516 [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
517 Using hostname $HOSTNAME"
518 [ -n "$MIRROR" ] && INFOTEXT="$INFOTEXT
519 Using mirror: $MIRROR"
520 [ -n "$ISO" ] && INFOTEXT="$INFOTEXT
525 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
527 dialog --title "$PN" --no-collapse \
528 --yesno "$INFOTEXT" 0 0
530 else # if not running automatic installation display configuration and prompt for execution:
531 einfo "$PN - Please recheck configuration before execution:"
533 echo " Target: $TARGET"
535 # do not display if MNTPOINT is the default one
536 case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo " Mount point: $MNTPOINT" ;; esac
538 if [ -n "$VIRTUAL" ] ; then
539 echo " Install grub: yes"
541 [ -n "$GRUB" ] && echo " Install grub: $GRUB" || echo " Install grub: no"
544 [ -n "$RELEASE" ] && echo " Using release: $RELEASE"
545 [ -n "$MIRROR" ] && echo " Using mirror: $MIRROR"
546 [ -n "$HOSTNAME" ] && echo " Using hostname: $HOSTNAME"
547 [ -n "$ISO" ] && echo " Using ISO: $ISO"
548 if [ -n "$VIRTUAL" ] ; then
549 echo " Deploying as Virtual Machine."
550 [ -n "$VMSIZE" ] && echo " Using Virtual Disk file with size of ${VMSIZE}."
554 echo " Important! Continuing will delete all data from ${TARGET}!"
556 if [ -n "$FORCE" ] ; then
557 einfo "Skip user acknowledgement as requested via --force option."
560 einfon "Is this ok for you? [y/N] "
562 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
563 eerror "Exiting as requested." ; eend 1
571 # interactive mode {{{
582 prompt_for_bootmanager
591 # run interactive mode if we didn't get the according configuration yet
592 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
593 # only target might be unset, so make sure the INTERACTIVE flag is set as well
601 # finally make sure at least $TARGET is set [the partition for the new system] {{{
602 if [ -n "$TARGET" ] ; then
603 SHORT_TARGET="${TARGET##*/}"
605 eerror "Please adjust $CONFFILES/config or..."
606 eerror "... use the interactive version for configuration before running ${0}" ; eend 1
612 if [ -z "$STAGES" ] ; then
613 STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
614 [ -d "$STAGES" ] || mkdir -p "$STAGES"
617 if [ -r "$STAGES"/grml-debootstrap ] ; then
618 if grep -q done $STAGES/grml-debootstrap ; then
619 eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
620 eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
625 # partition handling {{{
629 set_target_directory(){
630 # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
637 # make sure we normalise the path to an absolute directory name so something like:
638 # mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
640 TARGET="$(readlink -f $TARGET)"
643 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
650 # architecture setup {{{
651 if [ -n "$ARCH" ] ; then
652 ARCHCMD="--arch $ARCH"
653 ARCHINFO=" (${ARCH})"
655 ARCH="$(dpkg --print-architecture)"
656 ARCHCMD="--arch $ARCH"
657 ARCHINFO=" (${ARCH})"
663 if [ "$SECURE" = 'yes' ] ; then
664 if [ -e '/etc/apt/trusted.gpg' ] ; then
665 KEYRING="--keyring /etc/apt/trusted.gpg"
667 eerror "Could not find /etc/apt/trusted.gpg."
670 ewarn "Not checking Release signatures!"
674 # make sure we have the right syntax when using an iso image {{{
675 if [ -n "$ISO" ] ; then
688 # helper functions {{{
689 # we want to exit smoothly and clean:
691 # make sure $TARGET is not mounted when exiting grml-debootstrap
692 if [ -n "$MNTPOINT" ] ; then
693 if grep -q $MNTPOINT /proc/mounts ; then
694 # make sure nothing is left inside chroot so we can unmount it
695 [ -x "$MNTPOINT"/etc/init.d/ssh ] && "$MNTPOINT"/etc/init.d/ssh stop
696 [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
697 # ugly, but make sure we really don't leave anything (/proc /proc is intended)
698 for ARG in /sys /proc /proc ; do
699 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true
701 umount "$MNTPOINT"/dev >/dev/null 2>&1 || true
703 [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1 || true
705 if [ -n "$DIRECTORY" ] ; then
706 einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
708 einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
711 if [ -n "$STAGES" ] ; then
712 echo -n "Removing stages directory ${STAGES}: "
713 rm -rf "$STAGES" && echo done
716 # remove directory only if we used the default with process id inside the name
717 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
718 einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
723 if [ -n "${ORIG_TARGET}" ] ; then
724 einfo "Removing loopback mount of file ${ORIG_TARGET}."
725 kpartx -d "${ORIG_TARGET}" ; eend $?
728 [ -n "$1" ] && EXIT="$1" || EXIT="1"
729 [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
733 trap bailout HUP INT QUIT TERM
735 # we want to execute all the functions only once, simple check for it:
737 if [ -n "$2" ] ; then
738 echo "$2" > "${STAGES}/${1}"
740 elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
741 ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
743 ewarn "To reexecute it clean up the according directory inside $STAGES" ; eend 0
750 # create filesystem {{{
752 if [ -n "$DIRECTORY" ] ; then
753 einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
755 if grep -q "$TARGET" /proc/mounts ; then
756 eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
760 if [ -n "$MKFS" ] ; then
761 einfo "Running $MKFS on $TARGET"
762 $MKFS $TARGET ; RC=$?
764 # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
765 # will fail to detect the uuid in the chroot
766 if echo "$TARGET" | grep -q "/dev/md" ; then
767 blockdev --rereadpt "${TARGET}"
768 elif ! [ -n "$VIRTUAL" ] ; then
769 blockdev --rereadpt "${TARGET%%[0-9]*}"
771 # give the system 2 seconds, otherwise we might run into
772 # race conditions :-/
775 eval $(blkid -o udev $TARGET 2>/dev/null)
776 [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
785 # modify filesystem settings {{{
787 if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
788 einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
795 # mount the new partition or if it's a directory do nothing at all {{{
797 if [ -n "$DIRECTORY" ] ; then
798 einfo "Running grml-debootstrap on a directory, nothing to mount."
800 if grep -q $TARGET /proc/mounts ; then
801 ewarn "$TARGET already mounted, continuing anyway." ; eend 0
803 if ! [ -d "${MNTPOINT}" ] ; then
804 [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
806 einfo "Mounting $TARGET to $MNTPOINT"
808 mount -o rw,suid,dev $TARGET $MNTPOINT
812 if [ -n "$ISODIR" ] ; then
813 einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
814 mkdir -p "$MNTPOINT/$ISODIR"
815 mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
821 # prepare VM image for usage with debootstrap {{{
823 if [ -z "$VIRTUAL" ] ; then
824 return 0 # be quite by intention
827 if [ -b "$TARGET" ] ; then
828 eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
833 ORIG_TARGET="$TARGET" # store for later reuse
835 qemu-img create -f raw "${TARGET}" "${VMSIZE}"
836 echo 4 66 | mksh /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
837 dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
838 parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
840 DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
841 if [ -z "${DEVINFO}" ] ; then
842 echo Error setting up loopback device >&2
846 # hopefully this always works as expected
847 LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
848 BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/') # '253:0'
849 LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
850 export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
852 blockdev --rereadpt "${LOOP}"
854 if [ -z "$TARGET" ] ; then
855 echo "Error: target could not be set to according /dev/mapper/* device." >&2
861 # make VM image bootable and unmount it {{{
863 if [ -z "${VIRTUAL}" ] ; then
867 if ! mount "${TARGET}" "${MNTPOINT}" ; then
868 eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
872 einfo "Installing Grub as bootloader."
873 mount -t proc none "${MNTPOINT}"/proc
874 mount -t sysfs none "${MNTPOINT}"/sys
875 mount --bind /dev "${MNTPOINT}"/dev
877 mkdir -p "${MNTPOINT}/boot/grub"
878 if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
879 eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
883 cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
884 chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
885 dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
886 rm -f "${MNTPOINT}/tmp/core.img"
888 einfo "Updating grub configuration file."
889 chroot "${MNTPOINT}" update-grub
891 umount "${MNTPOINT}"/proc
892 umount "${MNTPOINT}"/sys
893 umount "${MNTPOINT}"/dev
895 einfo "Adjusting grub.cfg for successful boot sequence."
896 # ugly but needed to boot grub acordingly
897 sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
898 sed -i "s; root=/dev/.*; root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
901 kpartx -d "${ORIG_TARGET}" >/dev/null
905 # install main chroot {{{
906 debootstrap_system() {
907 if [ "$_opt_nodebootstrap" ]; then
908 einfo "Skipping debootstrap as requested."
912 if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
913 einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
914 if [ -n "$MIRROR" ] ; then
915 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
916 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
918 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
919 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
923 eerror "Error: $MNTPOINT not mounted, can not continue."
929 # prepare chroot via chroot-script {{{
931 einfo "Preparing chroot system"
933 # provide variables to chroot system
934 CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
935 touch $CHROOT_VARIABLES
936 chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
937 echo "# Configuration of ${PN}" > $CHROOT_VARIABLES
938 [ -n "$ARCH" ] && echo "ARCH=$ARCH" >> $CHROOT_VARIABLES
939 [ -n "$GRUB" ] && echo "GRUB=$GRUB" >> $CHROOT_VARIABLES
940 [ -n "$HOSTNAME" ] && echo "HOSTNAME=$HOSTNAME" >> $CHROOT_VARIABLES
941 [ -n "$INSTALL_NOTES" ] && echo "INSTALL_NOTES=$INSTALL_NOTES" >> $CHROOT_VARIABLES
942 [ -n "$ISODIR" ] && echo "ISODIR=$ISO" >> $CHROOT_VARIABLES
943 [ -n "$ISO" ] && echo "ISO=$ISO" >> $CHROOT_VARIABLES
944 [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=$KEEP_SRC_LIST" >> $CHROOT_VARIABLES
945 [ -n "$MIRROR" ] && echo "MIRROR=$MIRROR" >> $CHROOT_VARIABLES
946 [ -n "$PACKAGES" ] && echo "PACKAGES=$PACKAGES" >> $CHROOT_VARIABLES
947 [ -n "$RM_APTCACHE" ] && echo "RM_APTCACHE=$RM_APTCACHE" >> $CHROOT_VARIABLES
948 [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
949 [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=$SELECTED_PARTITIONS" >> $CHROOT_VARIABLES
950 [ -n "$TARGET" ] && echo "TARGET=$TARGET" >> $CHROOT_VARIABLES
951 [ -n "$TARGET_UUID" ] && echo "TARGET_UUID=$TARGET_UUID" >> $CHROOT_VARIABLES
953 cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
954 chmod 755 $MNTPOINT/bin/chroot-script
955 [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
957 # make sure we have our files for later use via chroot-script
958 cp $VERBOSE $CONFFILES/config $MNTPOINT/etc/debootstrap/
959 # make sure we adjust the configuration variables accordingly:
960 sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
961 sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#" $MNTPOINT/etc/debootstrap/config
962 sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#" $MNTPOINT/etc/debootstrap/config
965 if [ -n "$INSTALL_NOTES" ] ; then
966 [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
970 cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
971 $MNTPOINT/etc/debootstrap/packages
973 # debconf preseeding:
974 _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
975 [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
976 cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
978 # copy scripts that should be executed inside the chroot:
979 _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
980 [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
981 mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
982 cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
985 # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
986 cp $VERBOSE $CHROOT_VARIABLES $MNTPOINT/etc/debootstrap/variables
988 cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
990 # make sure we can access network [relevant for cdebootstrap]
991 [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
993 # provide system's /etc/hosts to the target:
994 if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
995 cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
998 # setup default locales
999 [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen $MNTPOINT/etc/locale.gen
1001 # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
1002 ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
1004 # copy any existing files to chroot
1005 [ -d $CONFFILES/bin ] && cp $VERBOSE -a -L $CONFFILES/bin/* $MNTPOINT/bin/
1006 [ -d $CONFFILES/boot ] && cp $VERBOSE -a -L $CONFFILES/boot/* $MNTPOINT/boot/
1007 [ -d $CONFFILES/etc ] && cp $VERBOSE -a -L $CONFFILES/etc/* $MNTPOINT/etc/
1008 [ -d $CONFFILES/sbin ] && cp $VERBOSE -a -L $CONFFILES/sbin/* $MNTPOINT/sbin/
1009 [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
1010 [ -d $CONFFILES/usr ] && cp $VERBOSE -a -L $CONFFILES/usr/* $MNTPOINT/usr/
1011 [ -d $CONFFILES/var ] && cp $VERBOSE -a -L $CONFFILES/var/* $MNTPOINT/var/
1013 # copy local network setup to chroot
1014 if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
1015 [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
1016 cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
1019 # install config file providing some example entries
1020 if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1021 cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1028 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1029 execute_pre_scripts() {
1030 # make sure we have $MNTPOINT available for our scripts
1032 if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1033 [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1034 for script in ${pre_scripts}/* ; do
1035 if [ -x "$script" ] ; then
1036 einfo "Executing pre-script $script"
1044 # execute all scripts in /etc/debootstrap/scripts/ {{{
1046 # make sure we have $MNTPOINT available for our scripts
1048 if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1049 [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1050 for script in ${scripts}/* ; do
1051 if [ -x "$script" ] ; then
1052 einfo "Executing script $script"
1060 # execute chroot-script {{{
1062 if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1066 if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1067 einfo "Executing chroot-script now"
1068 mount --bind /dev "$MNTPOINT"/dev
1069 chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1070 umount "$MNTPOINT"/dev
1073 eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1079 # unmount $MNTPOINT {{{
1082 # display installation notes:
1083 if [ -n "$INSTALL_NOTES" ] ; then
1084 [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1087 if [ -n "$ISODIR" ] ; then
1088 if grep -q "$ISODIR" /proc/mounts ; then
1089 einfo "Unmount $MNTPOINT/$ISODIR"
1090 umount "$MNTPOINT/$ISODIR"
1095 if grep -q "$MNTPOINT" /proc/mounts ; then
1096 if [ -n "$PARTITION" ] ; then
1097 einfo "Unmount $MNTPOINT"
1105 # execute filesystem check {{{
1107 if [ "$FSCK" = 'yes' ] ; then
1108 [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1109 einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1116 # now execute all the functions {{{
1117 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1118 preparechroot execute_pre_scripts chrootscript execute_scripts \
1119 umount_chroot finalize_vm fscktool ; do
1120 if stage "${i}" ; then
1121 $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
1127 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
1128 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
1130 # Remove temporary mountpoint again
1131 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
1132 einfo "Removing directory ${MNTPOINT}" ; rmdir "$MNTPOINT" ; eend $?
1136 # end dialog of autoinstallation {{{
1137 if [ -n "$AUTOINSTALL" ] ; then
1138 if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1139 Automatically rebooting in 10 seconds.
1141 Choose Cancel to skip rebooting." 10 60 10 ; then
1142 noeject noprompt reboot
1145 einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1149 ## END OF FILE #################################################################
1150 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=3