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 --nopackages Skip installation of packages defined in
71 /etc/debootstrap/packages
72 --debconf <file> Pre-seed packages using specified pre-seed db file.
73 --keep_src_list Do not overwrite user provided apt sources.list.
74 --hostname <name> Hostname of Debian system.
75 --password <pwd> Use specified password as password for user root.
76 --bootappend <line> Add specified appendline to kernel whilst booting.
77 --chroot-scripts <d> Execute chroot scripts from specified directory.
78 --pre-scripts <dir> Execute scripts from specified directory (before chroot-scripts).
79 --scripts <dir> Execute scripts from specified directory (after chroot-scripts).
83 -v, --verbose Increase verbosity.
84 -h, --help Print this usage information and exit.
85 -V, --version Show summary of options and exit.
87 Usage examples can be found in the grml-debootstrap manpage.
88 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
92 if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
94 echo 'Please notice that this script requires root permissions!'
113 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
114 printf " ${GOOD}*${NORMAL} $*"
120 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
121 printf " ${BAD}*${NORMAL} $*\n"
127 local retval="${1:-0}"
129 if [ $retval -gt 0 ]; then
130 printf " ${BAD}-> Failed (rc=${retval})${NORMAL}\n"
136 if [ "$(id -u 2>/dev/null)" != 0 ] ; then
137 echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
144 which $arg >/dev/null 2>&1 || RC="$arg"
146 if [ -n "$RC" ] ; then
147 echo "$RC not installed"
154 # make sure we have what we need {{{
155 check4progs debootstrap dialog || exit 1
158 # source main configuration file {{{
159 if [ -r /etc/debootstrap/config ] ; then
160 . /etc/debootstrap/config
164 # cmdline handling {{{
165 # source external command line parameter-processing script
166 if [ -r ./cmdlineopts.clp ] ; then
168 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
169 . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
171 eerror "Error: cmdline function file not found, exiting."
176 # == business-logic of command line parameter-processing
178 # source configuration file in <confdir> if supplied. {{{
179 [ "$_opt_confdir" ] && {
180 CONFFILES=$_opt_confdir
181 einfo "Using config files under $CONFFILES/."
182 if ! [ -r "$CONFFILES/config" ] ; then
183 eerror "Error: config file $CONFFILES/config not found."; eend 1; exit 1
185 if ! . "$CONFFILES/config" ; then
186 eerror "Error reading config file $CONFFILES/config" ; eend 1 ; exit 1
188 # restore the command line parameter value
189 CONFFILES=$_opt_confdir
193 [ "$_opt_mirror" ] && MIRROR=$_opt_mirror
194 [ "$_opt_iso" ] && ISO=$_opt_iso
195 [ "$_opt_release" ] && RELEASE=$_opt_release
196 [ "$_opt_target" ] && TARGET=$_opt_target
197 [ "$_opt_vmfile" ] && VIRTUAL=1
198 [ "$_opt_vmsize" ] && VMSIZE=$_opt_vmsize
199 [ "$_opt_mntpoint" ] && MNTPOINT=$_opt_mntpoint
200 [ "$_opt_debopt" ] && DEBOOTSTRAP_OPT=$_opt_debopt
201 [ "$_opt_interactive" ] && INTERACTIVE=1
202 [ "$_opt_config" ] && CONFIGFILE=$_opt_config
203 [ "$_opt_filesystem" ] && MKFS="mkfs.$_opt_filesystem"
204 [ "$_opt_packages_set" ] && PACKAGES='yes'
205 [ "$_opt_nopackages" ] && PACKAGES=''
206 [ "$_opt_debconf_set" ] && DEBCONF='yes'
207 [ "$_opt_scripts_set" ] && SCRIPTS='yes'
208 [ "$_opt_pre_scripts_set" ] && PRE_SCRIPTS='yes'
209 [ "$_opt_chroot_scripts_set" ] && CHROOT_SCRIPTS='yes'
210 [ "$_opt_keep_src_list" ] && KEEP_SRC_LIST='yes'
211 [ "$_opt_hostname" ] && HOSTNAME=$_opt_hostname
212 [ "$_opt_password" ] && ROOTPASSWORD=$_opt_password
213 [ "$_opt_bootappend" ] && BOOT_APPEND=$_opt_bootappend
214 [ "$_opt_grub" ] && GRUB=$_opt_grub
215 [ "$_opt_arch" ] && ARCH=$_opt_arch
216 [ "$_opt_insecure" ] && SECURE='false'
217 [ "$_opt_force" ] && FORCE=$_opt_force
218 [ "$_opt_verbose" ] && VERBOSE="-v"
220 [ "$_opt_help" ] && {
226 [ "$_opt_version" ] && {
227 einfo "$PN - version $VERSION"
228 einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
234 # check for root permissions {{{
235 if ! check4root ; then
236 echo "For usage instructions please execute '$PN --help'."
241 # make sure we have what we need {{{
242 if [ -n "$VIRTUAL" ] ; then
243 check4progs kpartx mksh qemu-img || exit 1
247 # source specified configuration file {{{
248 if [ -n "$CONFIGFILE" ] ; then
249 einfo "Reading specified config file $CONFIGFILE."
250 if ! . "$CONFIGFILE" ; then
251 eerror "Error reading config file $CONFIGFILE" ; eend 1 ; exit 1
256 # backwards compability checks {{{
257 if [ -n "$GROOT" ] ; then
258 eerror "Error: you seem to have \$GROOT configured."
259 eerror "This variable is no longer supported, please visit the"
260 eerror "grml-debootstrap documentation for details."
265 if echo "$GRUB" | grep -q '^hd' ; then
266 eerror "Error: this syntax for the grub configuration variable is no longer supported."
267 eerror "Please do not use hd... any longer but /dev/sdX instead."
276 dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
277 Do you want to continue installing Debian using this frontend?" 0 0
284 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
286 grep -v 'Extended$' | \
287 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
289 if [ -z "$AVAILABLE_PARTITIONS" ] ; then
290 dialog --title "$PN" --trim \
291 --msgbox "Sorry, no partitions found. Please configure your
292 harddisks (see /proc/partitions) using a tool like fdisk,
293 cfdisk, gpart, gparted,..." 0 0
297 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
298 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet])"
301 TARGET=$(dialog --title "$PN" --single-quoted --stdout \
302 --menu "Please select the target partition:" 0 0 0 \
307 # ask for bootmanager {{{
308 prompt_for_bootmanager()
312 if echo "$TARGET" | grep -q "/dev/md" ; then
313 MBRPART="all disks of Software RAID $TARGET"
315 # figure out whole disk device
317 for device in /dev/disk/by-id/*
319 [ $(readlink -f $device) = ${TARGET} ] || continue
323 [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
324 if [ -e "$MBRDISK" ]; then
325 MBRDISK=$(readlink -f $MBRDISK)
327 # fall back to old behaviour
328 MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$//')
331 MBRPART="MBR of $MBRDISK"
334 for device in cciss/c0d0 sda hda; do
335 if [ /dev/$device != ${MBRDISK} ]; then
336 grep -q $device /proc/partitions && \
337 ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
340 ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
344 GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
345 --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
346 mbr "install bootmanager into $MBRPART" \
347 nowhere "do not install bootmanager at all" \
348 ${ADDITIONAL_PARAMS})
349 [ $? -eq 0 ] || bailout 3
354 # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
355 if echo "$TARGET" | grep -q "/dev/md" ; then
357 if [ -n "$SELECTED_PARTITIONS" ] ; then
358 GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
379 # ask for Debian release {{{
382 [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='squeeze'
383 RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
384 "Please enter the Debian release you would like to use for installation:" \
386 lenny Debian/old-stable \
387 squeeze Debian/stable \
388 wheezy Debian/testing \
389 sid Debian/unstable)"
393 # ask for hostname {{{
394 prompt_for_hostname()
396 HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
397 "Please enter the hostname you would like to use for installation:" \
402 # ask for password {{{
403 prompt_for_password()
407 while [ "$ROOTPW1" != "$ROOTPW2" ]; do
408 ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
409 "Please enter the password for the root account:" 10 60)
410 ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
411 "Please enter the password for the root account again for \
412 confirmation:" 10 60)
414 if [ "$ROOTPW1" != "$ROOTPW2" ]; then
415 $(dialog --stdout --title "${PN}" --ok-label \
416 "Retry" --msgbox "Passwords do not match!" 10 60)
419 ROOTPASSWORD="$ROOTPW1"
423 # ask for Debian mirror {{{
426 [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
428 CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
429 --menu "Where do you want to install from?" 0 0 0 \
430 net "install via network (downloading from mirror)" \
431 local "install from local directory/mirror"
434 if [ "$CHOOSE_MIRROR" = 'net' ] ; then
435 [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
436 MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
437 "Please enter Debian mirror you would like to use for installing packages." \
439 else # CHOOSE_MIRROR == local
440 [ -n "$ISO" ] || ISO='/mnt/mirror'
441 ISO="$(dialog --stdout --title "${PN}" --inputbox \
442 "Please enter directory name you would like to use for installing packages." \
448 # software raid setup {{{
449 config_swraid_setup()
453 # Currently we support only raid1:
456 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
457 # --menu "Which RAID level do you want to use?" 0 0 0 \
458 # raid1 "Software RAID level 1" \
459 # raid5 "Software RAID level 5")
460 #[ $? -eq 0 ] || bailout 20
462 MD_LIST=$(for i in $(seq 0 9) ; do
463 awk '{print $4}' /proc/partitions | grep -q md$i || \
464 echo "/dev/md$i /dev/md$i"
467 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
468 --menu "Which device do you want to use for ${RAIDLEVEL}?
470 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 \
472 [ $? -eq 0 ] || bailout 20
474 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
476 grep -v 'Extended$' | \
477 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
478 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
479 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
480 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
483 dialog --title "$PN" --separate-output \
484 --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
485 $PARTITION_LIST 2>$TMPFILE
487 SELECTED_PARTITIONS="$(cat $TMPFILE)"
490 for i in $(cat $TMPFILE) ; do
491 NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
494 # force metadata version 0.90 for lenny so old grub can boot from this array.
496 if [ $RELEASE = "lenny" ]; then
497 METADATA_VERSION="-e0"
501 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
502 --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
504 if [ "$RC" = 0 ] ; then
505 dialog --title "$PN" --msgbox \
506 "Creating $TARGET was successful." 0 0
507 rm -f "$TMPFILE" "$ERRORFILE"
509 dialog --title "$PN" --msgbox \
510 "There was an error setting up $TARGET:
515 rm -f "$TMPFILE" "$ERRORFILE"
523 if dialog --stdout --title "$PN" \
524 --defaultno --yesno "Do you want to configure Software RAID?
526 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
532 # user should recheck his configuration {{{
533 # support full automatic installation:
535 dialog --timeout 10 --title "$PN" \
536 --yesno "Do you want to stop at this stage?
538 Notice: you are running ${PN} in non-interactive mode.
539 ${PN} will install Debian ${RELEASE} on ${TARGET}.
540 Last chance to quit. Timeout of 10 seconds running....
542 Do you want to stop now?" 0 0 2>/dev/null
546 # make sure the user is aware of the used configuration {{{
549 if [ -n "$AUTOINSTALL" ] ; then
550 if checkforrun ; then
551 eerror "Exiting as requested" ; eend 0
554 elif [ -n "$INTERACTIVE" ] ; then
556 INFOTEXT="Please recheck configuration before execution:
558 [ -n "$TARGET" ] && INFOTEXT="$INFOTEXT
560 [ -n "$GRUB" ] && INFOTEXT="$INFOTEXT
562 [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
563 Using release: $RELEASE"
564 [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
565 Using hostname $HOSTNAME"
566 [ -n "$MIRROR" ] && INFOTEXT="$INFOTEXT
567 Using mirror: $MIRROR"
568 [ -n "$ISO" ] && INFOTEXT="$INFOTEXT
573 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
575 dialog --title "$PN" --no-collapse \
576 --yesno "$INFOTEXT" 0 0
578 else # if not running automatic installation display configuration and prompt for execution:
579 einfo "$PN - Please recheck configuration before execution:"
581 echo " Target: $TARGET"
583 # do not display if MNTPOINT is the default one
584 case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo " Mount point: $MNTPOINT" ;; esac
586 if [ -n "$VIRTUAL" ] ; then
587 echo " Install grub: yes"
589 [ -n "$GRUB" ] && echo " Install grub: $GRUB" || echo " Install grub: no"
592 [ -n "$RELEASE" ] && echo " Using release: $RELEASE"
593 [ -n "$MIRROR" ] && echo " Using mirror: $MIRROR"
594 [ -n "$HOSTNAME" ] && echo " Using hostname: $HOSTNAME"
595 [ -n "$ISO" ] && echo " Using ISO: $ISO"
596 if [ -n "$VIRTUAL" ] ; then
597 echo " Deploying as Virtual Machine."
598 [ -n "$VMSIZE" ] && echo " Using Virtual Disk file with size of ${VMSIZE}."
602 echo " Important! Continuing will delete all data from ${TARGET}!"
604 if [ -n "$FORCE" ] ; then
605 einfo "Skip user acknowledgement as requested via --force option."
608 einfon "Is this ok for you? [y/N] "
610 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
611 eerror "Exiting as requested." ; eend 1
619 # interactive mode {{{
630 prompt_for_bootmanager
639 # run interactive mode if we didn't get the according configuration yet
640 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
641 # only target might be unset, so make sure the INTERACTIVE flag is set as well
649 # finally make sure at least $TARGET is set [the partition for the new system] {{{
650 if [ -n "$TARGET" ] ; then
651 SHORT_TARGET="${TARGET##*/}"
653 eerror "Please adjust $CONFFILES/config or..."
654 eerror "... use the interactive version for configuration before running ${0}" ; eend 1
660 if [ -z "$STAGES" ] ; then
661 STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
662 [ -d "$STAGES" ] || mkdir -p "$STAGES"
665 if [ -r "$STAGES"/grml-debootstrap ] ; then
666 if grep -q done $STAGES/grml-debootstrap ; then
667 eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
668 eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
673 # partition handling {{{
677 set_target_directory(){
678 # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
685 # make sure we normalise the path to an absolute directory name so something like:
686 # mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
688 TARGET="$(readlink -f $TARGET)"
691 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
698 # architecture setup {{{
699 if [ -n "$ARCH" ] ; then
700 ARCHCMD="--arch $ARCH"
701 ARCHINFO=" (${ARCH})"
703 ARCH="$(dpkg --print-architecture)"
704 ARCHCMD="--arch $ARCH"
705 ARCHINFO=" (${ARCH})"
711 if [ "$SECURE" = 'yes' ] ; then
712 if [ -e '/etc/apt/trusted.gpg' ] ; then
713 KEYRING="--keyring /etc/apt/trusted.gpg"
715 eerror "Could not find /etc/apt/trusted.gpg."
718 ewarn "Not checking Release signatures!"
722 # make sure we have the right syntax when using an iso image {{{
723 if [ -n "$ISO" ] ; then
736 # helper functions {{{
737 # we want to exit smoothly and clean:
739 # make sure $TARGET is not mounted when exiting grml-debootstrap
740 if [ -n "$MNTPOINT" ] ; then
741 if grep -q $MNTPOINT /proc/mounts ; then
742 # make sure nothing is left inside chroot so we can unmount it
743 [ -x "$MNTPOINT"/etc/init.d/ssh ] && "$MNTPOINT"/etc/init.d/ssh stop
744 [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
745 # ugly, but make sure we really don't leave anything (/proc /proc is intended)
746 for ARG in /sys /proc /proc ; do
747 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true
749 umount "$MNTPOINT"/dev >/dev/null 2>&1 || true
751 [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1 || true
753 if [ -n "$DIRECTORY" ] ; then
754 einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
756 einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
759 if [ -n "$STAGES" ] ; then
760 echo -n "Removing stages directory ${STAGES}: "
761 rm -rf "$STAGES" && echo done
764 # remove directory only if we used the default with process id inside the name
765 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
766 einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
771 if [ -n "${ORIG_TARGET}" ] ; then
772 einfo "Removing loopback mount of file ${ORIG_TARGET}."
773 kpartx -d "${ORIG_TARGET}" ; eend $?
776 [ -n "$1" ] && EXIT="$1" || EXIT="1"
777 [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
781 trap bailout HUP INT QUIT TERM
783 # we want to execute all the functions only once, simple check for it:
785 if [ -n "$2" ] ; then
786 echo "$2" > "${STAGES}/${1}"
788 elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
789 ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
790 ewarn " To reexecute it clean up the according directory inside $STAGES" ; eend 0
796 # create filesystem {{{
798 if [ -n "$DIRECTORY" ] ; then
799 einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
801 if grep -q "$TARGET" /proc/mounts ; then
802 eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
806 if [ -n "$MKFS" ] ; then
807 einfo "Running $MKFS on $TARGET"
808 $MKFS $TARGET ; RC=$?
810 # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
811 # will fail to detect the uuid in the chroot
812 if echo "$TARGET" | grep -q "/dev/md" ; then
813 blockdev --rereadpt "${TARGET}"
814 elif ! [ -n "$VIRTUAL" ] ; then
815 blockdev --rereadpt "${TARGET%%[0-9]*}"
817 # give the system 2 seconds, otherwise we might run into
818 # race conditions :-/
821 eval $(blkid -o udev $TARGET 2>/dev/null)
822 [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
831 # modify filesystem settings {{{
833 if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
834 einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
841 # mount the new partition or if it's a directory do nothing at all {{{
843 if [ -n "$DIRECTORY" ] ; then
844 einfo "Running grml-debootstrap on a directory, nothing to mount."
846 if grep -q $TARGET /proc/mounts ; then
847 ewarn "$TARGET already mounted, continuing anyway." ; eend 0
849 if ! [ -d "${MNTPOINT}" ] ; then
850 [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
852 einfo "Mounting $TARGET to $MNTPOINT"
854 mount -o rw,suid,dev $TARGET $MNTPOINT
858 if [ -n "$ISODIR" ] ; then
859 einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
860 mkdir -p "$MNTPOINT/$ISODIR"
861 mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
867 # prepare VM image for usage with debootstrap {{{
869 if [ -z "$VIRTUAL" ] ; then
870 return 0 # be quite by intention
873 if [ -b "$TARGET" ] ; then
874 eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
879 ORIG_TARGET="$TARGET" # store for later reuse
881 qemu-img create -f raw "${TARGET}" "${VMSIZE}"
882 echo 4 66 | mksh /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
883 dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
884 parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
886 DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
887 if [ -z "${DEVINFO}" ] ; then
888 eerror "Error setting up loopback device." ; eend 1
892 # hopefully this always works as expected
893 LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
894 BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/') # '253:0'
895 LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
896 export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
898 blockdev --rereadpt "${LOOP}"
900 if [ -z "$TARGET" ] ; then
901 eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
907 # make VM image bootable and unmount it {{{
909 if [ -z "${VIRTUAL}" ] ; then
913 if ! mount "${TARGET}" "${MNTPOINT}" ; then
914 eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
918 einfo "Installing Grub as bootloader."
919 mount -t proc none "${MNTPOINT}"/proc
920 mount -t sysfs none "${MNTPOINT}"/sys
921 mount --bind /dev "${MNTPOINT}"/dev
923 mkdir -p "${MNTPOINT}/boot/grub"
924 if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
925 eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
929 cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
930 chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
931 dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
932 rm -f "${MNTPOINT}/tmp/core.img"
934 einfo "Updating grub configuration file."
935 chroot "${MNTPOINT}" update-grub
937 umount "${MNTPOINT}"/proc
938 umount "${MNTPOINT}"/sys
939 umount "${MNTPOINT}"/dev
941 einfo "Adjusting grub.cfg for successful boot sequence."
942 # ugly but needed to boot grub acordingly
943 sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
944 sed -i "s; root=/dev/.*; root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
947 kpartx -d "${ORIG_TARGET}" >/dev/null
951 # install main chroot {{{
952 debootstrap_system() {
953 if [ "$_opt_nodebootstrap" ]; then
954 einfo "Skipping debootstrap as requested."
958 if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
959 einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
960 if [ -n "$MIRROR" ] ; then
961 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
962 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
964 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
965 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
969 eerror "Error: $MNTPOINT not mounted, can not continue."
975 # prepare chroot via chroot-script {{{
977 einfo "Preparing chroot system"
979 # provide variables to chroot system
980 CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
981 touch $CHROOT_VARIABLES
982 chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
983 echo "# Configuration of ${PN}" > $CHROOT_VARIABLES
984 [ -n "$ARCH" ] && echo "ARCH=$ARCH" >> $CHROOT_VARIABLES
985 [ -n "$GRUB" ] && echo "GRUB=$GRUB" >> $CHROOT_VARIABLES
986 [ -n "$HOSTNAME" ] && echo "HOSTNAME=$HOSTNAME" >> $CHROOT_VARIABLES
987 [ -n "$INSTALL_NOTES" ] && echo "INSTALL_NOTES=$INSTALL_NOTES" >> $CHROOT_VARIABLES
988 [ -n "$ISODIR" ] && echo "ISODIR=$ISO" >> $CHROOT_VARIABLES
989 [ -n "$ISO" ] && echo "ISO=$ISO" >> $CHROOT_VARIABLES
990 [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=$KEEP_SRC_LIST" >> $CHROOT_VARIABLES
991 [ -n "$MIRROR" ] && echo "MIRROR=$MIRROR" >> $CHROOT_VARIABLES
992 [ -n "$PACKAGES" ] && echo "PACKAGES=$PACKAGES" >> $CHROOT_VARIABLES
993 [ -n "$RM_APTCACHE" ] && echo "RM_APTCACHE=$RM_APTCACHE" >> $CHROOT_VARIABLES
994 [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
995 [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=$SELECTED_PARTITIONS" >> $CHROOT_VARIABLES
996 [ -n "$TARGET" ] && echo "TARGET=$TARGET" >> $CHROOT_VARIABLES
997 [ -n "$TARGET_UUID" ] && echo "TARGET_UUID=$TARGET_UUID" >> $CHROOT_VARIABLES
999 cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
1000 chmod 755 $MNTPOINT/bin/chroot-script
1001 [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
1003 # make sure we have our files for later use via chroot-script
1004 cp $VERBOSE $CONFFILES/config $MNTPOINT/etc/debootstrap/
1005 # make sure we adjust the configuration variables accordingly:
1006 sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
1007 sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#" $MNTPOINT/etc/debootstrap/config
1008 sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#" $MNTPOINT/etc/debootstrap/config
1011 if [ -n "$INSTALL_NOTES" ] ; then
1012 [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
1015 # package selection:
1016 cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
1017 $MNTPOINT/etc/debootstrap/packages
1019 # debconf preseeding:
1020 _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
1021 [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
1022 cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
1024 # copy scripts that should be executed inside the chroot:
1025 _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
1026 [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
1027 mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
1028 cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
1031 # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
1032 cp $VERBOSE $CHROOT_VARIABLES $MNTPOINT/etc/debootstrap/variables
1034 cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
1036 # make sure we can access network [relevant for cdebootstrap]
1037 [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
1039 # provide system's /etc/hosts to the target:
1040 if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
1041 cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
1044 # setup default locales
1045 [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen $MNTPOINT/etc/locale.gen
1047 # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
1048 ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
1050 # copy any existing files to chroot
1051 [ -d $CONFFILES/bin ] && cp $VERBOSE -a -L $CONFFILES/bin/* $MNTPOINT/bin/
1052 [ -d $CONFFILES/boot ] && cp $VERBOSE -a -L $CONFFILES/boot/* $MNTPOINT/boot/
1053 [ -d $CONFFILES/etc ] && cp $VERBOSE -a -L $CONFFILES/etc/* $MNTPOINT/etc/
1054 [ -d $CONFFILES/sbin ] && cp $VERBOSE -a -L $CONFFILES/sbin/* $MNTPOINT/sbin/
1055 [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
1056 [ -d $CONFFILES/usr ] && cp $VERBOSE -a -L $CONFFILES/usr/* $MNTPOINT/usr/
1057 [ -d $CONFFILES/var ] && cp $VERBOSE -a -L $CONFFILES/var/* $MNTPOINT/var/
1059 # copy local network setup to chroot
1060 if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
1061 [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
1062 cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
1065 # install config file providing some example entries
1066 if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1067 cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1074 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1075 execute_pre_scripts() {
1076 # make sure we have $MNTPOINT available for our scripts
1078 if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1079 [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1080 for script in ${pre_scripts}/* ; do
1081 if [ -x "$script" ] ; then
1082 einfo "Executing pre-script $script"
1090 # execute all scripts in /etc/debootstrap/scripts/ {{{
1092 # make sure we have $MNTPOINT available for our scripts
1094 if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1095 [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1096 for script in ${scripts}/* ; do
1097 if [ -x "$script" ] ; then
1098 einfo "Executing script $script"
1106 # execute chroot-script {{{
1108 if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1112 if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1113 einfo "Executing chroot-script now"
1114 mount --bind /dev "$MNTPOINT"/dev
1115 chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1116 umount "$MNTPOINT"/dev
1119 eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1125 # unmount $MNTPOINT {{{
1128 # display installation notes:
1129 if [ -n "$INSTALL_NOTES" ] ; then
1130 [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1133 if [ -n "$ISODIR" ] ; then
1134 if grep -q "$ISODIR" /proc/mounts ; then
1135 einfo "Unmount $MNTPOINT/$ISODIR"
1136 umount "$MNTPOINT/$ISODIR"
1141 if grep -q "$MNTPOINT" /proc/mounts ; then
1142 if [ -n "$PARTITION" ] ; then
1143 einfo "Unmount $MNTPOINT"
1151 # execute filesystem check {{{
1153 if [ "$FSCK" = 'yes' ] ; then
1154 [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1155 einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1162 # now execute all the functions {{{
1163 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1164 preparechroot execute_pre_scripts chrootscript execute_scripts \
1165 umount_chroot finalize_vm fscktool ; do
1166 if stage "${i}" ; then
1167 $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
1173 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
1174 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
1176 # Remove temporary mountpoint again
1177 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
1178 einfo "Removing directory ${MNTPOINT}" ; rmdir "$MNTPOINT" ; eend $?
1182 # end dialog of autoinstallation {{{
1183 if [ -n "$AUTOINSTALL" ] ; then
1184 if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1185 Automatically rebooting in 10 seconds.
1187 Choose Cancel to skip rebooting." 10 60 10 ; then
1188 noeject noprompt reboot
1191 einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1195 ## END OF FILE #################################################################
1196 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=3