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
12 VERSION="$(dpkg --list $PN 2>/dev/null| awk '/^i/ {print $3}')"
13 VERSION="${VERSION:-unknown}"
14 MNTPOINT="/mnt/debootstrap.$$"
20 # inside the chroot system locales might not be available, so use minimum:
24 # make sure interactive mode is only executed when
25 # using an empty configuration file or option --interactive
31 echo "$PN - wrapper around debootstrap for installing Debian
37 -m, --mirror <URL> Mirror which should be used for apt-get/aptitude.
38 -i, --iso <mnt> Mountpoint where a Debian ISO is mounted to, for use
39 instead of fetching packages from a mirror.
40 -r, --release <name> Release of new Debian system (default: squeeze).
41 -t, --target <target> Target partition (/dev/...) or directory where the
42 system should be installed to.
43 -p, --mntpoint <mnt> Mountpoint used for mounting the target system,
44 has no effect if -t is given and represents a directory.
45 --debopt <params> Extra parameters passed to the debootstrap command.
46 --interactive Use interactive mode (frontend).
47 --nodebootstrap Skip debootstrap, only do configuration to the target.
48 --grub <device> Target for grub installation. Usage example: /dev/sda
49 --arch <arch> Architecture to use. Currently only i386 is supported.
50 --filesystem <fs> Filesystem that should be used when target is a partition
51 or Virtual Machine (see --vmfile).
52 --insecure Do not download and check Release file signatures.
53 --force Do not prompt for user acknowledgement.
55 Options for Virtual Machine deployment:
57 --vmfile Set up a Virtual Machine instead of installing to
58 a partition or directory, to be combined with --target,
59 like: --vmfile --target /mnt/sda1/qemu.img
60 --vmsize <size> Use specified size for size of VM file (default: 2G).
61 Syntax as supported by qemu-img, like: --vmsize 3G
63 Configuration options:
65 -c, --config <file> Use specified configuration file, defaults to
66 /etc/debootstrap/config
67 -d, --confdir <path> Place of config files for debootstrap, defaults
69 --packages <file> Install packages defined in specified list file
70 instead of using /etc/debootstrap/packages.
71 --nopackages Skip installation of packages defined in
72 /etc/debootstrap/packages
73 --debconf <file> Pre-seed packages using specified pre-seed db file.
74 --grmlrepos Enable Grml's Debian repository (deb.grml.org).
75 --keep_src_list Do not overwrite user provided apt sources.list.
76 --hostname <name> Hostname of Debian system.
77 --nopassword Do not prompt for the root password.
78 --password <pwd> Use specified password as password for user root.
79 --bootappend <line> Add specified appendline to kernel whilst booting.
80 --chroot-scripts <d> Execute chroot scripts from specified directory.
81 --pre-scripts <dir> Execute scripts from specified directory (before chroot-scripts).
82 --scripts <dir> Execute scripts from specified directory (after chroot-scripts).
86 -v, --verbose Increase verbosity.
87 -h, --help Print this usage information and exit.
88 -V, --version Show summary of options and exit.
90 Usage examples can be found in the grml-debootstrap manpage.
91 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
95 if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
97 echo 'Please notice that this script requires root permissions!'
102 # early helper functions {{{
116 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
117 printf " ${GOOD}*${NORMAL} $*"
123 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
124 printf " ${BAD}*${NORMAL} $*\n"
130 local retval="${1:-0}"
132 if [ $retval -gt 0 ]; then
133 printf " ${BAD}-> Failed (rc=${retval})${NORMAL}\n"
139 if [ "$(id -u 2>/dev/null)" != 0 ] ; then
140 echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
147 which $arg >/dev/null 2>&1 || RC="$arg"
149 if [ -n "$RC" ] ; then
150 echo "$RC not installed"
156 # helper functions {{{
157 # we want to exit smoothly and clean:
159 # make sure $TARGET is not mounted when exiting grml-debootstrap
160 if [ -n "$MNTPOINT" ] ; then
161 if grep -q $MNTPOINT /proc/mounts ; then
162 # make sure nothing is left inside chroot so we can unmount it
163 [ -x "$MNTPOINT"/etc/init.d/ssh ] && "$MNTPOINT"/etc/init.d/ssh stop
164 [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
165 # ugly, but make sure we really don't leave anything (/proc /proc is intended)
166 for ARG in /sys /proc /proc ; do
167 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true
169 umount "$MNTPOINT"/dev >/dev/null 2>&1 || true
171 [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1 || true
173 if [ -n "$DIRECTORY" ] ; then
174 einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
176 einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
179 if [ -n "$STAGES" ] ; then
180 echo -n "Removing stages directory ${STAGES}: "
181 rm -rf "$STAGES" && echo done
184 # remove directory only if we used the default with process id inside the name
185 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
186 einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
191 if [ -n "${ORIG_TARGET}" ] ; then
192 einfo "Removing loopback mount of file ${ORIG_TARGET}."
193 kpartx -d "${ORIG_TARGET}" ; eend $?
196 [ -n "$1" ] && EXIT="$1" || EXIT="1"
197 [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
201 trap bailout HUP INT QUIT TERM
203 # we want to execute all the functions only once, simple check for it:
205 if [ -n "$2" ] ; then
206 echo "$2" > "${STAGES}/${1}"
208 elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
209 ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
210 ewarn " To reexecute it clean up the according directory inside $STAGES" ; eend 0
216 # make sure we have what we need {{{
217 check4progs debootstrap dialog || exit 1
220 # source main configuration file {{{
221 if [ -r /etc/debootstrap/config ] ; then
222 . /etc/debootstrap/config
226 # cmdline handling {{{
227 # source external command line parameter-processing script
228 if [ -r ./cmdlineopts.clp ] ; then
230 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
231 . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
233 eerror "Error: cmdline function file not found, exiting."
238 # == business-logic of command line parameter-processing
240 # source configuration file in <confdir> if supplied. {{{
241 [ "$_opt_confdir" ] && {
242 CONFFILES=$_opt_confdir
243 einfo "Using config files under $CONFFILES/."
244 if ! [ -r "$CONFFILES/config" ] ; then
245 eerror "Error: config file $CONFFILES/config not found."; eend 1; exit 1
247 if ! . "$CONFFILES/config" ; then
248 eerror "Error reading config file $CONFFILES/config" ; eend 1 ; exit 1
250 # restore the command line parameter value
251 CONFFILES=$_opt_confdir
255 [ "$_opt_mirror" ] && MIRROR=$_opt_mirror
256 [ "$_opt_iso" ] && ISO=$_opt_iso
257 [ "$_opt_release" ] && RELEASE=$_opt_release
258 [ "$_opt_target" ] && TARGET=$_opt_target
259 [ "$_opt_vmfile" ] && VIRTUAL=1
260 [ "$_opt_vmsize" ] && VMSIZE=$_opt_vmsize
261 [ "$_opt_mntpoint" ] && MNTPOINT=$_opt_mntpoint
262 [ "$_opt_debopt" ] && DEBOOTSTRAP_OPT=$_opt_debopt
263 [ "$_opt_interactive" ] && INTERACTIVE=1
264 [ "$_opt_config" ] && CONFIGFILE=$_opt_config
265 [ "$_opt_filesystem" ] && MKFS="mkfs.$_opt_filesystem"
266 [ "$_opt_packages_set" ] && PACKAGES='yes'
267 [ "$_opt_nopackages" ] && PACKAGES=''
268 [ "$_opt_debconf_set" ] && DEBCONF='yes'
269 [ "$_opt_scripts_set" ] && SCRIPTS='yes'
270 [ "$_opt_pre_scripts_set" ] && PRE_SCRIPTS='yes'
271 [ "$_opt_chroot_scripts_set" ] && CHROOT_SCRIPTS='yes'
272 [ "$_opt_keep_src_list" ] && KEEP_SRC_LIST='yes'
273 [ "$_opt_grmlrepos" ] && GRMLREPOS='yes'
274 [ "$_opt_hostname" ] && HOSTNAME=$_opt_hostname
275 [ "$_opt_password" ] && ROOTPASSWORD=$_opt_password
276 [ "$_opt_nopassword" ] && NOPASSWORD='yes'
277 [ "$_opt_bootappend" ] && BOOT_APPEND=$_opt_bootappend
278 [ "$_opt_grub" ] && GRUB=$_opt_grub
279 [ "$_opt_arch" ] && ARCH=$_opt_arch
280 [ "$_opt_insecure" ] && SECURE='false'
281 [ "$_opt_force" ] && FORCE=$_opt_force
282 [ "$_opt_verbose" ] && VERBOSE="-v"
284 [ "$_opt_help" ] && {
290 [ "$_opt_version" ] && {
291 einfo "$PN - version $VERSION"
292 einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
298 # check for root permissions {{{
299 if ! check4root ; then
300 echo "For usage instructions please execute '$PN --help'."
305 # make sure we have what we need {{{
306 if [ -n "$VIRTUAL" ] ; then
307 check4progs kpartx mksh qemu-img || exit 1
311 # source specified configuration file {{{
312 if [ -n "$CONFIGFILE" ] ; then
313 einfo "Reading specified config file $CONFIGFILE."
314 if ! . "$CONFIGFILE" ; then
315 eerror "Error reading config file $CONFIGFILE" ; eend 1 ; exit 1
320 # backwards compability checks {{{
321 if [ -n "$GROOT" ] ; then
322 eerror "Error: you seem to have \$GROOT configured."
323 eerror "This variable is no longer supported, please visit the"
324 eerror "grml-debootstrap documentation for details."
329 if echo "$GRUB" | grep -q '^hd' ; then
330 eerror "Error: this syntax for the grub configuration variable is no longer supported."
331 eerror "Please do not use hd... any longer but /dev/sdX instead."
340 dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
341 Do you want to continue installing Debian using this frontend?" 0 0 || bailout 0
348 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
350 grep -v 'Extended$' | \
351 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
353 if [ -z "$AVAILABLE_PARTITIONS" ] ; then
354 dialog --title "$PN" --trim \
355 --msgbox "Sorry, no partitions found. Please configure your
356 harddisks (see /proc/partitions) using a tool like fdisk,
357 cfdisk, gpart, gparted,..." 0 0
361 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
362 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet])"
365 TARGET=$(dialog --title "$PN" --single-quoted --stdout \
366 --menu "Please select the target partition:" 0 0 0 \
368 [ $? -eq 0 ] || bailout
372 # ask for bootmanager {{{
373 prompt_for_bootmanager()
377 if echo "$TARGET" | grep -q "/dev/md" ; then
378 MBRPART="all disks of Software RAID $TARGET"
380 # figure out whole disk device
382 for device in /dev/disk/by-id/*
384 [ $(readlink -f $device) = ${TARGET} ] || continue
388 [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
389 if [ -e "$MBRDISK" ]; then
390 MBRDISK=$(readlink -f $MBRDISK)
392 # fall back to old behaviour
393 MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$//')
396 MBRPART="MBR of $MBRDISK"
399 for device in cciss/c0d0 sda hda; do
400 if [ /dev/$device != ${MBRDISK} ]; then
401 grep -q $device /proc/partitions && \
402 ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
405 ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
409 GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
410 --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
411 mbr "install bootmanager into $MBRPART" \
412 nowhere "do not install bootmanager at all" \
413 ${ADDITIONAL_PARAMS})
414 [ $? -eq 0 ] || bailout 3
419 # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
420 if echo "$TARGET" | grep -q "/dev/md" ; then
422 if [ -n "$SELECTED_PARTITIONS" ] ; then
423 GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
444 # ask for Debian release {{{
447 [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='squeeze'
448 RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
449 "Please enter the Debian release you would like to use for installation:" \
451 lenny Debian/old-stable \
452 squeeze Debian/stable \
453 wheezy Debian/testing \
454 sid Debian/unstable)"
455 [ $? -eq 0 ] || bailout
459 # ask for hostname {{{
460 prompt_for_hostname()
462 HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
463 "Please enter the hostname you would like to use for installation:" \
465 [ $? -eq 0 ] || bailout
469 # ask for password {{{
470 prompt_for_password()
472 if [ "$_opt_nopassword" ] ; then
473 einfo "Skip asking for root password as requested."
479 while [ "$ROOTPW1" != "$ROOTPW2" ]; do
480 ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
481 "Please enter the password for the root account:" 10 60)
482 [ $? -eq 0 ] || bailout
483 ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
484 "Please enter the password for the root account again for \
485 confirmation:" 10 60)
486 [ $? -eq 0 ] || bailout
488 if [ "$ROOTPW1" != "$ROOTPW2" ]; then
489 $(dialog --stdout --title "${PN}" --ok-label \
490 "Retry" --msgbox "Passwords do not match!" 10 60)
493 ROOTPASSWORD="$ROOTPW1"
497 # ask for Debian mirror {{{
500 [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
502 CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
503 --menu "Where do you want to install from?" 0 0 0 \
504 net "install via network (downloading from mirror)" \
505 local "install from local directory/mirror"
507 [ $? -eq 0 ] || bailout
509 if [ "$CHOOSE_MIRROR" = 'net' ] ; then
510 [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
511 MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
512 "Please enter Debian mirror you would like to use for installing packages." \
514 [ $? -eq 0 ] || bailout
515 else # CHOOSE_MIRROR == local
516 [ -n "$ISO" ] || ISO='/mnt/mirror'
517 ISO="$(dialog --stdout --title "${PN}" --inputbox \
518 "Please enter directory name you would like to use for installing packages." \
520 [ $? -eq 0 ] || bailout
525 # software raid setup {{{
526 config_swraid_setup()
530 # Currently we support only raid1:
533 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
534 # --menu "Which RAID level do you want to use?" 0 0 0 \
535 # raid1 "Software RAID level 1" \
536 # raid5 "Software RAID level 5")
537 #[ $? -eq 0 ] || bailout 20
539 MD_LIST=$(for i in $(seq 0 9) ; do
540 awk '{print $4}' /proc/partitions | grep -q md$i || \
541 echo "/dev/md$i /dev/md$i"
544 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
545 --menu "Which device do you want to use for ${RAIDLEVEL}?
547 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 \
549 [ $? -eq 0 ] || bailout 20
551 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
553 grep -v 'Extended$' | \
554 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
555 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
556 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
557 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
560 dialog --title "$PN" --separate-output \
561 --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
562 $PARTITION_LIST 2>$TMPFILE
563 [ $? -eq 0 ] || bailout
565 SELECTED_PARTITIONS="$(cat $TMPFILE)"
568 for i in $(cat $TMPFILE) ; do
569 NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
572 # force metadata version 0.90 for lenny so old grub can boot from this array.
574 if [ $RELEASE = "lenny" ]; then
575 METADATA_VERSION="-e0"
579 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
580 --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
582 if [ "$RC" = 0 ] ; then
583 dialog --title "$PN" --msgbox \
584 "Creating $TARGET was successful." 0 0
585 rm -f "$TMPFILE" "$ERRORFILE"
587 dialog --title "$PN" --msgbox \
588 "There was an error setting up $TARGET:
593 rm -f "$TMPFILE" "$ERRORFILE"
601 if dialog --stdout --title "$PN" \
602 --defaultno --yesno "Do you want to configure Software RAID?
604 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
610 # user should recheck his configuration {{{
611 # support full automatic installation:
613 dialog --timeout 10 --title "$PN" \
614 --yesno "Do you want to stop at this stage?
616 Notice: you are running ${PN} in non-interactive mode.
617 ${PN} will install Debian ${RELEASE} on ${TARGET}.
618 Last chance to quit. Timeout of 10 seconds running....
620 Do you want to stop now?" 0 0 2>/dev/null
624 # make sure the user is aware of the used configuration {{{
627 if [ -n "$AUTOINSTALL" ] ; then
628 if checkforrun ; then
629 eerror "Exiting as requested" ; eend 0
632 elif [ -n "$INTERACTIVE" ] ; then
634 INFOTEXT="Please recheck configuration before execution:
636 [ -n "$TARGET" ] && INFOTEXT="$INFOTEXT
638 [ -n "$GRUB" ] && INFOTEXT="$INFOTEXT
640 [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
641 Using release: $RELEASE"
642 [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
643 Using hostname $HOSTNAME"
644 [ -n "$MIRROR" ] && INFOTEXT="$INFOTEXT
645 Using mirror: $MIRROR"
646 [ -n "$ISO" ] && INFOTEXT="$INFOTEXT
651 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
653 dialog --title "$PN" --no-collapse \
654 --yesno "$INFOTEXT" 0 0
655 [ $? -eq 0 ] || bailout 0
657 else # if not running automatic installation display configuration and prompt for execution:
658 einfo "$PN - Please recheck configuration before execution:"
660 echo " Target: $TARGET"
662 # do not display if MNTPOINT is the default one
663 case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo " Mount point: $MNTPOINT" ;; esac
665 if [ -n "$VIRTUAL" ] ; then
666 echo " Install grub: yes"
668 [ -n "$GRUB" ] && echo " Install grub: $GRUB" || echo " Install grub: no"
671 [ -n "$RELEASE" ] && echo " Using release: $RELEASE"
672 [ -n "$MIRROR" ] && echo " Using mirror: $MIRROR"
673 [ -n "$HOSTNAME" ] && echo " Using hostname: $HOSTNAME"
674 [ -n "$ISO" ] && echo " Using ISO: $ISO"
675 if [ -n "$VIRTUAL" ] ; then
676 echo " Deploying as Virtual Machine."
677 [ -n "$VMSIZE" ] && echo " Using Virtual Disk file with size of ${VMSIZE}."
681 echo " Important! Continuing will delete all data from ${TARGET}!"
683 if [ -n "$FORCE" ] ; then
684 einfo "Skip user acknowledgement as requested via --force option."
687 einfon "Is this ok for you? [y/N] "
689 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
690 eerror "Exiting as requested." ; eend 1
698 # interactive mode {{{
709 prompt_for_bootmanager
718 # run interactive mode if we didn't get the according configuration yet
719 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
720 # only target might be unset, so make sure the INTERACTIVE flag is set as well
728 # finally make sure at least $TARGET is set [the partition for the new system] {{{
729 if [ -n "$TARGET" ] ; then
730 SHORT_TARGET="${TARGET##*/}"
732 eerror "Please adjust $CONFFILES/config or..."
733 eerror "... use the interactive version for configuration before running ${0}" ; eend 1
739 if [ -z "$STAGES" ] ; then
740 STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
741 [ -d "$STAGES" ] || mkdir -p "$STAGES"
744 if [ -r "$STAGES"/grml-debootstrap ] ; then
745 if grep -q done $STAGES/grml-debootstrap ; then
746 eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
747 eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
752 # partition handling {{{
756 set_target_directory(){
757 # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
764 # make sure we normalise the path to an absolute directory name so something like:
765 # mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
767 TARGET="$(readlink -f $TARGET)"
770 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
777 # architecture setup {{{
778 if [ -n "$ARCH" ] ; then
779 ARCHCMD="--arch $ARCH"
780 ARCHINFO=" (${ARCH})"
782 ARCH="$(dpkg --print-architecture)"
783 ARCHCMD="--arch $ARCH"
784 ARCHINFO=" (${ARCH})"
790 if [ "$SECURE" = 'yes' ] ; then
791 if [ -e '/etc/apt/trusted.gpg' ] ; then
792 KEYRING="--keyring /etc/apt/trusted.gpg"
794 eerror "Could not find /etc/apt/trusted.gpg."
797 ewarn "Not checking Release signatures!"
801 # make sure we have the right syntax when using an iso image {{{
802 if [ -n "$ISO" ] ; then
815 # create filesystem {{{
817 if [ -n "$DIRECTORY" ] ; then
818 einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
820 if grep -q "$TARGET" /proc/mounts ; then
821 eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
825 if [ -n "$MKFS" ] ; then
826 einfo "Running $MKFS on $TARGET"
827 $MKFS $TARGET ; RC=$?
829 # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
830 # will fail to detect the uuid in the chroot
831 if echo "$TARGET" | grep -q "/dev/md" ; then
832 blockdev --rereadpt "${TARGET}"
833 elif ! [ -n "$VIRTUAL" ] ; then
834 blockdev --rereadpt "${TARGET%%[0-9]*}"
836 # give the system 2 seconds, otherwise we might run into
837 # race conditions :-/
840 eval $(blkid -o udev $TARGET 2>/dev/null)
841 [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
850 # modify filesystem settings {{{
852 if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
853 einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
860 # mount the new partition or if it's a directory do nothing at all {{{
862 if [ -n "$DIRECTORY" ] ; then
863 einfo "Running grml-debootstrap on a directory, nothing to mount."
865 if grep -q $TARGET /proc/mounts ; then
866 ewarn "$TARGET already mounted, continuing anyway." ; eend 0
868 if ! [ -d "${MNTPOINT}" ] ; then
869 [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
871 einfo "Mounting $TARGET to $MNTPOINT"
873 mount -o rw,suid,dev $TARGET $MNTPOINT
877 if [ -n "$ISODIR" ] ; then
878 einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
879 mkdir -p "$MNTPOINT/$ISODIR"
880 mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
886 # prepare VM image for usage with debootstrap {{{
888 if [ -z "$VIRTUAL" ] ; then
889 return 0 # be quite by intention
892 if [ -b "$TARGET" ] ; then
893 eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
898 ORIG_TARGET="$TARGET" # store for later reuse
900 qemu-img create -f raw "${TARGET}" "${VMSIZE}"
901 echo 4 66 | /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
902 dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
903 parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
905 DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
906 if [ -z "${DEVINFO}" ] ; then
907 eerror "Error setting up loopback device." ; eend 1
911 # hopefully this always works as expected
912 LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
913 BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/') # '253:0'
914 LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
915 export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
917 blockdev --rereadpt "${LOOP}"
919 if [ -z "$TARGET" ] ; then
920 eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
926 # make VM image bootable and unmount it {{{
928 if [ -z "${VIRTUAL}" ] ; then
932 if ! mount "${TARGET}" "${MNTPOINT}" ; then
933 eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
937 einfo "Installing Grub as bootloader."
938 mount -t proc none "${MNTPOINT}"/proc
939 mount -t sysfs none "${MNTPOINT}"/sys
940 mount --bind /dev "${MNTPOINT}"/dev
942 mkdir -p "${MNTPOINT}/boot/grub"
943 if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
944 eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
948 cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
949 chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
950 dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
951 rm -f "${MNTPOINT}/tmp/core.img"
953 einfo "Updating grub configuration file."
954 chroot "${MNTPOINT}" update-grub
956 umount "${MNTPOINT}"/proc
957 umount "${MNTPOINT}"/sys
958 umount "${MNTPOINT}"/dev
960 einfo "Adjusting grub.cfg for successful boot sequence."
961 # ugly but needed to boot grub acordingly
962 sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
963 sed -i "s; root=/dev/.*; root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
966 kpartx -d "${ORIG_TARGET}" >/dev/null
970 # install main chroot {{{
971 debootstrap_system() {
972 if [ "$_opt_nodebootstrap" ]; then
973 einfo "Skipping debootstrap as requested."
977 if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
978 einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
979 if [ -n "$MIRROR" ] ; then
980 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
981 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
983 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
984 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
988 eerror "Error: $MNTPOINT not mounted, can not continue."
994 # prepare chroot via chroot-script {{{
996 einfo "Preparing chroot system"
998 # provide variables to chroot system
999 CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
1000 touch $CHROOT_VARIABLES
1001 chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
1002 echo "# Configuration of ${PN}" > $CHROOT_VARIABLES
1003 [ -n "$ARCH" ] && echo "ARCH=$ARCH" >> $CHROOT_VARIABLES
1004 [ -n "$GRUB" ] && echo "GRUB=$GRUB" >> $CHROOT_VARIABLES
1005 [ -n "$HOSTNAME" ] && echo "HOSTNAME=$HOSTNAME" >> $CHROOT_VARIABLES
1006 [ -n "$INSTALL_NOTES" ] && echo "INSTALL_NOTES=$INSTALL_NOTES" >> $CHROOT_VARIABLES
1007 [ -n "$ISODIR" ] && echo "ISODIR=$ISO" >> $CHROOT_VARIABLES
1008 [ -n "$ISO" ] && echo "ISO=$ISO" >> $CHROOT_VARIABLES
1009 [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=$KEEP_SRC_LIST" >> $CHROOT_VARIABLES
1010 [ -n "$MIRROR" ] && echo "MIRROR=$MIRROR" >> $CHROOT_VARIABLES
1011 [ -n "$PACKAGES" ] && echo "PACKAGES=$PACKAGES" >> $CHROOT_VARIABLES
1012 [ -n "$GRMLREPOS" ] && echo "GRMLREPOS=$GRMLREPOS" >> $CHROOT_VARIABLES
1013 [ -n "$RM_APTCACHE" ] && echo "RM_APTCACHE=$RM_APTCACHE" >> $CHROOT_VARIABLES
1014 [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
1015 [ -n "$NOPASSWORD" ] && echo "NOPASSWORD=true" >> $CHROOT_VARIABLES
1016 [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=$SELECTED_PARTITIONS" >> $CHROOT_VARIABLES
1017 [ -n "$TARGET" ] && echo "TARGET=$TARGET" >> $CHROOT_VARIABLES
1018 [ -n "$TARGET_UUID" ] && echo "TARGET_UUID=$TARGET_UUID" >> $CHROOT_VARIABLES
1020 cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
1021 chmod 755 $MNTPOINT/bin/chroot-script
1022 [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
1024 # make sure we have our files for later use via chroot-script
1025 cp $VERBOSE $CONFFILES/config $MNTPOINT/etc/debootstrap/
1026 # make sure we adjust the configuration variables accordingly:
1027 sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
1028 sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#" $MNTPOINT/etc/debootstrap/config
1029 sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#" $MNTPOINT/etc/debootstrap/config
1032 if [ -n "$INSTALL_NOTES" ] ; then
1033 [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
1036 # package selection:
1037 cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
1038 $MNTPOINT/etc/debootstrap/packages
1040 # debconf preseeding:
1041 _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
1042 [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
1043 cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
1045 # copy scripts that should be executed inside the chroot:
1046 _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
1047 [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
1048 mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
1049 cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
1052 # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
1053 cp $VERBOSE $CHROOT_VARIABLES $MNTPOINT/etc/debootstrap/variables
1055 cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
1057 # make sure we can access network [relevant for cdebootstrap]
1058 [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
1060 # provide system's /etc/hosts to the target:
1061 if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
1062 cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
1065 # setup default locales
1066 [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen $MNTPOINT/etc/locale.gen
1068 # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
1069 ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
1071 # copy any existing files to chroot
1072 [ -d $CONFFILES/bin ] && cp $VERBOSE -a -L $CONFFILES/bin/* $MNTPOINT/bin/
1073 [ -d $CONFFILES/boot ] && cp $VERBOSE -a -L $CONFFILES/boot/* $MNTPOINT/boot/
1074 [ -d $CONFFILES/etc ] && cp $VERBOSE -a -L $CONFFILES/etc/* $MNTPOINT/etc/
1075 [ -d $CONFFILES/sbin ] && cp $VERBOSE -a -L $CONFFILES/sbin/* $MNTPOINT/sbin/
1076 [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
1077 [ -d $CONFFILES/usr ] && cp $VERBOSE -a -L $CONFFILES/usr/* $MNTPOINT/usr/
1078 [ -d $CONFFILES/var ] && cp $VERBOSE -a -L $CONFFILES/var/* $MNTPOINT/var/
1080 # copy local network setup to chroot
1081 if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
1082 [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
1083 cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
1086 # install config file providing some example entries
1087 if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1088 cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1095 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1096 execute_pre_scripts() {
1097 # make sure we have $MNTPOINT available for our scripts
1099 if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1100 [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1101 for script in ${pre_scripts}/* ; do
1102 if [ -x "$script" ] ; then
1103 einfo "Executing pre-script $script"
1111 # execute all scripts in /etc/debootstrap/scripts/ {{{
1113 # make sure we have $MNTPOINT available for our scripts
1115 if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1116 [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1117 for script in ${scripts}/* ; do
1118 if [ -x "$script" ] ; then
1119 einfo "Executing script $script"
1127 # execute chroot-script {{{
1129 if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1133 if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1134 einfo "Executing chroot-script now"
1135 mount --bind /dev "$MNTPOINT"/dev
1136 chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1137 umount "$MNTPOINT"/dev
1140 eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1146 # unmount $MNTPOINT {{{
1149 # display installation notes:
1150 if [ -n "$INSTALL_NOTES" ] ; then
1151 [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1154 if [ -n "$ISODIR" ] ; then
1155 if grep -q "$ISODIR" /proc/mounts ; then
1156 einfo "Unmount $MNTPOINT/$ISODIR"
1157 umount "$MNTPOINT/$ISODIR"
1162 if grep -q "$MNTPOINT" /proc/mounts ; then
1163 if [ -n "$PARTITION" ] ; then
1164 einfo "Unmount $MNTPOINT"
1172 # execute filesystem check {{{
1174 if [ -n "$VIRTUAL" ] ; then
1175 einfo "Skipping filesystem check since we deploy a virtual machine."
1179 if [ "$FSCK" = 'yes' ] ; then
1180 [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1181 einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1188 # now execute all the functions {{{
1189 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1190 preparechroot execute_pre_scripts chrootscript execute_scripts \
1191 umount_chroot finalize_vm fscktool ; do
1192 if stage "${i}" ; then
1193 $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
1199 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
1200 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
1202 # Remove temporary mountpoint again
1203 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
1204 einfo "Removing directory ${MNTPOINT}" ; rmdir "$MNTPOINT" ; eend $?
1208 # end dialog of autoinstallation {{{
1209 if [ -n "$AUTOINSTALL" ] ; then
1210 if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1211 Automatically rebooting in 10 seconds.
1213 Choose Cancel to skip rebooting." 10 60 10 ; then
1214 noeject noprompt reboot
1217 einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1221 ## END OF FILE #################################################################
1222 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=2