update createfstab function
[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 22:27: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 ! [ -r /etc/kernel-img.conf ] ; then
21    echo "Setting up /etc/kernel-img.conf"
22    cat > /etc/kernel-img.conf << EOF
23 # Kernel Image management overrides
24 # See kernel-img.conf(5) for details
25 do_initrd = Yes
26 do_symlinks = Yes
27 EOF
28 fi
29
30 # install additional packages
31 if [ "$PACKAGES" = 'yes' ] ; then
32    if ! [ -r /etc/debootstrap/packages ] ; then
33      echo "Error: /etc/debootstrap/packages not found, exiting."
34      exit 1
35    else
36      apt-get update
37      apt-get -y install $(cat /etc/debootstrap/packages)
38    fi
39 fi
40
41 if [ -n "$RECONFIGURE" ] ; then
42    for package in $RECONFIGURE ; do
43        dpkg --list $package 1>/dev/null 2>/dev/null && dpkg-reconfigure $package || echo "Warning: $package does not exist, can not reconfigure it."
44    done
45 fi
46
47 # set password of user root
48 echo "Activating shadow passwords."
49 shadowconfig on
50 echo "Setting password for user root:"
51 passwd
52 echo ""
53
54 if ! [ -f /etc/hosts ] ; then
55    echo "Setting up /etc/hosts"
56    echo "127.0.0.1       localhost  $HOSTNAME" > /etc/hosts
57 fi
58
59 if ! [ -f /etc/network/interfaces ] ; then
60    echo "Setting up /etc/network/interfaces"
61    cat >> /etc/network/interfaces << EOF
62 iface lo inet loopback
63 iface eth0 inet dhcp
64 auto lo
65 auto eth0
66 EOF
67 fi
68
69 # adjust timezone
70 if [ -n "$TIMEZONE" ] ; then
71    echo "Adjusting /etc/localtime"
72    ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
73 fi
74
75 function createfstab(){
76    echo "Setting up /etc/fstab"
77 cat > /etc/fstab << EOF
78 $TARGET      /            auto    defaults,errors=remount-ro 0   1
79 /sys           /sys         sysfs   rw,nosuid,nodev,noexec     0   0
80 proc           /proc        proc    defaults                   0   0
81 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto             0   0
82 # some other examples:
83 # /dev/sda2       none         swap    sw                   0   0
84 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
85 # //1.2.3.4/pub   /smb/pub     smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
86 # linux:/pub      /beer        nfs     defaults             0  0
87 # tmpfs           /tmp         tmpfs   size=300M            0  0
88 EOF
89 }
90
91 # set up /etc/fstab if file is not present (cdebootstrap)
92 if [ ! -f /etc/fstab  ] ; then
93    createfstab
94 fi
95
96 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
97 if grep -q UNCONFIGURED /etc/fstab ; then
98    createfstab
99 fi
100
101 # set up hostname
102 if [ -n "$HOSTNAME" ] ; then
103    echo "Setting hostname to ${HOSTNAME}."
104    echo "$HOSTNAME" > /etc/hostname
105 fi
106
107 # create default devices
108 if ! [ -r /dev/hda20 ] ; then
109    echo "Creating generic devices in /dev - this might take a while..."
110    cd /dev && MAKEDEV generic
111 fi
112
113 # assume the first available kernel as our main kernel
114 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
115 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
116
117 # generate initrd
118 if [ -n "$INITRD" ] ; then
119    echo "Generating initrd."
120    update-initramfs -c -t -k $KERNELVER
121    if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
122       GRUBINITRD="initrd          /boot/initrd.img-$KERNELVER"
123       LILOINITRD="        initrd=/boot/initrd.img-$KERNELVER"
124    fi
125 fi
126
127 if [ -n "$GROOT" ] ; then
128    echo "Installing grub"
129
130    # copy stage-files to /boot/grub/
131    [ -d /boot/grub/ ] || mkdir /boot/grub
132    cp /usr/lib/grub/i386-pc/* /boot/grub/
133
134    # finally install grub
135    update-grub -y
136    sed -i "s/groot=.*/groot=(${GROOT})/g" /boot/grub/menu.lst
137    # not sure why savedefault does not work for me; any ideas?
138    sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
139    update-grub -y
140
141    if [ -z "$MBR" ] ; then
142       echo "Notice: \$MBR not set, will not install grub therefor."
143    else
144       cp /proc/mounts /etc/mtab
145       grub-install "$MBR"
146       rm /etc/mtab
147    fi
148 fi
149
150 # make sure we don't have any running processes left
151 [ -x /etc/init.d/ssh ] && /etc/init.d/ssh stop
152
153 # unmount all filesystems in chroot, make sure nothing is left...
154 umount -a    1>/dev/null 2>/dev/null || true
155 umount /proc 1>/dev/null 2>/dev/null || true
156 umount /proc 1>/dev/null 2>/dev/null || true
157 umount -a    1>/dev/null 2>/dev/null || true
158
159 # finally exit the chroot
160 echo "Finished chroot installation, exiting."
161 exit 0
162
163 ## END OF FILE #################################################################