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.$$"
18 CONFFILES='/etc/debootstrap'
20 DEBIAN_FRONTEND='noninteractive'
21 DEBOOTSTRAP='debootstrap'
26 INSTALL_NOTES='/etc/debootstrap/install_notes'
28 MIRROR='http://cdn.debian.net/debian'
32 RECONFIGURE='console-data'
37 TIMEZONE='Europe/Vienna'
38 TUNE2FS='tune2fs -c0 -i0'
42 # inside the chroot system locales might not be available, so use minimum:
46 # make sure interactive mode is only executed when
47 # using an empty configuration file or option --interactive
53 echo "$PN - wrapper around debootstrap for installing Debian
59 -m, --mirror <URL> Mirror which should be used for apt-get/aptitude.
60 -i, --iso <mnt> Mountpoint where a Debian ISO is mounted to, for use
61 instead of fetching packages from a mirror.
62 -r, --release <name> Release of new Debian system (default: squeeze).
63 -t, --target <target> Target partition (/dev/...) or directory where the
64 system should be installed to.
65 -p, --mntpoint <mnt> Mountpoint used for mounting the target system,
66 has no effect if -t is given and represents a directory.
67 --debopt <params> Extra parameters passed to the debootstrap command.
68 --interactive Use interactive mode (frontend).
69 --nodebootstrap Skip debootstrap, only do configuration to the target.
70 --grub <device> Target for grub installation. Usage example: /dev/sda
71 --arch <arch> Set target architecture, use for installing i386 on amd64.
72 --filesystem <fs> Filesystem that should be used when target is a partition
73 or Virtual Machine (see --vmfile).
74 --force Do not prompt for user acknowledgement.
76 Options for Virtual Machine deployment:
78 --vmfile Set up a Virtual Machine instead of installing to
79 a partition or directory, to be combined with --target,
80 like: --vmfile --target /mnt/sda1/qemu.img
81 --vmsize <size> Use specified size for size of VM file (default: 2G).
82 Syntax as supported by qemu-img, like: --vmsize 3G
84 Configuration options:
86 -c, --config <file> Use specified configuration file, defaults to
87 /etc/debootstrap/config
88 -d, --confdir <path> Place of config files for debootstrap, defaults
90 --packages <file> Install packages defined in specified list file
91 instead of using /etc/debootstrap/packages.
92 --nopackages Skip installation of packages defined in
93 /etc/debootstrap/packages
94 --debconf <file> Pre-seed packages using specified pre-seed db file.
95 --grmlrepos Enable Grml's Debian repository (deb.grml.org).
96 --keep_src_list Do not overwrite user provided apt sources.list.
97 --hostname <name> Hostname of Debian system.
98 --nopassword Do not prompt for the root password.
99 --password <pwd> Use specified password as password for user root.
100 --bootappend <line> Add specified appendline to kernel whilst booting.
101 --chroot-scripts <d> Execute chroot scripts from specified directory.
102 --pre-scripts <dir> Execute scripts from specified directory (before chroot-scripts).
103 --scripts <dir> Execute scripts from specified directory (after chroot-scripts).
107 -v, --verbose Increase verbosity.
108 -h, --help Print this usage information and exit.
109 -V, --version Show summary of options and exit.
111 Usage examples can be found in the grml-debootstrap manpage.
112 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
116 if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
118 echo 'Please notice that this script requires root permissions!'
123 # early helper functions {{{
137 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
138 printf " ${GOOD}*${NORMAL} $*"
144 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
145 printf " ${BAD}*${NORMAL} $*\n" >&2
151 local retval="${1:-0}"
153 if [ $retval -gt 0 ]; then
154 printf " ${BAD}-> Failed (rc=${retval})${NORMAL}\n"
160 if [ "$(id -u 2>/dev/null)" != 0 ] ; then
161 echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
168 which $arg >/dev/null 2>&1 || RC="$arg"
170 if [ -n "$RC" ] ; then
171 echo "$RC not installed"
177 # helper functions {{{
179 if [ -n "$CHROOT_VARIABLES" ] ; then
180 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
183 if [ -n "$STAGES" ] ; then
184 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
187 # Remove temporary mountpoint again
188 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
189 rmdir "$MNTPOINT" 2>/dev/null
192 # make sure $TARGET is not mounted when exiting grml-debootstrap
193 if [ -n "$MNTPOINT" ] ; then
194 if grep -q "$MNTPOINT" /proc/mounts ; then
195 # make sure nothing is left inside chroot so we can unmount it
196 [ -x "$MNTPOINT"/etc/init.d/ssh ] && "$MNTPOINT"/etc/init.d/ssh stop
197 [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
199 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a >/dev/null 2>&1
201 # ugly, but make sure we really don't leave anything (/proc /proc and
202 # /dev /dev are intended, trying to work around timing issues, see #657023)
203 for ARG in /sys /proc /proc /dev /dev ; do
204 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1
205 umount "$MNTPOINT"/$ARG >/dev/null 2>&1
208 if [ -n "$ISODIR" ] ; then
209 [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1
212 if [ -n "$DIRECTORY" ] ; then
213 einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
215 einfo "Unmounting $MNTPOINT"
220 if [ -n "$STAGES" ] ; then
221 echo -n "Removing stages directory ${STAGES}: "
222 rm -rf "$STAGES" && echo done
225 # remove directory only if we used the default with process id inside the name
226 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
227 einfo "Removing directory ${MNTPOINT}"
234 if [ -n "${ORIG_TARGET}" ] ; then
235 einfo "Removing loopback mount of file ${ORIG_TARGET}."
236 kpartx -d "${ORIG_TARGET}" ; eend $?
240 # we want to exit smoothly and clean:
245 [ -n "$1" ] && EXIT="$1" || EXIT="1"
246 [ -n "$2" ] && einfo "Notice: remove $STAGES/$2 to reexecute the stage"
250 trap bailout HUP INT QUIT TERM
252 # we want to execute all the functions only once, simple check for it:
254 if [ -n "$2" ] ; then
255 echo "$2" > "${STAGES}/${1}"
257 elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
258 ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
259 ewarn " To reexecute it clean up the according directory inside $STAGES" ; eend 0
265 # make sure we have what we need {{{
266 check4progs debootstrap || bailout 1
269 # source main configuration file {{{
270 if [ -r /etc/debootstrap/config ] ; then
271 . /etc/debootstrap/config
275 # cmdline handling {{{
276 # source external command line parameter-processing script
277 if [ -r ./cmdlineopts.clp ] ; then
279 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
280 . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
282 eerror "Error: cmdline function file not found, exiting."
287 # == business-logic of command line parameter-processing
289 # source configuration file in <confdir> if supplied. {{{
290 [ "$_opt_confdir" ] && {
291 CONFFILES=$_opt_confdir
292 einfo "Using config files under $CONFFILES/."
293 if ! [ -r "$CONFFILES/config" ] ; then
294 eerror "Error: config file $CONFFILES/config not found."; eend 1; bailout 1
296 if ! . "$CONFFILES/config" ; then
297 eerror "Error reading config file $CONFFILES/config" ; eend 1 ; bailout 1
299 # restore the command line parameter value
300 CONFFILES=$_opt_confdir
304 [ "$_opt_mirror" ] && MIRROR=$_opt_mirror
305 [ "$_opt_iso" ] && ISO=$_opt_iso
306 [ "$_opt_release" ] && RELEASE=$_opt_release
307 [ "$_opt_target" ] && TARGET=$_opt_target
308 [ "$_opt_vmfile" ] && VIRTUAL=1
309 [ "$_opt_vmsize" ] && VMSIZE=$_opt_vmsize
310 [ "$_opt_mntpoint" ] && MNTPOINT=$_opt_mntpoint
311 [ "$_opt_debopt" ] && DEBOOTSTRAP_OPT=$_opt_debopt
312 [ "$_opt_interactive" ] && INTERACTIVE=1
313 [ "$_opt_config" ] && CONFIGFILE=$_opt_config
314 [ "$_opt_filesystem" ] && MKFS="mkfs.$_opt_filesystem"
315 [ "$_opt_packages_set" ] && PACKAGES='yes'
316 [ "$_opt_nopackages" ] && PACKAGES=''
317 [ "$_opt_debconf_set" ] && DEBCONF='yes'
318 [ "$_opt_scripts_set" ] && SCRIPTS='yes'
319 [ "$_opt_pre_scripts_set" ] && PRE_SCRIPTS='yes'
320 [ "$_opt_chroot_scripts_set" ] && CHROOT_SCRIPTS='yes'
321 [ "$_opt_keep_src_list" ] && KEEP_SRC_LIST='yes'
322 [ "$_opt_grmlrepos" ] && GRMLREPOS='yes'
323 [ "$_opt_hostname" ] && HOSTNAME=$_opt_hostname
324 [ "$_opt_password" ] && ROOTPASSWORD=$_opt_password
325 [ "$_opt_nopassword" ] && NOPASSWORD='yes'
326 [ "$_opt_bootappend" ] && BOOT_APPEND=$_opt_bootappend
327 [ "$_opt_grub" ] && GRUB=$_opt_grub
328 [ "$_opt_arch" ] && ARCH=$_opt_arch
329 [ "$_opt_insecure" ] && echo "Warning: --insecure is deprecated, continuing anyway."
330 [ "$_opt_force" ] && FORCE=$_opt_force
331 [ "$_opt_verbose" ] && VERBOSE="-v"
333 [ "$_opt_help" ] && {
339 [ "$_opt_version" ] && {
340 einfo "$PN - version $VERSION"
341 einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
347 # check for root permissions {{{
348 if ! check4root ; then
349 echo "For usage instructions please execute '$PN --help'."
354 # make sure we have what we need {{{
355 if [ -n "$VIRTUAL" ] ; then
356 check4progs kpartx mksh parted qemu-img || bailout 1
360 # source specified configuration file {{{
361 if [ -n "$CONFIGFILE" ] ; then
362 einfo "Reading specified config file $CONFIGFILE."
363 if ! . "$CONFIGFILE" ; then
364 eerror "Error reading config file $CONFIGFILE" ; eend 1 ; bailout 1
369 # backwards compability checks {{{
370 if [ -n "$GROOT" ] ; then
371 eerror "Error: you seem to have \$GROOT configured."
372 eerror "This variable is no longer supported, please visit the"
373 eerror "grml-debootstrap documentation for details."
378 if echo "$GRUB" | grep -q '^hd' ; then
379 eerror "Error: this syntax for the grub configuration variable is no longer supported."
380 eerror "Please do not use hd... any longer but /dev/sdX instead."
389 dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
390 Do you want to continue installing Debian using this frontend?" 0 0 || bailout 0
397 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
399 grep -v 'Extended$' | \
400 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
402 if [ -z "$AVAILABLE_PARTITIONS" ] ; then
403 dialog --title "$PN" --trim \
404 --msgbox "Sorry, no partitions found. Please configure your
405 harddisks (see /proc/partitions) using a tool like fdisk,
406 cfdisk, gpart, gparted,..." 0 0
410 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
411 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet])"
414 TARGET=$(dialog --title "$PN" --single-quoted --stdout \
415 --menu "Please select the target partition:" 0 0 0 \
417 [ $? -eq 0 ] || bailout 1
421 # ask for bootmanager {{{
422 prompt_for_bootmanager()
426 if echo "$TARGET" | grep -q "/dev/md" ; then
427 MBRPART="all disks of Software RAID $TARGET"
429 # figure out whole disk device
431 for device in /dev/disk/by-id/*
433 [ $(readlink -f $device) = ${TARGET} ] || continue
437 [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
438 if [ -e "$MBRDISK" ]; then
439 MBRDISK=$(readlink -f $MBRDISK)
441 # fall back to old behaviour
442 MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$//')
445 MBRPART="MBR of $MBRDISK"
448 for device in cciss/c0d0 sda hda; do
449 if [ /dev/$device != ${MBRDISK} ]; then
450 grep -q $device /proc/partitions && \
451 ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
454 ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
458 GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
459 --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
460 mbr "install bootmanager into $MBRPART" \
461 nowhere "do not install bootmanager at all" \
462 ${ADDITIONAL_PARAMS})
463 [ $? -eq 0 ] || bailout 3
468 # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
469 if echo "$TARGET" | grep -q "/dev/md" ; then
471 if [ -n "$SELECTED_PARTITIONS" ] ; then
472 GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
493 # ask for Debian release {{{
496 [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='squeeze'
497 RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
498 "Please enter the Debian release you would like to use for installation:" \
500 lenny Debian/old-stable \
501 squeeze Debian/stable \
502 wheezy Debian/testing \
503 sid Debian/unstable)"
504 [ $? -eq 0 ] || bailout
508 # ask for hostname {{{
509 prompt_for_hostname()
511 HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
512 "Please enter the hostname you would like to use for installation:" \
514 [ $? -eq 0 ] || bailout
518 # ask for password {{{
519 prompt_for_password()
521 if [ "$_opt_nopassword" ] ; then
522 einfo "Skip asking for root password as requested."
528 while [ "$ROOTPW1" != "$ROOTPW2" ]; do
529 ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
530 "Please enter the password for the root account:" 10 60)
531 [ $? -eq 0 ] || bailout
532 ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
533 "Please enter the password for the root account again for \
534 confirmation:" 10 60)
535 [ $? -eq 0 ] || bailout
537 if [ "$ROOTPW1" != "$ROOTPW2" ]; then
538 $(dialog --stdout --title "${PN}" --ok-label \
539 "Retry" --msgbox "Passwords do not match!" 10 60)
542 ROOTPASSWORD="$ROOTPW1"
546 # ask for Debian mirror {{{
549 [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
551 CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
552 --menu "Where do you want to install from?" 0 0 0 \
553 net "install via network (downloading from mirror)" \
554 local "install from local directory/mirror"
556 [ $? -eq 0 ] || bailout
558 if [ "$CHOOSE_MIRROR" = 'net' ] ; then
559 [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
560 MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
561 "Please enter Debian mirror you would like to use for installing packages." \
563 [ $? -eq 0 ] || bailout
564 else # CHOOSE_MIRROR == local
565 [ -n "$ISO" ] || ISO='/mnt/mirror'
566 ISO="$(dialog --stdout --title "${PN}" --inputbox \
567 "Please enter directory name you would like to use for installing packages." \
569 [ $? -eq 0 ] || bailout
574 # software raid setup {{{
575 config_swraid_setup()
579 # Currently we support only raid1:
582 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
583 # --menu "Which RAID level do you want to use?" 0 0 0 \
584 # raid1 "Software RAID level 1" \
585 # raid5 "Software RAID level 5")
586 #[ $? -eq 0 ] || bailout 20
588 MD_LIST=$(for i in $(seq 0 9) ; do
589 awk '{print $4}' /proc/partitions | grep -q md$i || \
590 echo "/dev/md$i /dev/md$i"
593 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
594 --menu "Which device do you want to use for ${RAIDLEVEL}?
596 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 \
598 [ $? -eq 0 ] || bailout 20
600 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
602 grep -v 'Extended$' | \
603 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
604 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
605 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
606 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
609 dialog --title "$PN" --separate-output \
610 --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
611 $PARTITION_LIST 2>$TMPFILE
612 [ $? -eq 0 ] || bailout
614 SELECTED_PARTITIONS="$(cat $TMPFILE)"
617 for i in $(cat $TMPFILE) ; do
618 NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
621 # force metadata version 0.90 for lenny so old grub can boot from this array.
623 if [ $RELEASE = "lenny" ]; then
624 METADATA_VERSION="-e0"
628 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
629 --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
631 if [ "$RC" = 0 ] ; then
632 dialog --title "$PN" --msgbox \
633 "Creating $TARGET was successful." 0 0
634 rm -f "$TMPFILE" "$ERRORFILE"
636 dialog --title "$PN" --msgbox \
637 "There was an error setting up $TARGET:
642 rm -f "$TMPFILE" "$ERRORFILE"
650 if dialog --stdout --title "$PN" \
651 --defaultno --yesno "Do you want to configure Software RAID?
653 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
659 # user should recheck his configuration {{{
660 # support full automatic installation:
662 dialog --timeout 10 --title "$PN" \
663 --yesno "Do you want to stop at this stage?
665 Notice: you are running ${PN} in non-interactive mode.
666 ${PN} will install Debian ${RELEASE} on ${TARGET}.
667 Last chance to quit. Timeout of 10 seconds running....
669 Do you want to stop now?" 0 0 2>/dev/null
673 # make sure the user is aware of the used configuration {{{
676 if [ -n "$AUTOINSTALL" ] ; then
677 if checkforrun ; then
678 eerror "Exiting as requested" ; eend 0
681 elif [ -n "$INTERACTIVE" ] ; then
683 INFOTEXT="Please recheck configuration before execution:
685 [ -n "$TARGET" ] && INFOTEXT="$INFOTEXT
687 [ -n "$GRUB" ] && INFOTEXT="$INFOTEXT
689 [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
690 Using release: $RELEASE"
691 [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
692 Using hostname: $HOSTNAME"
693 [ -n "$MIRROR" ] && INFOTEXT="$INFOTEXT
694 Using mirror: $MIRROR"
695 [ -n "$ISO" ] && INFOTEXT="$INFOTEXT
697 [ -n "$ARCH" ] && INFOTEXT="$INFOTEXT
702 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
704 dialog --title "$PN" --no-collapse \
705 --yesno "$INFOTEXT" 0 0
706 [ $? -eq 0 ] || bailout 0
708 else # if not running automatic installation display configuration and prompt for execution:
709 einfo "$PN [${VERSION}] - Please recheck configuration before execution:"
711 echo " Target: $TARGET"
713 # do not display if MNTPOINT is the default one
714 case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo " Mount point: $MNTPOINT" ;; esac
716 if [ -n "$VIRTUAL" ] ; then
717 echo " Install grub: yes"
719 [ -n "$GRUB" ] && echo " Install grub: $GRUB" || echo " Install grub: no"
722 [ -n "$RELEASE" ] && echo " Using release: $RELEASE"
723 [ -n "$HOSTNAME" ] && echo " Using hostname: $HOSTNAME"
724 [ -n "$MIRROR" ] && echo " Using mirror: $MIRROR"
725 [ -n "$ISO" ] && echo " Using ISO: $ISO"
726 [ -n "$ARCH" ] && echo " Using arch: $ARCH"
727 if [ -n "$VIRTUAL" ] ; then
728 echo " Deploying as Virtual Machine."
729 [ -n "$VMSIZE" ] && echo " Using Virtual Disk file with size of ${VMSIZE}."
733 echo " Important! Continuing will delete all data from ${TARGET}!"
735 if [ -n "$FORCE" ] ; then
736 einfo "Skip user acknowledgement as requested via --force option."
739 einfon "Is this ok for you? [y/N] "
741 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
742 eerror "Exiting as requested." ; eend 1
750 # interactive mode {{{
753 check4progs dialog || bailout 1
763 prompt_for_bootmanager
772 # run interactive mode if we didn't get the according configuration yet
773 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
774 # only target might be unset, so make sure the INTERACTIVE flag is set as well
780 # architecture setup {{{
781 if [ -n "$ARCH" ] ; then
782 ARCHCMD="--arch $ARCH"
783 ARCHINFO=" (${ARCH})"
785 ARCH="$(dpkg --print-architecture)"
786 ARCHCMD="--arch $ARCH"
787 ARCHINFO=" (${ARCH})"
793 # finally make sure at least $TARGET is set [the partition for the new system] {{{
794 if [ -n "$TARGET" ] ; then
795 SHORT_TARGET="${TARGET##*/}"
797 eerror "Please adjust $CONFFILES/config or..."
798 eerror "... use the interactive version for configuration before running ${0}" ; eend 1
804 if [ -z "$STAGES" ] ; then
805 STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
806 [ -d "$STAGES" ] || mkdir -p "$STAGES"
809 if [ -r "$STAGES"/grml-debootstrap ] ; then
810 if grep -q done $STAGES/grml-debootstrap ; then
811 eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
812 eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
817 # partition handling {{{
821 set_target_directory(){
822 # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
829 # make sure we normalise the path to an absolute directory name so something like:
830 # mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
832 TARGET="$(readlink -f $TARGET)"
835 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
842 # make sure we have the right syntax when using an iso image {{{
843 if [ -n "$ISO" ] ; then
856 # create filesystem {{{
858 if [ -n "$DIRECTORY" ] ; then
859 einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
861 if grep -q "$TARGET" /proc/mounts ; then
862 eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
866 if [ -n "$MKFS" ] ; then
867 einfo "Running $MKFS on $TARGET"
868 $MKFS $TARGET ; RC=$?
870 # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
871 # will fail to detect the uuid in the chroot
872 if echo "$TARGET" | grep -q "/dev/md" ; then
873 blockdev --rereadpt "${TARGET}"
874 elif ! [ -n "$VIRTUAL" ] ; then
875 blockdev --rereadpt "${TARGET%%[0-9]*}"
877 # give the system 2 seconds, otherwise we might run into
878 # race conditions :-/
881 eval $(blkid -o udev $TARGET 2>/dev/null)
882 [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
891 # modify filesystem settings {{{
893 if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
894 einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
901 # mount the new partition or if it's a directory do nothing at all {{{
903 if [ -n "$DIRECTORY" ] ; then
904 einfo "Running grml-debootstrap on a directory, nothing to mount."
906 if grep -q $TARGET /proc/mounts ; then
907 ewarn "$TARGET already mounted, continuing anyway." ; eend 0
909 if ! [ -d "${MNTPOINT}" ] ; then
910 [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
912 einfo "Mounting $TARGET to $MNTPOINT"
914 mount -o rw,suid,dev $TARGET $MNTPOINT
918 if [ -n "$ISODIR" ] ; then
919 einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
920 mkdir -p "$MNTPOINT/$ISODIR"
921 mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
927 # prepare VM image for usage with debootstrap {{{
929 if [ -z "$VIRTUAL" ] ; then
930 return 0 # be quite by intention
933 if [ -b "$TARGET" ] ; then
934 eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
939 ORIG_TARGET="$TARGET" # store for later reuse
941 qemu-img create -f raw "${TARGET}" "${VMSIZE}"
942 echo 4 66 | /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
943 dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
944 parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
946 # if dm-mod isn't available then kpartx will fail with
947 # "Is device-mapper driver missing from kernel? [...]"
948 if ! kpartx -av $TARGET >/dev/null 2>&1 || ! grep -q device-mapper /proc/misc >/dev/null 2>&1 ; then
949 einfo "Device-mapper not ready yet, trying to load dm-mod module."
950 modprobe dm-mod ; eend $?
953 # make sure loop module is present
954 if ! losetup -f >/dev/null 2>&1; then
955 einfo "Can not find a usable loop device, retrying after loading loop module."
957 if losetup -f >/dev/null 2>&1; then
958 einfo "Found a usable loop device now, continuing."
960 eerror "Error finding usable loop device" ; eend 1
965 DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
966 if [ -z "${DEVINFO}" ] ; then
967 eerror "Error setting up loopback device." ; eend 1
971 # hopefully this always works as expected
972 LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
973 BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/') # '253:0'
974 LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
975 export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
977 blockdev --rereadpt "${LOOP}"
979 if [ -z "$TARGET" ] ; then
980 eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
986 # make VM image bootable and unmount it {{{
988 if [ -z "${VIRTUAL}" ] ; then
992 if ! mount "${TARGET}" "${MNTPOINT}" ; then
993 eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
997 einfo "Installing Grub as bootloader."
998 mount -t proc none "${MNTPOINT}"/proc
999 mount -t sysfs none "${MNTPOINT}"/sys
1000 mount --bind /dev "${MNTPOINT}"/dev
1002 mkdir -p "${MNTPOINT}/boot/grub"
1003 if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
1004 eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
1008 cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
1009 chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
1010 dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
1011 rm -f "${MNTPOINT}/tmp/core.img"
1013 einfo "Updating grub configuration file."
1014 chroot "${MNTPOINT}" update-grub
1016 umount "${MNTPOINT}"/proc
1017 umount "${MNTPOINT}"/sys
1018 umount "${MNTPOINT}"/dev
1020 einfo "Adjusting grub.cfg for successful boot sequence."
1021 # ugly but needed to boot grub acordingly
1022 sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
1023 sed -i "s;root=[^ ]\+;root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
1025 umount "${MNTPOINT}"
1026 kpartx -d "${ORIG_TARGET}" >/dev/null
1030 # install main chroot {{{
1031 debootstrap_system() {
1032 if [ "$_opt_nodebootstrap" ]; then
1033 einfo "Skipping debootstrap as requested."
1037 if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
1040 eerror "Error: $MNTPOINT not mounted, can not continue."
1044 einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
1045 if [ -n "$MIRROR" ] ; then
1046 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
1047 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
1050 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
1051 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
1055 if [ $RC -ne 0 ] ; then
1056 if [ -r "$MNTPOINT/debootstrap/debootstrap.log" ] && \
1057 [ -s "$MNTPOINT/debootstrap/debootstrap.log" ] ; then
1058 einfo "Presenting last ten lines of debootstrap.log:"
1059 tail -10 $MNTPOINT/debootstrap/debootstrap.log
1060 einfo "End of debootstrap.log"
1068 # prepare chroot via chroot-script {{{
1070 einfo "Preparing chroot system"
1072 # provide variables to chroot system
1073 CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
1074 touch $CHROOT_VARIABLES
1075 chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
1076 echo "# Configuration of ${PN}" > $CHROOT_VARIABLES
1077 [ -n "$ARCH" ] && echo "ARCH=\"$ARCH\"" >> $CHROOT_VARIABLES
1078 [ -n "$CHROOT_SCRIPTS" ] && echo "CHROOT_SCRIPTS=\"$CHROOT_SCRIPTS\"" >> $CHROOT_VARIABLES
1079 [ -n "$CONFFILES" ] && echo "CONFFILES=\"$CONFFILES\"" >> $CHROOT_VARIABLES
1080 [ -n "$DEBCONF" ] && echo "DEBCONF=\"$DEBCONF\"" >> $CHROOT_VARIABLES
1081 [ -n "$DEBIAN_FRONTEND" ] && echo "DEBIAN_FRONTEND=\"$DEBIAN_FRONTEND\"" >> $CHROOT_VARIABLES
1082 [ -n "$DEBOOTSTRAP" ] && echo "DEBOOTSTRAP=\"$DEBOOTSTRAP\"" >> $CHROOT_VARIABLES
1083 [ -n "$EXTRAPACKAGES" ] && echo "EXTRAPACKAGES=\"$EXTRAPACKAGES\"" >> $CHROOT_VARIABLES
1084 [ -n "$FORCE" ] && echo "FORCE=\"$FORCE\"" >> $CHROOT_VARIABLES
1085 [ -n "$GRMLREPOS" ] && echo "GRMLREPOS=\"$GRMLREPOS\"" >> $CHROOT_VARIABLES
1086 [ -n "$GRUB" ] && echo "GRUB=\"$GRUB\"" >> $CHROOT_VARIABLES
1087 [ -n "$HOSTNAME" ] && echo "HOSTNAME=\"$HOSTNAME\"" >> $CHROOT_VARIABLES
1088 [ -n "$INITRD" ] && echo "INITRD=\"$INITRD\"" >> $CHROOT_VARIABLES
1089 [ -n "$INSTALL_NOTES" ] && echo "INSTALL_NOTES=\"$INSTALL_NOTES\"" >> $CHROOT_VARIABLES
1090 [ -n "$ISODIR" ] && echo "ISODIR=\"$ISO\"" >> $CHROOT_VARIABLES
1091 [ -n "$ISO" ] && echo "ISO=\"$ISO\"" >> $CHROOT_VARIABLES
1092 [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=\"$KEEP_SRC_LIST\"" >> $CHROOT_VARIABLES
1093 [ -n "$LOCALES" ] && echo "LOCALES=\"$LOCALES\"" >> $CHROOT_VARIABLES
1094 [ -n "$MIRROR" ] && echo "MIRROR=\"$MIRROR\"" >> $CHROOT_VARIABLES
1095 [ -n "$MKFS" ] && echo "MKFS=\"$MKFS\"" >> $CHROOT_VARIABLES
1096 [ -n "$NOPASSWORD" ] && echo "NOPASSWORD=\"true\"" >> $CHROOT_VARIABLES
1097 [ -n "$PACKAGES" ] && echo "PACKAGES=\"$PACKAGES\"" >> $CHROOT_VARIABLES
1098 [ -n "$PRE_SCRIPTS" ] && echo "PRE_SCRIPTS=\"$PRE_SCRIPTS\"" >> $CHROOT_VARIABLES
1099 [ -n "$RECONFIGURE" ] && echo "RECONFIGURE=\"$RECONFIGURE\"" >> $CHROOT_VARIABLES
1100 [ -n "$RELEASE" ] && echo "RELEASE=\"$RELEASE\"" >> $CHROOT_VARIABLES
1101 [ -n "$RM_APTCACHE" ] && echo "RM_APTCACHE=\"$RM_APTCACHE\"" >> $CHROOT_VARIABLES
1102 [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=\"$ROOTPASSWORD\"" >> $CHROOT_VARIABLES
1103 [ -n "$SCRIPTS" ] && echo "SCRIPTS=\"$SCRIPTS\"" >> $CHROOT_VARIABLES
1104 [ -n "$SECURE" ] && echo "SECURE=\"$SECURE\"" >> $CHROOT_VARIABLES
1105 [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=\"$SELECTED_PARTITIONS\"" >> $CHROOT_VARIABLES
1106 [ -n "$TARGET" ] && echo "TARGET=\"$TARGET\"" >> $CHROOT_VARIABLES
1107 [ -n "$UPGRADE_SYSTEM" ] && echo "UPGRADE_SYSTEM=\"$UPGRADE_SYSTEM\"" >> $CHROOT_VARIABLES
1108 [ -n "$TARGET_UUID" ] && echo "TARGET_UUID=\"$TARGET_UUID\"" >> $CHROOT_VARIABLES
1109 [ -n "$TIMEZONE" ] && echo "TIMEZONE=\"$TIMEZONE\"" >> $CHROOT_VARIABLES
1110 [ -n "$TUNE2FS" ] && echo "TUNE2FS=\"$TUNE2FS\"" >> $CHROOT_VARIABLES
1111 [ -n "$VMSIZE" ] && echo "VMSIZE=\"$VMSIZE\"" >> $CHROOT_VARIABLES
1113 cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
1114 chmod 755 $MNTPOINT/bin/chroot-script
1115 [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
1117 # make sure we have our files for later use via chroot-script
1118 cp $VERBOSE $CONFFILES/config $MNTPOINT/etc/debootstrap/
1119 # make sure we adjust the configuration variables accordingly:
1120 sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
1121 sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#" $MNTPOINT/etc/debootstrap/config
1122 sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#" $MNTPOINT/etc/debootstrap/config
1125 if [ -n "$INSTALL_NOTES" ] ; then
1126 [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
1129 # package selection:
1130 cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
1131 $MNTPOINT/etc/debootstrap/packages
1133 # debconf preseeding:
1134 _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
1135 [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
1136 cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
1138 # copy scripts that should be executed inside the chroot:
1139 _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
1140 [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
1141 mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
1142 cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
1145 # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
1146 cp $VERBOSE $CHROOT_VARIABLES $MNTPOINT/etc/debootstrap/variables
1148 cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
1150 # make sure we can access network [relevant for cdebootstrap]
1151 [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
1153 # provide system's /etc/hosts to the target:
1154 if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
1155 cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
1158 # setup default locales
1159 [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen $MNTPOINT/etc/locale.gen
1161 # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
1162 ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
1164 # copy any existing files to chroot
1165 [ -d $CONFFILES/bin ] && cp $VERBOSE -a -L $CONFFILES/bin/* $MNTPOINT/bin/
1166 [ -d $CONFFILES/boot ] && cp $VERBOSE -a -L $CONFFILES/boot/* $MNTPOINT/boot/
1167 [ -d $CONFFILES/etc ] && cp $VERBOSE -a -L $CONFFILES/etc/* $MNTPOINT/etc/
1168 [ -d $CONFFILES/sbin ] && cp $VERBOSE -a -L $CONFFILES/sbin/* $MNTPOINT/sbin/
1169 [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
1170 [ -d $CONFFILES/usr ] && cp $VERBOSE -a -L $CONFFILES/usr/* $MNTPOINT/usr/
1171 [ -d $CONFFILES/var ] && cp $VERBOSE -a -L $CONFFILES/var/* $MNTPOINT/var/
1173 # copy local network setup to chroot
1174 if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
1175 [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
1176 cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
1179 # install config file providing some example entries
1180 if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1181 cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1188 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1189 execute_pre_scripts() {
1190 # make sure we have $MNTPOINT available for our scripts
1192 if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1193 [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1194 for script in ${pre_scripts}/* ; do
1195 if [ -x "$script" ] ; then
1196 einfo "Executing pre-script $script"
1204 # execute all scripts in /etc/debootstrap/scripts/ {{{
1206 # make sure we have $MNTPOINT available for our scripts
1208 if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1209 [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1210 for script in ${scripts}/* ; do
1211 if [ -x "$script" ] ; then
1212 einfo "Executing script $script"
1220 # execute chroot-script {{{
1222 if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1226 if ! [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1227 eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1230 einfo "Executing chroot-script now"
1231 mount --bind /dev "$MNTPOINT"/dev
1232 chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1233 umount "$MNTPOINT"/dev
1237 # finally get rid of chroot-script again, there's no good reason to
1238 # keep it on the installed system
1239 if grep -q GRML_CHROOT_SCRIPT_MARKER "${MNTPOINT}/bin/chroot-script" ; then
1240 einfo "Removing chroot-script again"
1241 rm -f "${MNTPOINT}/bin/chroot-script"
1244 einfo "Keeping chroot-script as string GRML_CHROOT_SCRIPT_MARKER could not be found"
1250 # unmount $MNTPOINT {{{
1253 # display installation notes:
1254 if [ -n "$INSTALL_NOTES" ] ; then
1255 [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1258 if [ -n "$ISODIR" ] ; then
1259 if grep -q "$ISODIR" /proc/mounts ; then
1260 einfo "Unmount $MNTPOINT/$ISODIR"
1261 umount "$MNTPOINT/$ISODIR"
1266 if grep -q "$MNTPOINT" /proc/mounts ; then
1267 if [ -n "$PARTITION" ] ; then
1268 einfo "Unmount $MNTPOINT"
1276 # execute filesystem check {{{
1278 if [ -n "$VIRTUAL" ] ; then
1279 einfo "Skipping filesystem check because we deploy a virtual machine."
1283 if [ "$FSCK" = 'yes' ] ; then
1284 [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1285 einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1292 # now execute all the functions {{{
1293 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1294 preparechroot execute_pre_scripts chrootscript execute_scripts \
1295 umount_chroot finalize_vm fscktool ; do
1296 if stage "${i}" ; then
1297 $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "$i"
1304 # end dialog of autoinstallation {{{
1305 if [ -n "$AUTOINSTALL" ] ; then
1306 if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1307 Automatically rebooting in 10 seconds.
1309 Choose Cancel to skip rebooting." 10 60 10 ; then
1310 noeject noprompt reboot
1313 einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1317 ## END OF FILE #################################################################
1318 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=2