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.$$"
21 # inside the chroot system locales might not be available, so use minimum:
25 # make sure interactive mode is only executed when
26 # using an empty configuration file or option --interactive
30 # source core functions {{{
31 . /etc/grml/lsb-functions
32 . /etc/grml/script-functions
37 echo "$PN - wrapper around debootstrap for installing Debian
43 -m, --mirror <URL> Mirror which should be used for apt-get/aptitude.
44 -i, --iso <mnt> Mountpoint where a Debian ISO is mounted to, for use
45 instead of fetching packages from a mirror.
46 -r, --release <name> Release of new Debian system (default: squeeze).
47 -t, --target <target> Target partition (/dev/...) or directory where the
48 system should be installed to.
49 -p, --mntpoint <mnt> Mountpoint used for mounting the target system,
50 has no effect if -t is given and represents a directory.
51 --debopt <params> Extra parameters passed to the debootstrap command.
52 --interactive Use interactive mode (frontend).
53 --nodebootstrap Skip debootstrap, only do configuration to the target.
54 --grub <device> Target for grub installation. Usage example: /dev/sda
55 --arch <arch> Architecture to use. Currently only i386 is supported.
56 --filesystem <fs> Filesystem that should be used when target is a partition
57 or Virtual Machine (see --vmfile).
58 --insecure Do not download and check Release file signatures.
60 Options for Virtual Machine deployment:
62 --vmfile Set up a Virtual Machine instead of installing to
63 a partition or directory, to be combined with --target,
64 like: --vmfile --target /mnt/sda1/qemu.img
65 --vmsize <size> Use specified size for size of VM file (default: 2G).
66 Syntax as supported by qemu-img, like: --vmsize 3G
68 Configuration options:
70 -c, --config <file> Use specified configuration file, defaults to
71 /etc/debootstrap/config
72 -d, --confdir <path> Place of config files for debootstrap, defaults
74 --packages <file> Install packages defined in specified list file.
75 --nopackages Skip installation of packages defined in
76 /etc/debootstrap/packages
77 --debconf <file> Pre-seed packages using specified pre-seed db file.
78 --keep_src_list Do not overwrite user provided apt sources.list.
79 --hostname <name> Hostname of Debian system.
80 --password <pwd> Use specified password as password for user root.
81 --bootappend <line> Add specified appendline to kernel whilst booting.
82 --chroot-scripts <d> Execute chroot scripts from specified directory.
83 --pre-scripts <dir> Execute scripts from specified directory (before chroot-scripts).
84 --scripts <dir> Execute scripts from specified directory (after chroot-scripts).
88 -v, --verbose Increase verbosity.
89 -h, --help Print this usage information and exit.
90 -V, --version Show summary of options and exit.
92 Usage examples can be found in the grml-debootstrap manpage.
93 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
97 if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
99 echo 'Please notice that this script requires root permissions!'
104 # make sure we have what we need {{{
105 check4progs debootstrap dialog || exit 1
108 # source main configuration file {{{
109 if [ -r /etc/debootstrap/config ] ; then
110 . /etc/debootstrap/config
114 # cmdline handling {{{
115 # source external command line parameter-processing script
116 if [ -r ./cmdlineopts.clp ] ; then
118 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
119 . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
121 echo "Error: cmdline function file not found, exiting.">&2
125 # == business-logic of command line parameter-processing
127 # source configuration file in <confdir> if supplied. {{{
128 [ "$_opt_confdir" ] && {
129 CONFFILES=$_opt_confdir
130 einfo "Using config files under $CONFFILES/."
131 if ! [ -r "$CONFFILES/config" ] ; then
132 eerror "Error: config file $CONFFILES/config not found."; eend 1; exit 1
134 if ! . "$CONFFILES/config" ; then
135 eerror "Error reading config file $CONFFILES/config" ; eend 1 ; exit 1
137 # restore the command line parameter value
138 CONFFILES=$_opt_confdir
142 [ "$_opt_mirror" ] && MIRROR=$_opt_mirror
143 [ "$_opt_iso" ] && ISO=$_opt_iso
144 [ "$_opt_release" ] && RELEASE=$_opt_release
145 [ "$_opt_target" ] && TARGET=$_opt_target
146 [ "$_opt_vmfile" ] && VIRTUAL=1
147 [ "$_opt_vmsize" ] && VMSIZE=$_opt_vmsize
148 [ "$_opt_mntpoint" ] && MNTPOINT=$_opt_mntpoint
149 [ "$_opt_debopt" ] && DEBOOTSTRAP_OPT=$_opt_debopt
150 [ "$_opt_interactive" ] && INTERACTIVE=1
151 [ "$_opt_config" ] && CONFIGFILE=$_opt_config
152 [ "$_opt_filesystem" ] && MKFS="mkfs.$_opt_filesystem"
153 [ "$_opt_packages_set" ] && PACKAGES='yes'
154 [ "$_opt_nopackages" ] && PACKAGES=''
155 [ "$_opt_debconf_set" ] && DEBCONF='yes'
156 [ "$_opt_scripts_set" ] && SCRIPTS='yes'
157 [ "$_opt_pre_scripts_set" ] && PRE_SCRIPTS='yes'
158 [ "$_opt_chroot_scripts_set" ] && CHROOT_SCRIPTS='yes'
159 [ "$_opt_keep_src_list" ] && KEEP_SRC_LIST='yes'
160 [ "$_opt_hostname" ] && HOSTNAME=$_opt_hostname
161 [ "$_opt_password" ] && ROOTPASSWORD=$_opt_password
162 [ "$_opt_bootappend" ] && BOOT_APPEND=$_opt_bootappend
163 [ "$_opt_grub" ] && GRUB=$_opt_grub
164 [ "$_opt_arch" ] && ARCH=$_opt_arch
165 [ "$_opt_insecure" ] && SECURE='false'
166 [ "$_opt_verbose" ] && VERBOSE="-v"
168 [ "$_opt_help" ] && {
174 [ "$_opt_version" ] && {
175 einfo "$PN - version $VERSION"
176 einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
182 # check for root permissions {{{
183 if ! check4root ; then
184 echo "For usage instructions please execute '$PN --help'."
189 # make sure we have what we need {{{
190 if [ -n "$VIRTUAL" ] ; then
191 check4progs kpartx mksh qemu-img || exit 1
195 # source specified configuration file {{{
196 if [ -n "$CONFIGFILE" ] ; then
197 einfo "Reading specified config file $CONFIGFILE."
198 if ! . "$CONFIGFILE" ; then
199 eerror "Error reading config file $CONFIGFILE" ; eend 1 ; exit 1
204 # backwards compability checks {{{
205 if [ -n "$GROOT" ] ; then
206 echo "Error: you seem to have \$GROOT configured." >&2
207 echo "This variable is no longer supported, please visit the" >&2
208 echo "grml-debootstrap documentation for details." >&2
212 if echo "$GRUB" | grep -q '^hd' ; then
213 echo "Error: this syntax for the grub configuration variable is no longer supported." >&2
214 echo "Please do not use hd... any longer but /dev/sdX instead." >&2
222 dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
223 Do you want to continue installing Debian using this frontend?" 0 0
230 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
232 grep -v 'Extended$' | \
233 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
235 if [ -z "$AVAILABLE_PARTITIONS" ] ; then
236 dialog --title "$PN" --trim \
237 --msgbox "Sorry, no partitions found. Please configure your
238 harddisks (see /proc/partitions) using a tool like fdisk,
239 cfdisk, gpart, gparted,..." 0 0
243 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
244 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet])"
247 TARGET=$(dialog --title "$PN" --single-quoted --stdout \
248 --menu "Please select the target partition:" 0 0 0 \
253 # ask for bootmanager {{{
254 prompt_for_bootmanager()
258 if echo "$TARGET" | grep -q "/dev/md" ; then
259 MBRPART="all disks of Software RAID $TARGET"
261 # figure out whole disk device
263 for device in /dev/disk/by-id/*
265 [ $(readlink -f $device) = ${TARGET} ] || continue
269 [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
270 if [ -e "$MBRDISK" ]; then
271 MBRDISK=$(readlink -f $MBRDISK)
273 # fall back to old behaviour
274 MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$//')
277 MBRPART="MBR of $MBRDISK"
280 for device in cciss/c0d0 sda hda; do
281 if [ /dev/$device != ${MBRDISK} ]; then
282 grep -q $device /proc/partitions && \
283 ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
286 ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
290 GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
291 --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
292 mbr "install bootmanager into $MBRPART" \
293 nowhere "do not install bootmanager at all" \
294 ${ADDITIONAL_PARAMS})
295 [ $? -eq 0 ] || bailout 3
300 # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
301 if echo "$TARGET" | grep -q "/dev/md" ; then
303 if [ -n "$SELECTED_PARTITIONS" ] ; then
304 GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
325 # ask for Debian release {{{
328 [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='squeeze'
329 RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
330 "Please enter the Debian release you would like to use for installation:" \
332 lenny Debian/old-stable \
333 squeeze Debian/stable \
334 wheezy Debian/testing \
335 sid Debian/unstable)"
339 # ask for hostname {{{
340 prompt_for_hostname()
342 HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
343 "Please enter the hostname you would like to use for installation:" \
348 # ask for password {{{
349 prompt_for_password()
353 while [ "$ROOTPW1" != "$ROOTPW2" ]; do
354 ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
355 "Please enter the password for the root account:" 10 60)
356 ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
357 "Please enter the password for the root account again for \
358 confirmation:" 10 60)
360 if [ "$ROOTPW1" != "$ROOTPW2" ]; then
361 $(dialog --stdout --title "${PN}" --ok-label \
362 "Retry" --msgbox "Passwords do not match!" 10 60)
365 ROOTPASSWORD="$ROOTPW1"
369 # ask for Debian mirror {{{
372 [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
374 CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
375 --menu "Where do you want to install from?" 0 0 0 \
376 net "install via network (downloading from mirror)" \
377 local "install from local directory/mirror"
380 if [ "$CHOOSE_MIRROR" = 'net' ] ; then
381 [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
382 MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
383 "Please enter Debian mirror you would like to use for installing packages." \
385 else # CHOOSE_MIRROR == local
386 [ -n "$ISO" ] || ISO='/mnt/mirror'
387 ISO="$(dialog --stdout --title "${PN}" --inputbox \
388 "Please enter directory name you would like to use for installing packages." \
394 # software raid setup {{{
395 config_swraid_setup()
399 # Currently we support only raid1:
402 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
403 # --menu "Which RAID level do you want to use?" 0 0 0 \
404 # raid1 "Software RAID level 1" \
405 # raid5 "Software RAID level 5")
406 #[ $? -eq 0 ] || bailout 20
408 MD_LIST=$(for i in $(seq 0 9) ; do
409 awk '{print $4}' /proc/partitions | grep -q md$i || \
410 echo "/dev/md$i /dev/md$i"
413 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
414 --menu "Which device do you want to use for ${RAIDLEVEL}?
416 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 \
418 [ $? -eq 0 ] || bailout 20
420 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
422 grep -v 'Extended$' | \
423 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
424 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
425 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
426 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
429 dialog --title "$PN" --separate-output \
430 --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
431 $PARTITION_LIST 2>$TMPFILE
433 SELECTED_PARTITIONS="$(cat $TMPFILE)"
436 for i in $(cat $TMPFILE) ; do
437 NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
440 # force metadata version 0.90 for lenny so old grub can boot from this array.
442 if [ $RELEASE = "lenny" ]; then
443 METADATA_VERSION="-e0"
448 # TODO: better error handling?
449 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
450 --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
454 if [ "$RC" = 0 ] ; then
455 dialog --title "$PN" --msgbox \
456 "Creating $TARGET was successful." 0 0
457 rm -f "$TMPFILE" "$ERRORFILE"
459 dialog --title "$PN" --msgbox \
460 "There was an error setting up $TARGET:
465 rm -f "$TMPFILE" "$ERRORFILE"
473 if dialog --stdout --title "$PN" \
474 --defaultno --yesno "Do you want to configure Software RAID?
476 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
482 # user should recheck his configuration {{{
483 # support full automatic installation:
485 dialog --timeout 10 --title "$PN" \
486 --yesno "Do you want to stop at this stage?
488 Notice: you are running ${PN} in non-interactive mode.
489 ${PN} will install Debian ${RELEASE} on ${TARGET}.
490 Last chance to quit. Timeout of 10 seconds running....
492 Do you want to stop now?" 0 0 2>/dev/null
495 # make sure the user is aware of the used configuration {{{
498 if [ -n "$AUTOINSTALL" ] ; then
499 if checkforrun ; then
500 eerror "Exiting as requested" ; eend 0
503 elif [ -n "$INTERACTIVE" ] ; then
505 INFOTEXT="Please recheck configuration before execution:
507 [ -n "$TARGET" ] && INFOTEXT="$INFOTEXT
509 [ -n "$GRUB" ] && INFOTEXT="$INFOTEXT
511 [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
512 Using release: $RELEASE"
513 [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
514 Using hostname $HOSTNAME"
515 [ -n "$MIRROR" ] && INFOTEXT="$INFOTEXT
516 Using mirror: $MIRROR"
517 [ -n "$ISO" ] && INFOTEXT="$INFOTEXT
522 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
524 dialog --title "$PN" --no-collapse \
525 --yesno "$INFOTEXT" 0 0
527 else # if not running automatic installation display configuration and prompt for execution:
528 einfo "$PN - Please recheck configuration before execution:"
530 echo " Target: $TARGET"
532 # do not display if MNTPOINT is the default one
533 case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo " Mount point: $MNTPOINT" ;; esac
535 if [ -n "$VIRTUAL" ] ; then
536 echo " Install grub: yes"
538 [ -n "$GRUB" ] && echo " Install grub: $GRUB" || echo " Install grub: no"
541 [ -n "$RELEASE" ] && echo " Using release: $RELEASE"
542 [ -n "$MIRROR" ] && echo " Using mirror: $MIRROR"
543 [ -n "$HOSTNAME" ] && echo " Using hostname: $HOSTNAME"
544 [ -n "$ISO" ] && echo " Using ISO: $ISO"
545 if [ -n "$VIRTUAL" ] ; then
546 echo " Deploying as Virtual Machine."
547 [ -n "$VMSIZE" ] && echo " Using Virtual Disk file with size of ${VMSIZE}."
551 echo " Important! Continuing will delete all data from ${TARGET}!"
554 einfon "Is this ok for you? [y/N] "
556 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
557 eerror "Exiting as requested." ; eend 1
564 # interactive mode {{{
575 prompt_for_bootmanager
584 # run interactive mode if we didn't get the according configuration yet
585 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
586 # only target might be unset, so make sure the INTERACTIVE flag is set as well
594 # finally make sure at least $TARGET is set [the partition for the new system] {{{
595 if [ -n "$TARGET" ] ; then
596 SHORT_TARGET="${TARGET##*/}"
598 eerror "Please adjust $CONFFILES/config or..."
599 eerror "... use the interactive version for configuration before running ${0}" ; eend 1
605 if [ -z "$STAGES" ] ; then
606 STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
607 [ -d "$STAGES" ] || mkdir -p "$STAGES"
610 if [ -r "$STAGES"/grml-debootstrap ] ; then
611 if grep -q done $STAGES/grml-debootstrap ; then
612 eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
613 eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
618 # partition handling {{{
622 set_target_directory(){
623 # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
630 # make sure we normalise the path to an absolute directory name so something like:
631 # mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
633 TARGET="$(readlink -f $TARGET)"
636 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
643 # architecture setup {{{
644 if [ -n "$ARCH" ] ; then
645 ARCHCMD="--arch $ARCH"
646 ARCHINFO=" (${ARCH})"
648 ARCH="$(dpkg --print-architecture)"
649 ARCHCMD="--arch $ARCH"
650 ARCHINFO=" (${ARCH})"
656 if [ "$SECURE" = 'yes' ] ; then
657 if [ -e '/etc/apt/trusted.gpg' ] ; then
658 KEYRING="--keyring /etc/apt/trusted.gpg"
660 eerror "Could not find /etc/apt/trusted.gpg."
663 ewarn "Not checking Release signatures!"
667 # make sure we have the right syntax when using an iso image {{{
668 if [ -n "$ISO" ] ; then
681 # helper functions {{{
682 # we want to exit smoothly and clean:
684 # make sure $TARGET is not mounted when exiting grml-debootstrap
685 if [ -n "$MNTPOINT" ] ; then
686 if grep -q $MNTPOINT /proc/mounts ; then
687 # make sure nothing is left inside chroot so we can unmount it
688 [ -x "$MNTPOINT"/etc/init.d/ssh ] && "$MNTPOINT"/etc/init.d/ssh stop
689 [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
690 # ugly, but make sure we really don't leave anything (/proc /proc is intended)
691 for ARG in /sys /proc /proc ; do
692 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true
694 umount "$MNTPOINT"/dev >/dev/null 2>&1 || true
696 [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1 || true
698 if [ -n "$DIRECTORY" ] ; then
699 einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
701 einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
704 if [ -n "$STAGES" ] ; then
705 echo -n "Removing stages directory ${STAGES}: "
706 rm -rf "$STAGES" && echo done
709 # remove directory only if we used the default with process id inside the name
710 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
711 einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
716 if [ -n "${ORIG_TARGET}" ] ; then
717 einfo "Removing loopback mount of file ${ORIG_TARGET}."
718 kpartx -d "${ORIG_TARGET}" ; eend $?
721 [ -n "$1" ] && EXIT="$1" || EXIT="1"
722 [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
726 trap bailout HUP INT QUIT TERM
728 # we want to execute all the functions only once, simple check for it:
730 if [ -n "$2" ] ; then
731 echo "$2" > "${STAGES}/${1}"
733 elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
734 ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
736 ewarn "To reexecute it clean up the according directory inside $STAGES" ; eend 0
743 # create filesystem {{{
745 if [ -n "$DIRECTORY" ] ; then
746 einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
748 if grep -q "$TARGET" /proc/mounts ; then
749 eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
753 if [ -n "$MKFS" ] ; then
754 einfo "Running $MKFS on $TARGET"
755 $MKFS $TARGET ; RC=$?
757 # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
758 # will fail to detect the uuid in the chroot
759 if echo "$TARGET" | grep -q "/dev/md" ; then
760 blockdev --rereadpt "${TARGET}"
761 elif ! [ -n "$VIRTUAL" ] ; then
762 blockdev --rereadpt "${TARGET%%[0-9]*}"
764 # give the system 2 seconds, otherwise we might run into
765 # race conditions :-/
768 eval $(blkid -o udev $TARGET 2>/dev/null)
769 [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
778 # modify filesystem settings {{{
780 if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
781 einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
788 # mount the new partition or if it's a directory do nothing at all {{{
790 if [ -n "$DIRECTORY" ] ; then
791 einfo "Running grml-debootstrap on a directory, nothing to mount."
793 if grep -q $TARGET /proc/mounts ; then
794 ewarn "$TARGET already mounted, continuing anyway." ; eend 0
796 if ! [ -d "${MNTPOINT}" ] ; then
797 [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
799 einfo "Mounting $TARGET to $MNTPOINT"
801 mount -o rw,suid,dev $TARGET $MNTPOINT
805 if [ -n "$ISODIR" ] ; then
806 einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
807 mkdir -p "$MNTPOINT/$ISODIR"
808 mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
814 # prepare VM image for usage with debootstrap {{{
816 if [ -z "$VIRTUAL" ] ; then
817 return 0 # be quite by intention
820 if [ -b "$TARGET" ] ; then
821 eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
826 ORIG_TARGET="$TARGET" # store for later reuse
828 qemu-img create -f raw "${TARGET}" "${VMSIZE}"
829 echo 4 66 | mksh /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
830 dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
831 parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
833 DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
834 if [ -z "${DEVINFO}" ] ; then
835 echo Error setting up loopback device >&2
839 # hopefully this always works as expected
840 LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
841 BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/') # '253:0'
842 LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
843 export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
845 blockdev --rereadpt "${LOOP}"
847 if [ -z "$TARGET" ] ; then
848 echo "Error: target could not be set to according /dev/mapper/* device." >&2
854 # make VM image bootable and unmount it {{{
856 if [ -z "${VIRTUAL}" ] ; then
860 if ! mount "${TARGET}" "${MNTPOINT}" ; then
861 eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
865 einfo "Installing Grub as bootloader."
866 mount -t proc none "${MNTPOINT}"/proc
867 mount -t sysfs none "${MNTPOINT}"/sys
868 mount --bind /dev "${MNTPOINT}"/dev
870 mkdir -p "${MNTPOINT}/boot/grub"
871 if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
872 eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
876 cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
877 chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
878 dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
879 rm -f "${MNTPOINT}/tmp/core.img"
881 einfo "Updating grub configuration file."
882 chroot "${MNTPOINT}" update-grub
884 umount "${MNTPOINT}"/proc
885 umount "${MNTPOINT}"/sys
886 umount "${MNTPOINT}"/dev
888 einfo "Adjusting grub.cfg for successful boot sequence."
889 # ugly but needed to boot grub acordingly
890 sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
891 sed -i "s; root=/dev/.*; root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
894 kpartx -d "${ORIG_TARGET}" >/dev/null
898 # install main chroot {{{
899 debootstrap_system() {
900 if [ "$_opt_nodebootstrap" ]; then
901 einfo "Skipping debootstrap as requested."
905 if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
906 einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
907 if [ -n "$MIRROR" ] ; then
908 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
909 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
911 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
912 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
916 eerror "Error: $MNTPOINT not mounted, can not continue."
922 # prepare chroot via chroot-script {{{
924 einfo "Preparing chroot system"
926 # provide variables to chroot system
927 CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
928 touch $CHROOT_VARIABLES
929 chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
930 echo "# Configuration of ${PN}" > $CHROOT_VARIABLES
931 [ -n "$ARCH" ] && echo "ARCH=$ARCH" >> $CHROOT_VARIABLES
932 [ -n "$GRUB" ] && echo "GRUB=$GRUB" >> $CHROOT_VARIABLES
933 [ -n "$HOSTNAME" ] && echo "HOSTNAME=$HOSTNAME" >> $CHROOT_VARIABLES
934 [ -n "$INSTALL_NOTES" ] && echo "INSTALL_NOTES=$INSTALL_NOTES" >> $CHROOT_VARIABLES
935 [ -n "$ISODIR" ] && echo "ISODIR=$ISO" >> $CHROOT_VARIABLES
936 [ -n "$ISO" ] && echo "ISO=$ISO" >> $CHROOT_VARIABLES
937 [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=$KEEP_SRC_LIST" >> $CHROOT_VARIABLES
938 [ -n "$MIRROR" ] && echo "MIRROR=$MIRROR" >> $CHROOT_VARIABLES
939 [ -n "$PACKAGES" ] && echo "PACKAGES=$PACKAGES" >> $CHROOT_VARIABLES
940 [ -n "$RM_APTCACHE" ] && echo "RM_APTCACHE=$RM_APTCACHE" >> $CHROOT_VARIABLES
941 [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
942 [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=$SELECTED_PARTITIONS" >> $CHROOT_VARIABLES
943 [ -n "$TARGET" ] && echo "TARGET=$TARGET" >> $CHROOT_VARIABLES
944 [ -n "$TARGET_UUID" ] && echo "TARGET_UUID=$TARGET_UUID" >> $CHROOT_VARIABLES
946 cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
947 chmod 755 $MNTPOINT/bin/chroot-script
948 [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
950 # make sure we have our files for later use via chroot-script
951 cp $VERBOSE $CONFFILES/config $MNTPOINT/etc/debootstrap/
952 # make sure we adjust the configuration variables accordingly:
953 sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
954 sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#" $MNTPOINT/etc/debootstrap/config
955 sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#" $MNTPOINT/etc/debootstrap/config
958 if [ -n "$INSTALL_NOTES" ] ; then
959 [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
963 cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
964 $MNTPOINT/etc/debootstrap/packages
966 # debconf preseeding:
967 _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
968 [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
969 cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
971 # copy scripts that should be executed inside the chroot:
972 _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
973 [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
974 mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
975 cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
978 # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
979 cp $VERBOSE $CHROOT_VARIABLES $MNTPOINT/etc/debootstrap/variables
981 cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
983 # make sure we can access network [relevant for cdebootstrap]
984 [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
986 # provide system's /etc/hosts to the target:
987 if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
988 cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
991 # setup default locales
992 [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen $MNTPOINT/etc/locale.gen
994 # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
995 ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
997 # copy any existing files to chroot
998 [ -d $CONFFILES/bin ] && cp $VERBOSE -a -L $CONFFILES/bin/* $MNTPOINT/bin/
999 [ -d $CONFFILES/boot ] && cp $VERBOSE -a -L $CONFFILES/boot/* $MNTPOINT/boot/
1000 [ -d $CONFFILES/etc ] && cp $VERBOSE -a -L $CONFFILES/etc/* $MNTPOINT/etc/
1001 [ -d $CONFFILES/sbin ] && cp $VERBOSE -a -L $CONFFILES/sbin/* $MNTPOINT/sbin/
1002 [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
1003 [ -d $CONFFILES/usr ] && cp $VERBOSE -a -L $CONFFILES/usr/* $MNTPOINT/usr/
1004 [ -d $CONFFILES/var ] && cp $VERBOSE -a -L $CONFFILES/var/* $MNTPOINT/var/
1006 # copy local network setup to chroot
1007 if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
1008 [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
1009 cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
1012 # install config file providing some example entries
1013 if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1014 cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1021 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1022 execute_pre_scripts() {
1023 # make sure we have $MNTPOINT available for our scripts
1025 if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1026 [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1027 for script in ${pre_scripts}/* ; do
1028 if [ -x "$script" ] ; then
1029 einfo "Executing pre-script $script"
1037 # execute all scripts in /etc/debootstrap/scripts/ {{{
1039 # make sure we have $MNTPOINT available for our scripts
1041 if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1042 [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1043 for script in ${scripts}/* ; do
1044 if [ -x "$script" ] ; then
1045 einfo "Executing script $script"
1053 # execute chroot-script {{{
1055 if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1059 if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1060 einfo "Executing chroot-script now"
1061 mount --bind /dev "$MNTPOINT"/dev
1062 chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1063 umount "$MNTPOINT"/dev
1066 eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1072 # unmount $MNTPOINT {{{
1075 # display installation notes:
1076 if [ -n "$INSTALL_NOTES" ] ; then
1077 [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1080 if [ -n "$ISODIR" ] ; then
1081 if grep -q "$ISODIR" /proc/mounts ; then
1082 einfo "Unmount $MNTPOINT/$ISODIR"
1083 umount "$MNTPOINT/$ISODIR"
1088 if grep -q "$MNTPOINT" /proc/mounts ; then
1089 if [ -n "$PARTITION" ] ; then
1090 einfo "Unmount $MNTPOINT"
1098 # execute filesystem check {{{
1100 if [ "$FSCK" = 'yes' ] ; then
1101 [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1102 einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1109 # now execute all the functions {{{
1110 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1111 preparechroot execute_pre_scripts chrootscript execute_scripts \
1112 umount_chroot finalize_vm fscktool ; do
1113 if stage "${i}" ; then
1114 $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
1120 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
1121 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
1123 # Remove temporary mountpoint again
1124 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
1125 einfo "Removing directory ${MNTPOINT}" ; rmdir "$MNTPOINT" ; eend $?
1129 # end dialog of autoinstallation {{{
1130 if [ -n "$AUTOINSTALL" ] ; then
1131 if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1132 Automatically rebooting in 10 seconds.
1134 Choose Cancel to skip rebooting." 10 60 10 ; then
1135 noeject noprompt reboot
1138 einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1142 ## END OF FILE #################################################################
1143 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=3