2 # Filename: /etc/debootstrap/chroot-script
3 # Purpose: script executed in chroot when installing Debian via grml-debootstrap
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 # GRML_CHROOT_SCRIPT_MARKER - do not remove this line unless you want to keep
9 # this script as /bin/chroot-script on your new installed system
10 ################################################################################
12 . /etc/debootstrap/config || exit 1
13 . /etc/debootstrap/variables || exit 1
15 [ -r /proc/1 ] || mount -t proc none /proc
19 # use aptitude only if it's available
20 if [ -x /usr/bin/aptitude ] ; then
21 APTUPDATE="aptitude update $DPKG_OPTIONS"
22 # Debian ISOs do not contain signed Release files
23 if [ -n "$ISO" ] ; then
24 APTINSTALL="aptitude -y --allow-untrusted --without-recommends install $DPKG_OPTIONS"
25 APTUPGRADE="aptitude -y --allow-untrusted safe-upgrade $DPKG_OPTIONS"
27 APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
28 APTUPGRADE="aptitude -y safe-upgrade $DPKG_OPTIONS"
31 APTINSTALL="apt-get --force-yes -y --no-install-recommends install $DPKG_OPTIONS"
32 APTUPDATE="apt-get update $DPKG_OPTIONS"
33 APTUPGRADE="apt-get --force-yes -y upgrade $DPKG_OPTIONS"
36 if [ -z "$STAGES" ] ; then
37 STAGES='/etc/debootstrap/stages'
38 [ -d "$STAGES" ] || mkdir -p "$STAGES"
42 # helper functions {{{
45 echo "$2" > "$STAGES/$1"
47 elif grep -q done "$STAGES/$1" 2>/dev/null ; then
48 echo " [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
51 echo " Executing stage ${1}"
56 # read -s emulation for dash. result is in $resp.
58 [ -t 0 ] && stty -echo
65 # define chroot mirror {{{
67 if [ -n "$KEEP_SRC_LIST" ] ; then
68 echo "KEEP_SRC_LIST has been set, skipping chrootmirror stage."
72 if [ -z "$COMPONENTS" ] ; then
73 COMPONENTS='main contrib non-free'
75 echo "Using repository components $COMPONENTS"
77 if [ -n "$ISO" ] ; then
78 echo "Adjusting sources.list for ISO (${ISO})."
79 echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
80 echo "Adding mirror entry (${MIRROR}) to sources.list."
81 [ -n "$MIRROR" ] && echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list || true
83 if [ -n "$MIRROR" ] ; then
84 echo "Adjusting sources.list for mirror (${MIRROR})."
85 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
92 if [ -n "$MIRROR" ] ; then
93 echo "Release matching $RELEASE - enabling LTS support in sources.list"
94 echo "deb $MIRROR ${RELEASE}-lts $COMPONENTS" >> /etc/apt/sources.list
99 # add security.debian.org:
101 unstable|sid|lenny) ;; # no security pool available
103 echo "Adding security.debian.org to sources.list."
104 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
110 # remove local chroot mirror {{{
111 remove_chrootmirror() {
112 if [ -n "$KEEP_SRC_LIST" ] ; then
113 echo "KEEP_SRC_LIST has been set, skipping remove_chrootmirror stage."
117 if [ -n "$ISO" ] ; then
118 echo "Removing ISO (${ISO}) from sources.list."
119 TMP_ISO=$(echo "$ISO" |sed 's#/#\\/#g')
120 sed -i "/deb $TMP_ISO $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
122 if [ -n "$MIRROR" -a -n "$(echo $MIRROR|grep file:)" ] ; then
123 echo "Removing local mirror (${MIRROR}) from sources.list."
124 TMP_MIRROR=$(echo "$MIRROR" |sed 's#/#\\/#g')
125 sed -i "/deb $TMP_MIRROR $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
126 echo "Adding fallback mirror entry (${FALLBACK_MIRROR}) to sources.list instead."
127 echo "deb $FALLBACK_MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
133 # set up grml repository {{{
135 if [ -n "$GRMLREPOS" ] ; then
136 # user might have provided their own apt sources.list
137 if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
138 cat >> /etc/apt/sources.list.d/grml.list << EOF
139 # grml: stable repository:
140 deb http://deb.grml.org/ grml-stable main
141 deb-src http://deb.grml.org/ grml-stable main
143 # grml: testing/development repository:
144 deb http://deb.grml.org/ grml-testing main
145 deb-src http://deb.grml.org/ grml-testing main
149 if apt-get update $DPKG_OPTIONS; then
150 apt-get -y --allow-unauthenticated install grml-debian-keyring $DPKG_OPTIONS
151 apt-get update $DPKG_OPTIONS
153 # make sure we have the keys available for aptitude
154 gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
155 gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
156 # why it's necessary, sometimes we get an error even though it works [mika]
159 # make sure we install packages from Grml's pool only if not available
161 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
162 cat >> /etc/apt/preferences << EOF
163 // debian pool (default):
165 Pin: release o=Debian
168 // main grml-repository:
170 Pin: origin deb.grml.org
178 # check available backports release version {{{
180 wget -q -O/dev/null http://backports.debian.org/debian-backports/dists/${1}-backports/Release
184 # feature to provide Debian backports repos {{{
186 if [ -n "$BACKPORTREPOS" ] ; then
187 if ! checkbackports $RELEASE ; then
188 echo "Backports for ${RELEASE} are not available." >&2
191 # user might have provided their own apt sources.list
192 if ! grep -q backports /etc/apt/sources.list.d/backports.list 2>/dev/null ; then
193 cat >> /etc/apt/sources.list.d/backports.list << EOF
194 # debian backports: ${RELEASE}-backports repository:
195 deb http://backports.debian.org/debian-backports ${RELEASE}-backports main
196 deb-src http://backports.debian.org/debian-backports ${RELEASE}-backports main
204 # set up kernel-img.conf {{{
206 if ! [ -r /etc/kernel-img.conf ] ; then
207 echo "Setting up /etc/kernel-img.conf"
208 cat > /etc/kernel-img.conf << EOF
209 # Kernel Image management overrides
210 # See kernel-img.conf(5) for details
218 # make sure services do not start up {{{
219 install_policy_rcd() {
220 if ! [ -r /usr/sbin/policy-rc.d ] ; then
222 cat > /usr/sbin/policy-rc.d << EOF
226 chmod 775 /usr/sbin/policy-rc.d
231 # make sure we have an up2date system {{{
233 if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
234 echo "Running update + upgrade"
236 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTUPGRADE
238 echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
243 # remove now useless apt cache {{{
245 if [ "$RM_APTCACHE" = 'yes' ] ; then
246 echo "Cleaning apt cache."
247 apt-get clean $DPKG_OPTIONS
249 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
254 # install additional packages {{{
256 # Pre-seed the debconf database with answers. Each question will be marked
257 # as seen to prevent debconf from asking the question interactively.
258 [ -f /etc/debootstrap/debconf-selections ] && {
259 echo "Preseeding the debconf database, some lines might be skipped..."
260 cat /etc/debootstrap/debconf-selections | debconf-set-selections
263 if [ "$PACKAGES" = 'yes' ] ; then
264 if ! [ -r /etc/debootstrap/packages ] ; then
265 echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
269 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
275 # install extra packages {{{
277 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
278 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
279 if [ -n "$PACKAGELIST" ]; then
281 # run apt again to resolve any deps
282 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
288 # check if the specified Debian package exists
290 output=$(apt-cache show "$1" 2>/dev/null)
296 # determine the kernel version postfix
297 get_kernel_version() {
298 # do not override $KERNEL if set via config file
299 if [ -n "$KERNEL" ] ; then
306 amd64) KARCH=amd64 ;;
308 echo "Only i386 and amd64 are currently supported" >&2
312 for KPREFIX in "" "2.6-" ; do # iterate through the kernel prefixes,
313 # currently "" and "2.6-"
314 if package_exists linux-image-${KPREFIX}${KARCH} ; then
315 echo ${KPREFIX}${KARCH}
322 # install kernel packages {{{
324 if [ -n "$NOKERNEL" ] ; then
325 echo "Skipping installation of kernel packages as requested via --nokernel"
330 KVER=$(get_kernel_version)
331 if [ -n "$KVER" ] ; then
332 # note: install busybox to be able to debug initramfs
333 KERNELPACKAGES="linux-image-$KVER linux-headers-$KVER busybox firmware-linux-free firmware-linux"
334 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
336 echo "Warning: Could not find a kernel for your system. Your system won't be able to boot itself!"
341 # reconfigure packages {{{
343 if [ -n "$RECONFIGURE" ] ; then
344 for package in $RECONFIGURE ; do
345 if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
346 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
347 echo "Warning: $package does not exist, can not reconfigure it."
354 # set password of user root {{{
357 if [ -n "$NOPASSWORD" ] ; then
358 echo "Skip setting root password as requested."
362 echo "Activating shadow passwords."
366 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
370 if [ -n "$ROOTPASSWORD" ] ; then
371 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
372 export ROOTPASSWORD=''
376 echo "Setting password for user root:"
377 while [ "$a" != "$b" ] ; do
378 echo -n "Enter new UNIX password for user root: "
383 echo -n "Retype new UNIX password for user root: "
388 if [ "$a" != "$b" ] ; then
389 echo "Sorry, passwords do not match. Retry."
393 echo root:"$a" | chpasswd $CHPASSWD_OPTION
402 # set up /etc/hosts {{{
404 if [ -f /etc/hosts ] ; then
405 sed -i "s#127.0.0.1 .*#127.0.0.1 localhost $HOSTNAME#" /etc/hosts
406 [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
408 cat > /etc/hosts << EOF
409 127.0.0.1 localhost $HOSTNAME
412 #127.0.1.1 $HOSTNAME.example.org $HOSTNAME
414 # The following lines are desirable for IPv6 capable hosts
415 #::1 ip6-localhost ip6-loopback $HOSTNAME
416 ::1 ip6-localhost ip6-loopback
418 ff00::0 ip6-mcastprefix
420 ff02::2 ip6-allrouters
427 # set default locales {{{
429 if [ -n "$DEFAULT_LOCALES" ] ; then
430 if ! [ -x /usr/sbin/update-locale ] ; then
431 echo "Warning: update-locale executable not available (no locales package installed?)"
432 echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
436 /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
441 # adjust timezone {{{
443 if [ -n "$TIMEZONE" ] ; then
444 echo "Adjusting /etc/localtime"
445 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
450 # helper function for fstab() {{{
452 echo "Setting up /etc/fstab"
453 if [ -n "$TARGET_UUID" ] ; then
454 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
456 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
459 cat >> /etc/fstab << EOF
460 proc /proc proc defaults 0 0
461 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
462 # some other examples:
463 # /dev/sda2 none swap sw,pri=0 0 0
464 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
465 # //1.2.3.4/pub /smb/pub cifs user,noauto,uid=grml,gid=grml 0 0
466 # linux:/pub /beer nfs defaults 0 0
467 # tmpfs /tmp tmpfs size=300M 0 0
468 # /dev/sda5 none swap sw 0 0
473 # generate /etc/fstab {{{
475 # set up /etc/fstab if file is not present (cdebootstrap)
476 if [ ! -f /etc/fstab ] ; then
480 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
481 if grep -q UNCONFIGURED /etc/fstab ; then
487 # set up hostname {{{
489 if [ -n "$HOSTNAME" ] ; then
490 echo "Setting hostname to ${HOSTNAME}."
491 echo "$HOSTNAME" > /etc/hostname
493 # adjust postfix configuration
494 if [ -r /etc/postfix/main.cf ] ; then
495 # adjust hostname related options:
496 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
498 # listen on loopback interface only:
499 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
500 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
502 if [ -r /etc/mailname ] ; then
503 # adjust /etc/mailname
504 local etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
507 local mailname="$HOSTNAME"
510 local mailname="${HOSTNAME}.${etc_mail_domain}"
513 echo "Setting mailname to ${mailname}"
514 echo "$mailname" > /etc/mailname
520 # generate initrd/initramfs {{{
522 # assume the first available kernel as our main kernel
523 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
524 if [ -z "$KERNELIMG" ] ; then
525 echo 'No kernel image found, skipping initrd stuff.'>&2
529 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
532 if [ -n "$INITRD" ] ; then
533 echo "Generating initrd."
534 update-initramfs -c -t -k $KERNELVER
539 # grub configuration/installation {{{
542 if [ -z "$GRUB" ] ; then
543 echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
547 if ! dpkg --list grub-pc 2>/dev/null | grep -q '^ii' ; then
548 echo "Notice: grub option set but no grub-pc package, installing it therefore."
549 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL grub-pc
552 if ! [ -x "$(which grub-install)" ] ; then
553 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
557 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
558 for device in $SELECTED_PARTITIONS ; do
559 GRUB="${device%%[0-9]}"
560 echo "Installing grub on ${GRUB}:"
561 grub-install --no-floppy "$GRUB"
564 echo "Installing grub on ${GRUB}:"
565 grub-install --no-floppy "$GRUB"
568 echo "Adjusting grub configuration for use on ${GRUB}."
570 # finally install grub
571 if [ -x /usr/sbin/update-grub ] ; then
572 UPDATEGRUB='/usr/sbin/update-grub'
573 elif [ -x /sbin/update-grub ] ; then
574 UPDATEGRUB='/sbin/update-grub'
576 echo "Error: update-grub not available, can not execute it." >&2
584 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
586 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
588 for script in /etc/debootstrap/chroot-scripts/* ; do
589 echo "Executing script $script"
590 $script && echo "done" || echo "failed"
595 # make sure we don't have any running processes left {{{
597 for service in ssh mdadm mdadm-raid ; do
598 if [ -x /etc/init.d/"$service" ] ; then
599 /etc/init.d/"$service" stop || true
605 # unmount /proc and make sure nothing is left {{{
607 # make sure we don't leave any sensible data
608 rm -f /etc/debootstrap/variables
610 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
612 umount /proc >/dev/null 2>/dev/null || true
619 [ -n "$1" ] && EXIT="$1" || EXIT="1"
624 # set signal handler {{{
625 trap signal_handler HUP INT QUIT TERM
628 # execute the functions {{{
630 # always execute install_policy_rcd
633 for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
634 kernel packages extrapackages reconfigure hosts \
635 default_locales timezone fstab hostname initrd grub_install passwords \
636 custom_scripts upgrade_system remove_apt_cache services \
637 remove_chrootmirror; do
639 $i && stage $i done || exit 1
642 # always execute the finalize stage:
646 # finally exit the chroot {{{
647 echo "Finished chroot installation, exiting."
651 ## END OF FILE #################################################################
652 # vim: ai tw=80 expandtab foldmethod=marker