some code updates for cdebootstrap
[grml-debootstrap.git] / chroot-script
1 #!/bin/sh
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 # Latest change: Fre Nov 03 21:45:09 CET 2006 [mika]
8 ################################################################################
9
10 set -e # exit on any error
11
12 . /etc/debootstrap/config || exit 1
13
14 [ -r /proc/1 ] || mount -t proc   none /proc
15
16 if [ -n "$CHROOTMIRROR" ] ; then
17    echo "$CHROOTMIRROR" > /etc/apt/sources.list
18 fi
19
20 # if ! [ -n /etc/kernel-img.conf ] ; then
21 #   echo "do_initrd = Yes" > /etc/kernel-img.conf
22 # fi
23
24 # install additional packages
25 if [ "$PACKAGES" = 'yes' ] ; then
26    if ! [ -r /etc/debootstrap/packages ] ; then
27      echo "Error: /etc/debootstrap/packages not found, exiting."
28      exit 1
29    else
30      apt-get update
31      apt-get -y install $(cat /etc/debootstrap/packages)
32    fi
33 fi
34
35 if [ -n "$RECONFIGURE" ] ; then
36    for package in $RECONFIGURE ; do
37        dpkg --list $package 1>/dev/null 2>/dev/null && dpkg-reconfigure $package || echo "Warning: $package does not exist, can not reconfigure it."
38    done
39 fi
40
41 # set password of user root
42 echo "Activating shadow passwords."
43 shadowconfig on
44 echo "Setting password for user root:"
45 passwd
46 echo ""
47
48 if ! [ -f /etc/hosts ] ; then
49    echo "Setting up /etc/hosts"
50    echo "127.0.0.1       localhost  $HOSTNAME" > /etc/hosts
51 fi
52
53 if ! [ -f /etc/network/interfaces ] ; then
54    echo "Setting up /etc/network/interfaces"
55    cat >> /etc/network/interfaces << EOF
56 iface lo inet loopback
57 iface eth0 inet dhcp
58 auto lo
59 auto eth0
60 EOF
61 fi
62
63 # adjust timezone
64 if [ -n "$TIMEZONE" ] ; then
65    echo "Adjusting /etc/localtime"
66    ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
67 fi
68
69 function createfstab(){
70    echo "Setting up /etc/fstab"
71 cat > /etc/fstab << EOF
72 $TARGET      /            auto    defaults,errors=remount-ro 0   1
73 sysfs          /sys         sysfs   auto                       0   0
74 proc           /proc        proc    defaults                   0   0
75 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto             0   0
76 # /dev/sda2    none         swap    sw                         0   0
77 EOF
78 }
79
80 # set up /etc/fstab if file is not present (cdebootstrap)
81 if [ ! -f /etc/fstab  ] ; then
82    createfstab
83 fi
84
85 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
86 if grep -q UNCONFIGURED /etc/fstab ; then
87    createfstab
88 fi
89
90 # create default devices
91 if ! [ -r /dev/hda20 ] ; then
92    echo "Creating generic devices in /dev - this might take a while..."
93    cd /dev && MAKEDEV generic
94 fi
95
96 # assume the first available kernel as our main kernel
97 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
98 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
99
100 # generate initrd
101 if [ -n "$INITRD" ] ; then
102    echo "Generating initrd."
103    update-initramfs -c -t -k $KERNELVER
104    if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
105       GRUBINITRD="initrd          /boot/initrd.img-$KERNELVER"
106       LILOINITRD="        initrd=/boot/initrd.img-$KERNELVER"
107    fi
108 fi
109
110 if [ -n "$GROOT" ] ; then
111    echo "Installing grub"
112
113    # copy stage-files to /boot/grub/
114    [ -d /boot/grub/ ] || mkdir /boot/grub
115    cp /usr/lib/grub/i386-pc/* /boot/grub/
116
117    # finally install grub
118    update-grub -y
119    sed -i "s/groot=.*/groot=(${GROOT})/g" /boot/grub/menu.lst
120    # not sure why savedefault does not work for me; any ideas?
121    sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
122    update-grub -y
123
124    if [ -z "$MBR" ] ; then
125       echo "Notice: \$MBR not set, will not install grub therefor."
126    else
127       cp /proc/mounts /etc/mtab
128       grub-install "$MBR"
129       rm /etc/mtab
130    fi
131 fi
132
133 # unmount all filesystems in chroot, make sure nothing is left...
134 umount -a    1>/dev/null 2>/dev/null || true
135 umount /proc 1>/dev/null 2>/dev/null || true
136 umount /proc 1>/dev/null 2>/dev/null || true
137 umount -a    1>/dev/null 2>/dev/null || true
138
139 # finally exit the chroot
140 echo "Finished chroot installation, exiting."
141 exit 0
142
143 ## END OF FILE #################################################################