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 --insecure Do not download and check Release file signatures.
75 --force Do not prompt for user acknowledgement.
77 Options for Virtual Machine deployment:
79 --vmfile Set up a Virtual Machine instead of installing to
80 a partition or directory, to be combined with --target,
81 like: --vmfile --target /mnt/sda1/qemu.img
82 --vmsize <size> Use specified size for size of VM file (default: 2G).
83 Syntax as supported by qemu-img, like: --vmsize 3G
85 Configuration options:
87 -c, --config <file> Use specified configuration file, defaults to
88 /etc/debootstrap/config
89 -d, --confdir <path> Place of config files for debootstrap, defaults
91 --packages <file> Install packages defined in specified list file
92 instead of using /etc/debootstrap/packages.
93 --nopackages Skip installation of packages defined in
94 /etc/debootstrap/packages
95 --debconf <file> Pre-seed packages using specified pre-seed db file.
96 --grmlrepos Enable Grml's Debian repository (deb.grml.org).
97 --keep_src_list Do not overwrite user provided apt sources.list.
98 --hostname <name> Hostname of Debian system.
99 --nopassword Do not prompt for the root password.
100 --password <pwd> Use specified password as password for user root.
101 --bootappend <line> Add specified appendline to kernel whilst booting.
102 --chroot-scripts <d> Execute chroot scripts from specified directory.
103 --pre-scripts <dir> Execute scripts from specified directory (before chroot-scripts).
104 --scripts <dir> Execute scripts from specified directory (after chroot-scripts).
108 -v, --verbose Increase verbosity.
109 -h, --help Print this usage information and exit.
110 -V, --version Show summary of options and exit.
112 Usage examples can be found in the grml-debootstrap manpage.
113 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
117 if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
119 echo 'Please notice that this script requires root permissions!'
124 # early helper functions {{{
138 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
139 printf " ${GOOD}*${NORMAL} $*"
145 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
146 printf " ${BAD}*${NORMAL} $*\n"
152 local retval="${1:-0}"
154 if [ $retval -gt 0 ]; then
155 printf " ${BAD}-> Failed (rc=${retval})${NORMAL}\n"
161 if [ "$(id -u 2>/dev/null)" != 0 ] ; then
162 echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
169 which $arg >/dev/null 2>&1 || RC="$arg"
171 if [ -n "$RC" ] ; then
172 echo "$RC not installed"
178 # helper functions {{{
179 # we want to exit smoothly and clean:
181 # make sure $TARGET is not mounted when exiting grml-debootstrap
182 if [ -n "$MNTPOINT" ] ; then
183 if grep -q $MNTPOINT /proc/mounts ; then
184 # make sure nothing is left inside chroot so we can unmount it
185 [ -x "$MNTPOINT"/etc/init.d/ssh ] && "$MNTPOINT"/etc/init.d/ssh stop
186 [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
187 # ugly, but make sure we really don't leave anything (/proc /proc is intended)
188 for ARG in /sys /proc /proc ; do
189 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true
191 umount "$MNTPOINT"/dev >/dev/null 2>&1 || true
193 [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1 || true
195 if [ -n "$DIRECTORY" ] ; then
196 einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
198 einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
201 if [ -n "$STAGES" ] ; then
202 echo -n "Removing stages directory ${STAGES}: "
203 rm -rf "$STAGES" && echo done
206 # remove directory only if we used the default with process id inside the name
207 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
208 einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
213 if [ -n "${ORIG_TARGET}" ] ; then
214 einfo "Removing loopback mount of file ${ORIG_TARGET}."
215 kpartx -d "${ORIG_TARGET}" ; eend $?
218 [ -n "$1" ] && EXIT="$1" || EXIT="1"
219 [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
223 trap bailout HUP INT QUIT TERM
225 # we want to execute all the functions only once, simple check for it:
227 if [ -n "$2" ] ; then
228 echo "$2" > "${STAGES}/${1}"
230 elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
231 ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
232 ewarn " To reexecute it clean up the according directory inside $STAGES" ; eend 0
238 # make sure we have what we need {{{
239 check4progs debootstrap dialog || exit 1
242 # source main configuration file {{{
243 if [ -r /etc/debootstrap/config ] ; then
244 . /etc/debootstrap/config
248 # cmdline handling {{{
249 # source external command line parameter-processing script
250 if [ -r ./cmdlineopts.clp ] ; then
252 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
253 . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
255 eerror "Error: cmdline function file not found, exiting."
260 # == business-logic of command line parameter-processing
262 # source configuration file in <confdir> if supplied. {{{
263 [ "$_opt_confdir" ] && {
264 CONFFILES=$_opt_confdir
265 einfo "Using config files under $CONFFILES/."
266 if ! [ -r "$CONFFILES/config" ] ; then
267 eerror "Error: config file $CONFFILES/config not found."; eend 1; exit 1
269 if ! . "$CONFFILES/config" ; then
270 eerror "Error reading config file $CONFFILES/config" ; eend 1 ; exit 1
272 # restore the command line parameter value
273 CONFFILES=$_opt_confdir
277 [ "$_opt_mirror" ] && MIRROR=$_opt_mirror
278 [ "$_opt_iso" ] && ISO=$_opt_iso
279 [ "$_opt_release" ] && RELEASE=$_opt_release
280 [ "$_opt_target" ] && TARGET=$_opt_target
281 [ "$_opt_vmfile" ] && VIRTUAL=1
282 [ "$_opt_vmsize" ] && VMSIZE=$_opt_vmsize
283 [ "$_opt_mntpoint" ] && MNTPOINT=$_opt_mntpoint
284 [ "$_opt_debopt" ] && DEBOOTSTRAP_OPT=$_opt_debopt
285 [ "$_opt_interactive" ] && INTERACTIVE=1
286 [ "$_opt_config" ] && CONFIGFILE=$_opt_config
287 [ "$_opt_filesystem" ] && MKFS="mkfs.$_opt_filesystem"
288 [ "$_opt_packages_set" ] && PACKAGES='yes'
289 [ "$_opt_nopackages" ] && PACKAGES=''
290 [ "$_opt_debconf_set" ] && DEBCONF='yes'
291 [ "$_opt_scripts_set" ] && SCRIPTS='yes'
292 [ "$_opt_pre_scripts_set" ] && PRE_SCRIPTS='yes'
293 [ "$_opt_chroot_scripts_set" ] && CHROOT_SCRIPTS='yes'
294 [ "$_opt_keep_src_list" ] && KEEP_SRC_LIST='yes'
295 [ "$_opt_grmlrepos" ] && GRMLREPOS='yes'
296 [ "$_opt_hostname" ] && HOSTNAME=$_opt_hostname
297 [ "$_opt_password" ] && ROOTPASSWORD=$_opt_password
298 [ "$_opt_nopassword" ] && NOPASSWORD='yes'
299 [ "$_opt_bootappend" ] && BOOT_APPEND=$_opt_bootappend
300 [ "$_opt_grub" ] && GRUB=$_opt_grub
301 [ "$_opt_arch" ] && ARCH=$_opt_arch
302 [ "$_opt_insecure" ] && SECURE='false'
303 [ "$_opt_force" ] && FORCE=$_opt_force
304 [ "$_opt_verbose" ] && VERBOSE="-v"
306 [ "$_opt_help" ] && {
312 [ "$_opt_version" ] && {
313 einfo "$PN - version $VERSION"
314 einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
320 # check for root permissions {{{
321 if ! check4root ; then
322 echo "For usage instructions please execute '$PN --help'."
327 # make sure we have what we need {{{
328 if [ -n "$VIRTUAL" ] ; then
329 check4progs kpartx mksh qemu-img || exit 1
333 # source specified configuration file {{{
334 if [ -n "$CONFIGFILE" ] ; then
335 einfo "Reading specified config file $CONFIGFILE."
336 if ! . "$CONFIGFILE" ; then
337 eerror "Error reading config file $CONFIGFILE" ; eend 1 ; exit 1
342 # backwards compability checks {{{
343 if [ -n "$GROOT" ] ; then
344 eerror "Error: you seem to have \$GROOT configured."
345 eerror "This variable is no longer supported, please visit the"
346 eerror "grml-debootstrap documentation for details."
351 if echo "$GRUB" | grep -q '^hd' ; then
352 eerror "Error: this syntax for the grub configuration variable is no longer supported."
353 eerror "Please do not use hd... any longer but /dev/sdX instead."
362 dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
363 Do you want to continue installing Debian using this frontend?" 0 0 || bailout 0
370 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
372 grep -v 'Extended$' | \
373 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
375 if [ -z "$AVAILABLE_PARTITIONS" ] ; then
376 dialog --title "$PN" --trim \
377 --msgbox "Sorry, no partitions found. Please configure your
378 harddisks (see /proc/partitions) using a tool like fdisk,
379 cfdisk, gpart, gparted,..." 0 0
383 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
384 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet])"
387 TARGET=$(dialog --title "$PN" --single-quoted --stdout \
388 --menu "Please select the target partition:" 0 0 0 \
390 [ $? -eq 0 ] || bailout
394 # ask for bootmanager {{{
395 prompt_for_bootmanager()
399 if echo "$TARGET" | grep -q "/dev/md" ; then
400 MBRPART="all disks of Software RAID $TARGET"
402 # figure out whole disk device
404 for device in /dev/disk/by-id/*
406 [ $(readlink -f $device) = ${TARGET} ] || continue
410 [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
411 if [ -e "$MBRDISK" ]; then
412 MBRDISK=$(readlink -f $MBRDISK)
414 # fall back to old behaviour
415 MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$//')
418 MBRPART="MBR of $MBRDISK"
421 for device in cciss/c0d0 sda hda; do
422 if [ /dev/$device != ${MBRDISK} ]; then
423 grep -q $device /proc/partitions && \
424 ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
427 ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
431 GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
432 --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
433 mbr "install bootmanager into $MBRPART" \
434 nowhere "do not install bootmanager at all" \
435 ${ADDITIONAL_PARAMS})
436 [ $? -eq 0 ] || bailout 3
441 # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
442 if echo "$TARGET" | grep -q "/dev/md" ; then
444 if [ -n "$SELECTED_PARTITIONS" ] ; then
445 GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
466 # ask for Debian release {{{
469 [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='squeeze'
470 RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
471 "Please enter the Debian release you would like to use for installation:" \
473 lenny Debian/old-stable \
474 squeeze Debian/stable \
475 wheezy Debian/testing \
476 sid Debian/unstable)"
477 [ $? -eq 0 ] || bailout
481 # ask for hostname {{{
482 prompt_for_hostname()
484 HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
485 "Please enter the hostname you would like to use for installation:" \
487 [ $? -eq 0 ] || bailout
491 # ask for password {{{
492 prompt_for_password()
494 if [ "$_opt_nopassword" ] ; then
495 einfo "Skip asking for root password as requested."
501 while [ "$ROOTPW1" != "$ROOTPW2" ]; do
502 ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
503 "Please enter the password for the root account:" 10 60)
504 [ $? -eq 0 ] || bailout
505 ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
506 "Please enter the password for the root account again for \
507 confirmation:" 10 60)
508 [ $? -eq 0 ] || bailout
510 if [ "$ROOTPW1" != "$ROOTPW2" ]; then
511 $(dialog --stdout --title "${PN}" --ok-label \
512 "Retry" --msgbox "Passwords do not match!" 10 60)
515 ROOTPASSWORD="$ROOTPW1"
519 # ask for Debian mirror {{{
522 [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
524 CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
525 --menu "Where do you want to install from?" 0 0 0 \
526 net "install via network (downloading from mirror)" \
527 local "install from local directory/mirror"
529 [ $? -eq 0 ] || bailout
531 if [ "$CHOOSE_MIRROR" = 'net' ] ; then
532 [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
533 MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
534 "Please enter Debian mirror you would like to use for installing packages." \
536 [ $? -eq 0 ] || bailout
537 else # CHOOSE_MIRROR == local
538 [ -n "$ISO" ] || ISO='/mnt/mirror'
539 ISO="$(dialog --stdout --title "${PN}" --inputbox \
540 "Please enter directory name you would like to use for installing packages." \
542 [ $? -eq 0 ] || bailout
547 # software raid setup {{{
548 config_swraid_setup()
552 # Currently we support only raid1:
555 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
556 # --menu "Which RAID level do you want to use?" 0 0 0 \
557 # raid1 "Software RAID level 1" \
558 # raid5 "Software RAID level 5")
559 #[ $? -eq 0 ] || bailout 20
561 MD_LIST=$(for i in $(seq 0 9) ; do
562 awk '{print $4}' /proc/partitions | grep -q md$i || \
563 echo "/dev/md$i /dev/md$i"
566 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
567 --menu "Which device do you want to use for ${RAIDLEVEL}?
569 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 \
571 [ $? -eq 0 ] || bailout 20
573 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
575 grep -v 'Extended$' | \
576 gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
577 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
578 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
579 echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
582 dialog --title "$PN" --separate-output \
583 --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
584 $PARTITION_LIST 2>$TMPFILE
585 [ $? -eq 0 ] || bailout
587 SELECTED_PARTITIONS="$(cat $TMPFILE)"
590 for i in $(cat $TMPFILE) ; do
591 NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
594 # force metadata version 0.90 for lenny so old grub can boot from this array.
596 if [ $RELEASE = "lenny" ]; then
597 METADATA_VERSION="-e0"
601 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
602 --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
604 if [ "$RC" = 0 ] ; then
605 dialog --title "$PN" --msgbox \
606 "Creating $TARGET was successful." 0 0
607 rm -f "$TMPFILE" "$ERRORFILE"
609 dialog --title "$PN" --msgbox \
610 "There was an error setting up $TARGET:
615 rm -f "$TMPFILE" "$ERRORFILE"
623 if dialog --stdout --title "$PN" \
624 --defaultno --yesno "Do you want to configure Software RAID?
626 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
632 # user should recheck his configuration {{{
633 # support full automatic installation:
635 dialog --timeout 10 --title "$PN" \
636 --yesno "Do you want to stop at this stage?
638 Notice: you are running ${PN} in non-interactive mode.
639 ${PN} will install Debian ${RELEASE} on ${TARGET}.
640 Last chance to quit. Timeout of 10 seconds running....
642 Do you want to stop now?" 0 0 2>/dev/null
646 # make sure the user is aware of the used configuration {{{
649 if [ -n "$AUTOINSTALL" ] ; then
650 if checkforrun ; then
651 eerror "Exiting as requested" ; eend 0
654 elif [ -n "$INTERACTIVE" ] ; then
656 INFOTEXT="Please recheck configuration before execution:
658 [ -n "$TARGET" ] && INFOTEXT="$INFOTEXT
660 [ -n "$GRUB" ] && INFOTEXT="$INFOTEXT
662 [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
663 Using release: $RELEASE"
664 [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
665 Using hostname $HOSTNAME"
666 [ -n "$MIRROR" ] && INFOTEXT="$INFOTEXT
667 Using mirror: $MIRROR"
668 [ -n "$ISO" ] && INFOTEXT="$INFOTEXT
673 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
675 dialog --title "$PN" --no-collapse \
676 --yesno "$INFOTEXT" 0 0
677 [ $? -eq 0 ] || bailout 0
679 else # if not running automatic installation display configuration and prompt for execution:
680 einfo "$PN - Please recheck configuration before execution:"
682 echo " Target: $TARGET"
684 # do not display if MNTPOINT is the default one
685 case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo " Mount point: $MNTPOINT" ;; esac
687 if [ -n "$VIRTUAL" ] ; then
688 echo " Install grub: yes"
690 [ -n "$GRUB" ] && echo " Install grub: $GRUB" || echo " Install grub: no"
693 [ -n "$RELEASE" ] && echo " Using release: $RELEASE"
694 [ -n "$MIRROR" ] && echo " Using mirror: $MIRROR"
695 [ -n "$HOSTNAME" ] && echo " Using hostname: $HOSTNAME"
696 [ -n "$ISO" ] && echo " Using ISO: $ISO"
697 if [ -n "$VIRTUAL" ] ; then
698 echo " Deploying as Virtual Machine."
699 [ -n "$VMSIZE" ] && echo " Using Virtual Disk file with size of ${VMSIZE}."
703 echo " Important! Continuing will delete all data from ${TARGET}!"
705 if [ -n "$FORCE" ] ; then
706 einfo "Skip user acknowledgement as requested via --force option."
709 einfon "Is this ok for you? [y/N] "
711 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
712 eerror "Exiting as requested." ; eend 1
720 # interactive mode {{{
731 prompt_for_bootmanager
740 # run interactive mode if we didn't get the according configuration yet
741 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
742 # only target might be unset, so make sure the INTERACTIVE flag is set as well
750 # finally make sure at least $TARGET is set [the partition for the new system] {{{
751 if [ -n "$TARGET" ] ; then
752 SHORT_TARGET="${TARGET##*/}"
754 eerror "Please adjust $CONFFILES/config or..."
755 eerror "... use the interactive version for configuration before running ${0}" ; eend 1
761 if [ -z "$STAGES" ] ; then
762 STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
763 [ -d "$STAGES" ] || mkdir -p "$STAGES"
766 if [ -r "$STAGES"/grml-debootstrap ] ; then
767 if grep -q done $STAGES/grml-debootstrap ; then
768 eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
769 eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
774 # partition handling {{{
778 set_target_directory(){
779 # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
786 # make sure we normalise the path to an absolute directory name so something like:
787 # mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
789 TARGET="$(readlink -f $TARGET)"
792 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
799 # architecture setup {{{
800 if [ -n "$ARCH" ] ; then
801 ARCHCMD="--arch $ARCH"
802 ARCHINFO=" (${ARCH})"
804 ARCH="$(dpkg --print-architecture)"
805 ARCHCMD="--arch $ARCH"
806 ARCHINFO=" (${ARCH})"
812 if [ "$SECURE" = 'yes' ] ; then
813 if [ -e '/etc/apt/trusted.gpg' ] ; then
814 KEYRING="--keyring /etc/apt/trusted.gpg"
816 eerror "Could not find /etc/apt/trusted.gpg."
819 ewarn "Not checking Release signatures!"
823 # make sure we have the right syntax when using an iso image {{{
824 if [ -n "$ISO" ] ; then
837 # create filesystem {{{
839 if [ -n "$DIRECTORY" ] ; then
840 einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
842 if grep -q "$TARGET" /proc/mounts ; then
843 eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
847 if [ -n "$MKFS" ] ; then
848 einfo "Running $MKFS on $TARGET"
849 $MKFS $TARGET ; RC=$?
851 # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
852 # will fail to detect the uuid in the chroot
853 if echo "$TARGET" | grep -q "/dev/md" ; then
854 blockdev --rereadpt "${TARGET}"
855 elif ! [ -n "$VIRTUAL" ] ; then
856 blockdev --rereadpt "${TARGET%%[0-9]*}"
858 # give the system 2 seconds, otherwise we might run into
859 # race conditions :-/
862 eval $(blkid -o udev $TARGET 2>/dev/null)
863 [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
872 # modify filesystem settings {{{
874 if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
875 einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
882 # mount the new partition or if it's a directory do nothing at all {{{
884 if [ -n "$DIRECTORY" ] ; then
885 einfo "Running grml-debootstrap on a directory, nothing to mount."
887 if grep -q $TARGET /proc/mounts ; then
888 ewarn "$TARGET already mounted, continuing anyway." ; eend 0
890 if ! [ -d "${MNTPOINT}" ] ; then
891 [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
893 einfo "Mounting $TARGET to $MNTPOINT"
895 mount -o rw,suid,dev $TARGET $MNTPOINT
899 if [ -n "$ISODIR" ] ; then
900 einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
901 mkdir -p "$MNTPOINT/$ISODIR"
902 mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
908 # prepare VM image for usage with debootstrap {{{
910 if [ -z "$VIRTUAL" ] ; then
911 return 0 # be quite by intention
914 if [ -b "$TARGET" ] ; then
915 eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
920 ORIG_TARGET="$TARGET" # store for later reuse
922 qemu-img create -f raw "${TARGET}" "${VMSIZE}"
923 echo 4 66 | /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
924 dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
925 parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
927 DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
928 if [ -z "${DEVINFO}" ] ; then
929 eerror "Error setting up loopback device." ; eend 1
933 # hopefully this always works as expected
934 LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
935 BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/') # '253:0'
936 LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
937 export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
939 blockdev --rereadpt "${LOOP}"
941 if [ -z "$TARGET" ] ; then
942 eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
948 # make VM image bootable and unmount it {{{
950 if [ -z "${VIRTUAL}" ] ; then
954 if ! mount "${TARGET}" "${MNTPOINT}" ; then
955 eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
959 einfo "Installing Grub as bootloader."
960 mount -t proc none "${MNTPOINT}"/proc
961 mount -t sysfs none "${MNTPOINT}"/sys
962 mount --bind /dev "${MNTPOINT}"/dev
964 mkdir -p "${MNTPOINT}/boot/grub"
965 if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
966 eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
970 cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
971 chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
972 dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
973 rm -f "${MNTPOINT}/tmp/core.img"
975 einfo "Updating grub configuration file."
976 chroot "${MNTPOINT}" update-grub
978 umount "${MNTPOINT}"/proc
979 umount "${MNTPOINT}"/sys
980 umount "${MNTPOINT}"/dev
982 einfo "Adjusting grub.cfg for successful boot sequence."
983 # ugly but needed to boot grub acordingly
984 sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
985 sed -i "s; root=/dev/.*; root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
988 kpartx -d "${ORIG_TARGET}" >/dev/null
992 # install main chroot {{{
993 debootstrap_system() {
994 if [ "$_opt_nodebootstrap" ]; then
995 einfo "Skipping debootstrap as requested."
999 if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
1000 einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
1001 if [ -n "$MIRROR" ] ; then
1002 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
1003 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
1005 einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
1006 $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
1010 eerror "Error: $MNTPOINT not mounted, can not continue."
1016 # prepare chroot via chroot-script {{{
1018 einfo "Preparing chroot system"
1020 # provide variables to chroot system
1021 CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
1022 touch $CHROOT_VARIABLES
1023 chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
1024 echo "# Configuration of ${PN}" > $CHROOT_VARIABLES
1025 [ -n "$ARCH" ] && echo "ARCH=\"$ARCH\"" >> $CHROOT_VARIABLES
1026 [ -n "$CHROOT_SCRIPTS" ] && echo "CHROOT_SCRIPTS=\"$CHROOT_SCRIPTS\"" >> $CHROOT_VARIABLES
1027 [ -n "$CONFFILES" ] && echo "CONFFILES=\"$CONFFILES\"" >> $CHROOT_VARIABLES
1028 [ -n "$DEBCONF" ] && echo "DEBCONF=\"$DEBCONF\"" >> $CHROOT_VARIABLES
1029 [ -n "$DEBIAN_FRONTEND" ] && echo "DEBIAN_FRONTEND=\"$DEBIAN_FRONTEND\"" >> $CHROOT_VARIABLES
1030 [ -n "$DEBOOTSTRAP" ] && echo "DEBOOTSTRAP=\"$DEBOOTSTRAP\"" >> $CHROOT_VARIABLES
1031 [ -n "$EXTRAPACKAGES" ] && echo "EXTRAPACKAGES=\"$EXTRAPACKAGES\"" >> $CHROOT_VARIABLES
1032 [ -n "$FORCE" ] && echo "FORCE=\"$FORCE\"" >> $CHROOT_VARIABLES
1033 [ -n "$GRMLREPOS" ] && echo "GRMLREPOS=\"$GRMLREPOS\"" >> $CHROOT_VARIABLES
1034 [ -n "$GRUB" ] && echo "GRUB=\"$GRUB\"" >> $CHROOT_VARIABLES
1035 [ -n "$HOSTNAME" ] && echo "HOSTNAME=\"$HOSTNAME\"" >> $CHROOT_VARIABLES
1036 [ -n "$INITRD" ] && echo "INITRD=\"$INITRD\"" >> $CHROOT_VARIABLES
1037 [ -n "$INSTALL_NOTES" ] && echo "INSTALL_NOTES=\"$INSTALL_NOTES\"" >> $CHROOT_VARIABLES
1038 [ -n "$ISODIR" ] && echo "ISODIR=\"$ISO\"" >> $CHROOT_VARIABLES
1039 [ -n "$ISO" ] && echo "ISO=\"$ISO\"" >> $CHROOT_VARIABLES
1040 [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=\"$KEEP_SRC_LIST\"" >> $CHROOT_VARIABLES
1041 [ -n "$LOCALES" ] && echo "LOCALES=\"$LOCALES\"" >> $CHROOT_VARIABLES
1042 [ -n "$MIRROR" ] && echo "MIRROR=\"$MIRROR\"" >> $CHROOT_VARIABLES
1043 [ -n "$MKFS" ] && echo "MKFS=\"$MKFS\"" >> $CHROOT_VARIABLES
1044 [ -n "$NOPASSWORD" ] && echo "NOPASSWORD=\"true\"" >> $CHROOT_VARIABLES
1045 [ -n "$PACKAGES" ] && echo "PACKAGES=\"$PACKAGES\"" >> $CHROOT_VARIABLES
1046 [ -n "$PRE_SCRIPTS" ] && echo "PRE_SCRIPTS=\"$PRE_SCRIPTS\"" >> $CHROOT_VARIABLES
1047 [ -n "$RECONFIGURE" ] && echo "RECONFIGURE=\"$RECONFIGURE\"" >> $CHROOT_VARIABLES
1048 [ -n "$RELEASE" ] && echo "RELEASE=\"$RELEASE\"" >> $CHROOT_VARIABLES
1049 [ -n "$RM_APTCACHE" ] && echo "RM_APTCACHE=\"$RM_APTCACHE\"" >> $CHROOT_VARIABLES
1050 [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=\"$ROOTPASSWORD\"" >> $CHROOT_VARIABLES
1051 [ -n "$SCRIPTS" ] && echo "SCRIPTS=\"$SCRIPTS\"" >> $CHROOT_VARIABLES
1052 [ -n "$SECURE" ] && echo "SECURE=\"$SECURE\"" >> $CHROOT_VARIABLES
1053 [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=\"$SELECTED_PARTITIONS\"" >> $CHROOT_VARIABLES
1054 [ -n "$TARGET" ] && echo "TARGET=\"$TARGET\"" >> $CHROOT_VARIABLES
1055 [ -n "$UPGRADE_SYSTEM" ] && echo "UPGRADE_SYSTEM=\"$UPGRADE_SYSTEM\"" >> $CHROOT_VARIABLES
1056 [ -n "$TARGET_UUID" ] && echo "TARGET_UUID=\"$TARGET_UUID\"" >> $CHROOT_VARIABLES
1057 [ -n "$TIMEZONE" ] && echo "TIMEZONE=\"$TIMEZONE\"" >> $CHROOT_VARIABLES
1058 [ -n "$TUNE2FS" ] && echo "TUNE2FS=\"$TUNE2FS\"" >> $CHROOT_VARIABLES
1059 [ -n "$VMSIZE" ] && echo "VMSIZE=\"$VMSIZE\"" >> $CHROOT_VARIABLES
1061 cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
1062 chmod 755 $MNTPOINT/bin/chroot-script
1063 [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
1065 # make sure we have our files for later use via chroot-script
1066 cp $VERBOSE $CONFFILES/config $MNTPOINT/etc/debootstrap/
1067 # make sure we adjust the configuration variables accordingly:
1068 sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
1069 sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#" $MNTPOINT/etc/debootstrap/config
1070 sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#" $MNTPOINT/etc/debootstrap/config
1073 if [ -n "$INSTALL_NOTES" ] ; then
1074 [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
1077 # package selection:
1078 cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
1079 $MNTPOINT/etc/debootstrap/packages
1081 # debconf preseeding:
1082 _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
1083 [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
1084 cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
1086 # copy scripts that should be executed inside the chroot:
1087 _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
1088 [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
1089 mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
1090 cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
1093 # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
1094 cp $VERBOSE $CHROOT_VARIABLES $MNTPOINT/etc/debootstrap/variables
1096 cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
1098 # make sure we can access network [relevant for cdebootstrap]
1099 [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
1101 # provide system's /etc/hosts to the target:
1102 if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
1103 cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
1106 # setup default locales
1107 [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen $MNTPOINT/etc/locale.gen
1109 # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
1110 ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
1112 # copy any existing files to chroot
1113 [ -d $CONFFILES/bin ] && cp $VERBOSE -a -L $CONFFILES/bin/* $MNTPOINT/bin/
1114 [ -d $CONFFILES/boot ] && cp $VERBOSE -a -L $CONFFILES/boot/* $MNTPOINT/boot/
1115 [ -d $CONFFILES/etc ] && cp $VERBOSE -a -L $CONFFILES/etc/* $MNTPOINT/etc/
1116 [ -d $CONFFILES/sbin ] && cp $VERBOSE -a -L $CONFFILES/sbin/* $MNTPOINT/sbin/
1117 [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
1118 [ -d $CONFFILES/usr ] && cp $VERBOSE -a -L $CONFFILES/usr/* $MNTPOINT/usr/
1119 [ -d $CONFFILES/var ] && cp $VERBOSE -a -L $CONFFILES/var/* $MNTPOINT/var/
1121 # copy local network setup to chroot
1122 if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
1123 [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
1124 cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
1127 # install config file providing some example entries
1128 if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1129 cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1136 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1137 execute_pre_scripts() {
1138 # make sure we have $MNTPOINT available for our scripts
1140 if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1141 [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1142 for script in ${pre_scripts}/* ; do
1143 if [ -x "$script" ] ; then
1144 einfo "Executing pre-script $script"
1152 # execute all scripts in /etc/debootstrap/scripts/ {{{
1154 # make sure we have $MNTPOINT available for our scripts
1156 if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1157 [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1158 for script in ${scripts}/* ; do
1159 if [ -x "$script" ] ; then
1160 einfo "Executing script $script"
1168 # execute chroot-script {{{
1170 if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1174 if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1175 einfo "Executing chroot-script now"
1176 mount --bind /dev "$MNTPOINT"/dev
1177 chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1178 umount "$MNTPOINT"/dev
1181 eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1187 # unmount $MNTPOINT {{{
1190 # display installation notes:
1191 if [ -n "$INSTALL_NOTES" ] ; then
1192 [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1195 if [ -n "$ISODIR" ] ; then
1196 if grep -q "$ISODIR" /proc/mounts ; then
1197 einfo "Unmount $MNTPOINT/$ISODIR"
1198 umount "$MNTPOINT/$ISODIR"
1203 if grep -q "$MNTPOINT" /proc/mounts ; then
1204 if [ -n "$PARTITION" ] ; then
1205 einfo "Unmount $MNTPOINT"
1213 # execute filesystem check {{{
1215 if [ -n "$VIRTUAL" ] ; then
1216 einfo "Skipping filesystem check since we deploy a virtual machine."
1220 if [ "$FSCK" = 'yes' ] ; then
1221 [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1222 einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1229 # now execute all the functions {{{
1230 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1231 preparechroot execute_pre_scripts chrootscript execute_scripts \
1232 umount_chroot finalize_vm fscktool ; do
1233 if stage "${i}" ; then
1234 $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
1240 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
1241 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
1243 # Remove temporary mountpoint again
1244 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
1245 einfo "Removing directory ${MNTPOINT}" ; rmdir "$MNTPOINT" ; eend $?
1249 # end dialog of autoinstallation {{{
1250 if [ -n "$AUTOINSTALL" ] ; then
1251 if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1252 Automatically rebooting in 10 seconds.
1254 Choose Cancel to skip rebooting." 10 60 10 ; then
1255 noeject noprompt reboot
1258 einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1262 ## END OF FILE #################################################################
1263 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=2