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 --keep_src_list Do not overwrite user provided apt sources.list.
75 --hostname <name> Hostname of Debian system.
76 --password <pwd> Use specified password as password for user root.
77 --bootappend <line> Add specified appendline to kernel whilst booting.
78 --chroot-scripts <d> Execute chroot scripts from specified directory.
79 --pre-scripts <dir> Execute scripts from specified directory (before chroot-scripts).
80 --scripts <dir> Execute scripts from specified directory (after chroot-scripts).
84 -v, --verbose Increase verbosity.
85 -h, --help Print this usage information and exit.
86 -V, --version Show summary of options and exit.
88 Usage examples can be found in the grml-debootstrap manpage.
89 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
93 if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
95 echo 'Please notice that this script requires root permissions!'
100 # early helper functions {{{
114 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
115 printf " ${GOOD}*${NORMAL} $*"
121 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
122 printf " ${BAD}*${NORMAL} $*\n"
128 local retval="${1:-0}"
130 if [ $retval -gt 0 ]; then
131 printf " ${BAD}-> Failed (rc=${retval})${NORMAL}\n"
137 if [ "$(id -u 2>/dev/null)" != 0 ] ; then
138 echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
145 which $arg >/dev/null 2>&1 || RC="$arg"
147 if [ -n "$RC" ] ; then
148 echo "$RC not installed"
154 # helper functions {{{
155 # we want to exit smoothly and clean:
157 # make sure $TARGET is not mounted when exiting grml-debootstrap
158 if [ -n "$MNTPOINT" ] ; then
159 if grep -q $MNTPOINT /proc/mounts ; then
160 # make sure nothing is left inside chroot so we can unmount it
161 [ -x "$MNTPOINT"/etc/init.d/ssh ] && "$MNTPOINT"/etc/init.d/ssh stop
162 [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
163 # ugly, but make sure we really don't leave anything (/proc /proc is intended)
164 for ARG in /sys /proc /proc ; do
165 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true
167 umount "$MNTPOINT"/dev >/dev/null 2>&1 || true
169 [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1 || true
171 if [ -n "$DIRECTORY" ] ; then
172 einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
174 einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
177 if [ -n "$STAGES" ] ; then
178 echo -n "Removing stages directory ${STAGES}: "
179 rm -rf "$STAGES" && echo done
182 # remove directory only if we used the default with process id inside the name
183 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
184 einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
189 if [ -n "${ORIG_TARGET}" ] ; then
190 einfo "Removing loopback mount of file ${ORIG_TARGET}."
191 kpartx -d "${ORIG_TARGET}" ; eend $?
194 [ -n "$1" ] && EXIT="$1" || EXIT="1"
195 [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
199 trap bailout HUP INT QUIT TERM
201 # we want to execute all the functions only once, simple check for it:
203 if [ -n "$2" ] ; then
204 echo "$2" > "${STAGES}/${1}"
206 elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
207 ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
208 ewarn " To reexecute it clean up the according directory inside $STAGES" ; eend 0
214 # make sure we have what we need {{{
215 check4progs debootstrap dialog || exit 1
218 # source main configuration file {{{
219 if [ -r /etc/debootstrap/config ] ; then
220 . /etc/debootstrap/config
224 # cmdline handling {{{
225 # source external command line parameter-processing script
226 if [ -r ./cmdlineopts.clp ] ; then
228 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
229 . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
231 eerror "Error: cmdline function file not found, exiting."
236 # == business-logic of command line parameter-processing
238 # source configuration file in <confdir> if supplied. {{{
239 [ "$_opt_confdir" ] && {
240 CONFFILES=$_opt_confdir
241 einfo "Using config files under $CONFFILES/."
242 if ! [ -r "$CONFFILES/config" ] ; then
243 eerror "Error: config file $CONFFILES/config not found."; eend 1; exit 1
245 if ! . "$CONFFILES/config" ; then
246 eerror "Error reading config file $CONFFILES/config" ; eend 1 ; exit 1
248 # restore the command line parameter value
249 CONFFILES=$_opt_confdir
253 [ "$_opt_mirror" ] && MIRROR=$_opt_mirror
254 [ "$_opt_iso" ] && ISO=$_opt_iso
255 [ "$_opt_release" ] && RELEASE=$_opt_release
256 [ "$_opt_target" ] && TARGET=$_opt_target
257 [ "$_opt_vmfile" ] && VIRTUAL=1
258 [ "$_opt_vmsize" ] && VMSIZE=$_opt_vmsize
259 [ "$_opt_mntpoint" ] && MNTPOINT=$_opt_mntpoint
260 [ "$_opt_debopt" ] && DEBOOTSTRAP_OPT=$_opt_debopt
261 [ "$_opt_interactive" ] && INTERACTIVE=1
262 [ "$_opt_config" ] && CONFIGFILE=$_opt_config
263 [ "$_opt_filesystem" ] && MKFS="mkfs.$_opt_filesystem"
264 [ "$_opt_packages_set" ] && PACKAGES='yes'
265 [ "$_opt_nopackages" ] && PACKAGES=''
266 [ "$_opt_debconf_set" ] && DEBCONF='yes'
267 [ "$_opt_scripts_set" ] && SCRIPTS='yes'
268 [ "$_opt_pre_scripts_set" ] && PRE_SCRIPTS='yes'
269 [ "$_opt_chroot_scripts_set" ] && CHROOT_SCRIPTS='yes'
270 [ "$_opt_keep_src_list" ] && KEEP_SRC_LIST='yes'
271 [ "$_opt_hostname" ] && HOSTNAME=$_opt_hostname
272 [ "$_opt_password" ] && ROOTPASSWORD=$_opt_password
273 [ "$_opt_bootappend" ] && BOOT_APPEND=$_opt_bootappend
274 [ "$_opt_grub" ] && GRUB=$_opt_grub
275 [ "$_opt_arch" ] && ARCH=$_opt_arch
276 [ "$_opt_insecure" ] && SECURE='false'
277 [ "$_opt_force" ] && FORCE=$_opt_force
278 [ "$_opt_verbose" ] && VERBOSE="-v"
280 [ "$_opt_help" ] && {
286 [ "$_opt_version" ] && {
287 einfo "$PN - version $VERSION"
288 einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
294 # check for root permissions {{{
295 if ! check4root ; then
296 echo "For usage instructions please execute '$PN --help'."
301 # make sure we have what we need {{{
302 if [ -n "$VIRTUAL" ] ; then
303 check4progs kpartx mksh qemu-img || exit 1
307 # source specified configuration file {{{
308 if [ -n "$CONFIGFILE" ] ; then
309 einfo "Reading specified config file $CONFIGFILE."
310 if ! . "$CONFIGFILE" ; then
311 eerror "Error reading config file $CONFIGFILE" ; eend 1 ; exit 1
316 # backwards compability checks {{{
317 if [ -n "$GROOT" ] ; then
318 eerror "Error: you seem to have \$GROOT configured."
319 eerror "This variable is no longer supported, please visit the"
320 eerror "grml-debootstrap documentation for details."
325 if echo "$GRUB" | grep -q '^hd' ; then
326 eerror "Error: this syntax for the grub configuration variable is no longer supported."
327 eerror "Please do not use hd... any longer but /dev/sdX instead."
336 dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
337 Do you want to continue installing Debian using this frontend?" 0 0 || bailout 0
344 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
346 grep -v 'Extended$' | \
347 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
349 if [ -z "$AVAILABLE_PARTITIONS" ] ; then
350 dialog --title "$PN" --trim \
351 --msgbox "Sorry, no partitions found. Please configure your
352 harddisks (see /proc/partitions) using a tool like fdisk,
353 cfdisk, gpart, gparted,..." 0 0
357 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
358 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet])"
361 TARGET=$(dialog --title "$PN" --single-quoted --stdout \
362 --menu "Please select the target partition:" 0 0 0 \
364 [ $? -eq 0 ] || bailout
368 # ask for bootmanager {{{
369 prompt_for_bootmanager()
373 if echo "$TARGET" | grep -q "/dev/md" ; then
374 MBRPART="all disks of Software RAID $TARGET"
376 # figure out whole disk device
378 for device in /dev/disk/by-id/*
380 [ $(readlink -f $device) = ${TARGET} ] || continue
384 [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
385 if [ -e "$MBRDISK" ]; then
386 MBRDISK=$(readlink -f $MBRDISK)
388 # fall back to old behaviour
389 MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$//')
392 MBRPART="MBR of $MBRDISK"
395 for device in cciss/c0d0 sda hda; do
396 if [ /dev/$device != ${MBRDISK} ]; then
397 grep -q $device /proc/partitions && \
398 ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
401 ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
405 GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
406 --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
407 mbr "install bootmanager into $MBRPART" \
408 nowhere "do not install bootmanager at all" \
409 ${ADDITIONAL_PARAMS})
410 [ $? -eq 0 ] || bailout 3
415 # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
416 if echo "$TARGET" | grep -q "/dev/md" ; then
418 if [ -n "$SELECTED_PARTITIONS" ] ; then
419 GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
440 # ask for Debian release {{{
443 [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='squeeze'
444 RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
445 "Please enter the Debian release you would like to use for installation:" \
447 lenny Debian/old-stable \
448 squeeze Debian/stable \
449 wheezy Debian/testing \
450 sid Debian/unstable)"
451 [ $? -eq 0 ] || bailout
455 # ask for hostname {{{
456 prompt_for_hostname()
458 HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
459 "Please enter the hostname you would like to use for installation:" \
461 [ $? -eq 0 ] || bailout
465 # ask for password {{{
466 prompt_for_password()
470 while [ "$ROOTPW1" != "$ROOTPW2" ]; do
471 ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
472 "Please enter the password for the root account:" 10 60)
473 [ $? -eq 0 ] || bailout
474 ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
475 "Please enter the password for the root account again for \
476 confirmation:" 10 60)
477 [ $? -eq 0 ] || bailout
479 if [ "$ROOTPW1" != "$ROOTPW2" ]; then
480 $(dialog --stdout --title "${PN}" --ok-label \
481 "Retry" --msgbox "Passwords do not match!" 10 60)
484 ROOTPASSWORD="$ROOTPW1"
488 # ask for Debian mirror {{{
491 [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
493 CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
494 --menu "Where do you want to install from?" 0 0 0 \
495 net "install via network (downloading from mirror)" \
496 local "install from local directory/mirror"
498 [ $? -eq 0 ] || bailout
500 if [ "$CHOOSE_MIRROR" = 'net' ] ; then
501 [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
502 MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
503 "Please enter Debian mirror you would like to use for installing packages." \
505 [ $? -eq 0 ] || bailout
506 else # CHOOSE_MIRROR == local
507 [ -n "$ISO" ] || ISO='/mnt/mirror'
508 ISO="$(dialog --stdout --title "${PN}" --inputbox \
509 "Please enter directory name you would like to use for installing packages." \
511 [ $? -eq 0 ] || bailout
516 # software raid setup {{{
517 config_swraid_setup()
521 # Currently we support only raid1:
524 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
525 # --menu "Which RAID level do you want to use?" 0 0 0 \
526 # raid1 "Software RAID level 1" \
527 # raid5 "Software RAID level 5")
528 #[ $? -eq 0 ] || bailout 20
530 MD_LIST=$(for i in $(seq 0 9) ; do
531 awk '{print $4}' /proc/partitions | grep -q md$i || \
532 echo "/dev/md$i /dev/md$i"
535 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
536 --menu "Which device do you want to use for ${RAIDLEVEL}?
538 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 \
540 [ $? -eq 0 ] || bailout 20
542 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
544 grep -v 'Extended$' | \
545 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
546 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
547 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
548 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
551 dialog --title "$PN" --separate-output \
552 --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
553 $PARTITION_LIST 2>$TMPFILE
554 [ $? -eq 0 ] || bailout
556 SELECTED_PARTITIONS="$(cat $TMPFILE)"
559 for i in $(cat $TMPFILE) ; do
560 NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
563 # force metadata version 0.90 for lenny so old grub can boot from this array.
565 if [ $RELEASE = "lenny" ]; then
566 METADATA_VERSION="-e0"
570 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
571 --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
573 if [ "$RC" = 0 ] ; then
574 dialog --title "$PN" --msgbox \
575 "Creating $TARGET was successful." 0 0
576 rm -f "$TMPFILE" "$ERRORFILE"
578 dialog --title "$PN" --msgbox \
579 "There was an error setting up $TARGET:
584 rm -f "$TMPFILE" "$ERRORFILE"
592 if dialog --stdout --title "$PN" \
593 --defaultno --yesno "Do you want to configure Software RAID?
595 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
601 # user should recheck his configuration {{{
602 # support full automatic installation:
604 dialog --timeout 10 --title "$PN" \
605 --yesno "Do you want to stop at this stage?
607 Notice: you are running ${PN} in non-interactive mode.
608 ${PN} will install Debian ${RELEASE} on ${TARGET}.
609 Last chance to quit. Timeout of 10 seconds running....
611 Do you want to stop now?" 0 0 2>/dev/null
615 # make sure the user is aware of the used configuration {{{
618 if [ -n "$AUTOINSTALL" ] ; then
619 if checkforrun ; then
620 eerror "Exiting as requested" ; eend 0
623 elif [ -n "$INTERACTIVE" ] ; then
625 INFOTEXT="Please recheck configuration before execution:
627 [ -n "$TARGET" ] && INFOTEXT="$INFOTEXT
629 [ -n "$GRUB" ] && INFOTEXT="$INFOTEXT
631 [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
632 Using release: $RELEASE"
633 [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
634 Using hostname $HOSTNAME"
635 [ -n "$MIRROR" ] && INFOTEXT="$INFOTEXT
636 Using mirror: $MIRROR"
637 [ -n "$ISO" ] && INFOTEXT="$INFOTEXT
642 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
644 dialog --title "$PN" --no-collapse \
645 --yesno "$INFOTEXT" 0 0
646 [ $? -eq 0 ] || bailout 0
648 else # if not running automatic installation display configuration and prompt for execution:
649 einfo "$PN - Please recheck configuration before execution:"
651 echo " Target: $TARGET"
653 # do not display if MNTPOINT is the default one
654 case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo " Mount point: $MNTPOINT" ;; esac
656 if [ -n "$VIRTUAL" ] ; then
657 echo " Install grub: yes"
659 [ -n "$GRUB" ] && echo " Install grub: $GRUB" || echo " Install grub: no"
662 [ -n "$RELEASE" ] && echo " Using release: $RELEASE"
663 [ -n "$MIRROR" ] && echo " Using mirror: $MIRROR"
664 [ -n "$HOSTNAME" ] && echo " Using hostname: $HOSTNAME"
665 [ -n "$ISO" ] && echo " Using ISO: $ISO"
666 if [ -n "$VIRTUAL" ] ; then
667 echo " Deploying as Virtual Machine."
668 [ -n "$VMSIZE" ] && echo " Using Virtual Disk file with size of ${VMSIZE}."
672 echo " Important! Continuing will delete all data from ${TARGET}!"
674 if [ -n "$FORCE" ] ; then
675 einfo "Skip user acknowledgement as requested via --force option."
678 einfon "Is this ok for you? [y/N] "
680 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
681 eerror "Exiting as requested." ; eend 1
689 # interactive mode {{{
700 prompt_for_bootmanager
709 # run interactive mode if we didn't get the according configuration yet
710 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
711 # only target might be unset, so make sure the INTERACTIVE flag is set as well
719 # finally make sure at least $TARGET is set [the partition for the new system] {{{
720 if [ -n "$TARGET" ] ; then
721 SHORT_TARGET="${TARGET##*/}"
723 eerror "Please adjust $CONFFILES/config or..."
724 eerror "... use the interactive version for configuration before running ${0}" ; eend 1
730 if [ -z "$STAGES" ] ; then
731 STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
732 [ -d "$STAGES" ] || mkdir -p "$STAGES"
735 if [ -r "$STAGES"/grml-debootstrap ] ; then
736 if grep -q done $STAGES/grml-debootstrap ; then
737 eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
738 eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
743 # partition handling {{{
747 set_target_directory(){
748 # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
755 # make sure we normalise the path to an absolute directory name so something like:
756 # mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
758 TARGET="$(readlink -f $TARGET)"
761 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
768 # architecture setup {{{
769 if [ -n "$ARCH" ] ; then
770 ARCHCMD="--arch $ARCH"
771 ARCHINFO=" (${ARCH})"
773 ARCH="$(dpkg --print-architecture)"
774 ARCHCMD="--arch $ARCH"
775 ARCHINFO=" (${ARCH})"
781 if [ "$SECURE" = 'yes' ] ; then
782 if [ -e '/etc/apt/trusted.gpg' ] ; then
783 KEYRING="--keyring /etc/apt/trusted.gpg"
785 eerror "Could not find /etc/apt/trusted.gpg."
788 ewarn "Not checking Release signatures!"
792 # make sure we have the right syntax when using an iso image {{{
793 if [ -n "$ISO" ] ; then
806 # create filesystem {{{
808 if [ -n "$DIRECTORY" ] ; then
809 einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
811 if grep -q "$TARGET" /proc/mounts ; then
812 eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
816 if [ -n "$MKFS" ] ; then
817 einfo "Running $MKFS on $TARGET"
818 $MKFS $TARGET ; RC=$?
820 # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
821 # will fail to detect the uuid in the chroot
822 if echo "$TARGET" | grep -q "/dev/md" ; then
823 blockdev --rereadpt "${TARGET}"
824 elif ! [ -n "$VIRTUAL" ] ; then
825 blockdev --rereadpt "${TARGET%%[0-9]*}"
827 # give the system 2 seconds, otherwise we might run into
828 # race conditions :-/
831 eval $(blkid -o udev $TARGET 2>/dev/null)
832 [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
841 # modify filesystem settings {{{
843 if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
844 einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
851 # mount the new partition or if it's a directory do nothing at all {{{
853 if [ -n "$DIRECTORY" ] ; then
854 einfo "Running grml-debootstrap on a directory, nothing to mount."
856 if grep -q $TARGET /proc/mounts ; then
857 ewarn "$TARGET already mounted, continuing anyway." ; eend 0
859 if ! [ -d "${MNTPOINT}" ] ; then
860 [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
862 einfo "Mounting $TARGET to $MNTPOINT"
864 mount -o rw,suid,dev $TARGET $MNTPOINT
868 if [ -n "$ISODIR" ] ; then
869 einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
870 mkdir -p "$MNTPOINT/$ISODIR"
871 mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
877 # prepare VM image for usage with debootstrap {{{
879 if [ -z "$VIRTUAL" ] ; then
880 return 0 # be quite by intention
883 if [ -b "$TARGET" ] ; then
884 eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
889 ORIG_TARGET="$TARGET" # store for later reuse
891 qemu-img create -f raw "${TARGET}" "${VMSIZE}"
892 echo 4 66 | /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
893 dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
894 parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
896 DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
897 if [ -z "${DEVINFO}" ] ; then
898 eerror "Error setting up loopback device." ; eend 1
902 # hopefully this always works as expected
903 LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
904 BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/') # '253:0'
905 LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
906 export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
908 blockdev --rereadpt "${LOOP}"
910 if [ -z "$TARGET" ] ; then
911 eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
917 # make VM image bootable and unmount it {{{
919 if [ -z "${VIRTUAL}" ] ; then
923 if ! mount "${TARGET}" "${MNTPOINT}" ; then
924 eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
928 einfo "Installing Grub as bootloader."
929 mount -t proc none "${MNTPOINT}"/proc
930 mount -t sysfs none "${MNTPOINT}"/sys
931 mount --bind /dev "${MNTPOINT}"/dev
933 mkdir -p "${MNTPOINT}/boot/grub"
934 if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
935 eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
939 cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
940 chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
941 dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
942 rm -f "${MNTPOINT}/tmp/core.img"
944 einfo "Updating grub configuration file."
945 chroot "${MNTPOINT}" update-grub
947 umount "${MNTPOINT}"/proc
948 umount "${MNTPOINT}"/sys
949 umount "${MNTPOINT}"/dev
951 einfo "Adjusting grub.cfg for successful boot sequence."
952 # ugly but needed to boot grub acordingly
953 sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
954 sed -i "s; root=/dev/.*; root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
957 kpartx -d "${ORIG_TARGET}" >/dev/null
961 # install main chroot {{{
962 debootstrap_system() {
963 if [ "$_opt_nodebootstrap" ]; then
964 einfo "Skipping debootstrap as requested."
968 if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
969 einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
970 if [ -n "$MIRROR" ] ; then
971 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
972 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
974 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
975 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
979 eerror "Error: $MNTPOINT not mounted, can not continue."
985 # prepare chroot via chroot-script {{{
987 einfo "Preparing chroot system"
989 # provide variables to chroot system
990 CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
991 touch $CHROOT_VARIABLES
992 chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
993 echo "# Configuration of ${PN}" > $CHROOT_VARIABLES
994 [ -n "$ARCH" ] && echo "ARCH=$ARCH" >> $CHROOT_VARIABLES
995 [ -n "$GRUB" ] && echo "GRUB=$GRUB" >> $CHROOT_VARIABLES
996 [ -n "$HOSTNAME" ] && echo "HOSTNAME=$HOSTNAME" >> $CHROOT_VARIABLES
997 [ -n "$INSTALL_NOTES" ] && echo "INSTALL_NOTES=$INSTALL_NOTES" >> $CHROOT_VARIABLES
998 [ -n "$ISODIR" ] && echo "ISODIR=$ISO" >> $CHROOT_VARIABLES
999 [ -n "$ISO" ] && echo "ISO=$ISO" >> $CHROOT_VARIABLES
1000 [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=$KEEP_SRC_LIST" >> $CHROOT_VARIABLES
1001 [ -n "$MIRROR" ] && echo "MIRROR=$MIRROR" >> $CHROOT_VARIABLES
1002 [ -n "$PACKAGES" ] && echo "PACKAGES=$PACKAGES" >> $CHROOT_VARIABLES
1003 [ -n "$RM_APTCACHE" ] && echo "RM_APTCACHE=$RM_APTCACHE" >> $CHROOT_VARIABLES
1004 [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
1005 [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=$SELECTED_PARTITIONS" >> $CHROOT_VARIABLES
1006 [ -n "$TARGET" ] && echo "TARGET=$TARGET" >> $CHROOT_VARIABLES
1007 [ -n "$TARGET_UUID" ] && echo "TARGET_UUID=$TARGET_UUID" >> $CHROOT_VARIABLES
1009 cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
1010 chmod 755 $MNTPOINT/bin/chroot-script
1011 [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
1013 # make sure we have our files for later use via chroot-script
1014 cp $VERBOSE $CONFFILES/config $MNTPOINT/etc/debootstrap/
1015 # make sure we adjust the configuration variables accordingly:
1016 sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
1017 sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#" $MNTPOINT/etc/debootstrap/config
1018 sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#" $MNTPOINT/etc/debootstrap/config
1021 if [ -n "$INSTALL_NOTES" ] ; then
1022 [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
1025 # package selection:
1026 cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
1027 $MNTPOINT/etc/debootstrap/packages
1029 # debconf preseeding:
1030 _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
1031 [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
1032 cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
1034 # copy scripts that should be executed inside the chroot:
1035 _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
1036 [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
1037 mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
1038 cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
1041 # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
1042 cp $VERBOSE $CHROOT_VARIABLES $MNTPOINT/etc/debootstrap/variables
1044 cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
1046 # make sure we can access network [relevant for cdebootstrap]
1047 [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
1049 # provide system's /etc/hosts to the target:
1050 if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
1051 cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
1054 # setup default locales
1055 [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen $MNTPOINT/etc/locale.gen
1057 # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
1058 ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
1060 # copy any existing files to chroot
1061 [ -d $CONFFILES/bin ] && cp $VERBOSE -a -L $CONFFILES/bin/* $MNTPOINT/bin/
1062 [ -d $CONFFILES/boot ] && cp $VERBOSE -a -L $CONFFILES/boot/* $MNTPOINT/boot/
1063 [ -d $CONFFILES/etc ] && cp $VERBOSE -a -L $CONFFILES/etc/* $MNTPOINT/etc/
1064 [ -d $CONFFILES/sbin ] && cp $VERBOSE -a -L $CONFFILES/sbin/* $MNTPOINT/sbin/
1065 [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
1066 [ -d $CONFFILES/usr ] && cp $VERBOSE -a -L $CONFFILES/usr/* $MNTPOINT/usr/
1067 [ -d $CONFFILES/var ] && cp $VERBOSE -a -L $CONFFILES/var/* $MNTPOINT/var/
1069 # copy local network setup to chroot
1070 if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
1071 [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
1072 cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
1075 # install config file providing some example entries
1076 if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1077 cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1084 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1085 execute_pre_scripts() {
1086 # make sure we have $MNTPOINT available for our scripts
1088 if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1089 [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1090 for script in ${pre_scripts}/* ; do
1091 if [ -x "$script" ] ; then
1092 einfo "Executing pre-script $script"
1100 # execute all scripts in /etc/debootstrap/scripts/ {{{
1102 # make sure we have $MNTPOINT available for our scripts
1104 if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1105 [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1106 for script in ${scripts}/* ; do
1107 if [ -x "$script" ] ; then
1108 einfo "Executing script $script"
1116 # execute chroot-script {{{
1118 if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1122 if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1123 einfo "Executing chroot-script now"
1124 mount --bind /dev "$MNTPOINT"/dev
1125 chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1126 umount "$MNTPOINT"/dev
1129 eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1135 # unmount $MNTPOINT {{{
1138 # display installation notes:
1139 if [ -n "$INSTALL_NOTES" ] ; then
1140 [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1143 if [ -n "$ISODIR" ] ; then
1144 if grep -q "$ISODIR" /proc/mounts ; then
1145 einfo "Unmount $MNTPOINT/$ISODIR"
1146 umount "$MNTPOINT/$ISODIR"
1151 if grep -q "$MNTPOINT" /proc/mounts ; then
1152 if [ -n "$PARTITION" ] ; then
1153 einfo "Unmount $MNTPOINT"
1161 # execute filesystem check {{{
1163 if [ "$FSCK" = 'yes' ] ; then
1164 [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1165 einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1172 # now execute all the functions {{{
1173 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1174 preparechroot execute_pre_scripts chrootscript execute_scripts \
1175 umount_chroot finalize_vm fscktool ; do
1176 if stage "${i}" ; then
1177 $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
1183 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
1184 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
1186 # Remove temporary mountpoint again
1187 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
1188 einfo "Removing directory ${MNTPOINT}" ; rmdir "$MNTPOINT" ; eend $?
1192 # end dialog of autoinstallation {{{
1193 if [ -n "$AUTOINSTALL" ] ; then
1194 if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1195 Automatically rebooting in 10 seconds.
1197 Choose Cancel to skip rebooting." 10 60 10 ; then
1198 noeject noprompt reboot
1201 einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1205 ## END OF FILE #################################################################
1206 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=3