fix stage() 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: Mon Apr 09 23:27:30 CEST 2007 [mika]
8 ################################################################################
9
10 set -e # exit on any error
11
12 . /etc/debootstrap/config || exit 1
13
14 if [ -z "$STAGES" ] ; then
15    STAGES='/etc/debootstrap/stages'
16    [ -d "$STAGES" ] || mkdir -p "$STAGES"
17 fi
18
19 stage() {
20   # error handling
21   if [ -z "$1" ] ; then
22      eerror 'Error: stage() requires at least one argument.'
23      eend 1
24      return 1
25   fi
26   if [ -z "$STAGES" ] ; then
27      eerror 'Error: $STAGES not set.'
28      eend 1
29      return 1
30   fi
31   # main function
32   if grep -q done "$STAGES/$1" 2>/dev/null ; then
33      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
34   else
35      echo "$2" > "$STAGES/$1"
36   fi
37 }
38
39 [ -r /proc/1 ] || mount -t proc   none /proc
40
41 if [ -n "$CHROOTMIRROR" ] ; then
42    stage chrootmirror
43    echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" > /etc/apt/sources.list
44    stage chrootmirror done
45 fi
46
47 if [ -n "$GRMLREPOS" ] ; then
48    stage grmlrepos
49    echo 'deb     http://grml.org/repos/ ./' >> /etc/apt/sources.list
50    stage grmlrepos done
51 fi
52
53 if ! [ -r /etc/kernel-img.conf ] ; then
54    stage kernelimg.conf
55    echo "Setting up /etc/kernel-img.conf"
56    cat > /etc/kernel-img.conf << EOF
57 # Kernel Image management overrides
58 # See kernel-img.conf(5) for details
59 do_initrd = Yes
60 do_symlinks = Yes
61 EOF
62    stage kernelimg.conf done
63 fi
64
65 # create default devices
66 if ! [ -r /dev/hda20 ] ; then
67    stage makedev
68    echo "Creating generic devices in /dev - this might take a while..."
69    cd /dev && MAKEDEV generic
70    stage makedev done
71 fi
72
73 # install additional packages
74 if [ "$PACKAGES" = 'yes' ] ; then
75    stage packages
76    if ! [ -r /etc/debootstrap/packages ] ; then
77      echo "Error: /etc/debootstrap/packages not found, exiting."
78      exit 1
79    else
80      apt-get update
81      apt-get --force-yes -y install $(cat /etc/debootstrap/packages) $GRMLPACKAGES
82    fi
83    stage packages done
84 fi
85
86 # sarge specific stuff
87 if [ "$RELEASE" = 'sarge' ] ; then
88    stage mkinitrd
89    sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
90    stage mkinitrd done
91 fi
92
93 if [ -n "$KERNEL" ] ; then
94    stage kernel
95    apt-get update
96    if [ "$RELEASE" = 'sarge' ] ; then
97       KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
98    else
99       KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
100    fi
101    apt-get --force-yes -y install $KERNELPACKAGES
102    stage kernel done
103 fi
104
105 if [ -n "$RECONFIGURE" ] ; then
106    stage reconfigure
107    for package in $RECONFIGURE ; do
108        dpkg --list $package 1>/dev/null 2>/dev/null && dpkg-reconfigure $package || echo "Warning: $package does not exist, can not reconfigure it."
109    done
110    stage reconfigure done
111 fi
112
113 # set password of user root
114 stage passwords
115 echo "Activating shadow passwords."
116 shadowconfig on
117 echo "Setting password for user root:"
118 set +e # do not exit if passwd returns error due to missmatching passwords
119 passwd
120 echo ""
121 set -e # restore default behaviour again
122 stage passwords done
123
124 if ! [ -f /etc/hosts ] ; then
125    stage hosts
126    echo "Setting up /etc/hosts"
127    echo "127.0.0.1       localhost  $HOSTNAME" > /etc/hosts
128    stage hosts done
129 fi
130
131 if ! [ -f /etc/network/interfaces ] ; then
132    stage interfaces
133    echo "Setting up /etc/network/interfaces"
134    cat >> /etc/network/interfaces << EOF
135 iface lo inet loopback
136 iface eth0 inet dhcp
137 auto lo
138 auto eth0
139 EOF
140    stage interfaces done
141 fi
142
143 # adjust timezone
144 if [ -n "$TIMEZONE" ] ; then
145    stage timezone
146    echo "Adjusting /etc/localtime"
147    ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
148    stage timezone done
149 fi
150
151 createfstab(){
152    echo "Setting up /etc/fstab"
153 cat > /etc/fstab << EOF
154 $TARGET      /            auto    defaults,errors=remount-ro 0   1
155 /sys           /sys         sysfs   rw,nosuid,nodev,noexec     0   0
156 proc           /proc        proc    defaults                   0   0
157 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto             0   0
158 # some other examples:
159 # /dev/sda2       none         swap    sw                   0   0
160 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
161 # //1.2.3.4/pub   /smb/pub     smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
162 # linux:/pub      /beer        nfs     defaults             0  0
163 # tmpfs           /tmp         tmpfs   size=300M            0  0
164 EOF
165 }
166
167 # set up /etc/fstab if file is not present (cdebootstrap)
168 if [ ! -f /etc/fstab  ] ; then
169    stage fstab
170    createfstab
171    stage fstab done
172 fi
173
174 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
175 if grep -q UNCONFIGURED /etc/fstab ; then
176    stage fstab
177    createfstab
178    stage fstab done
179 fi
180
181 # set up hostname
182 if [ -n "$HOSTNAME" ] ; then
183    stage hostname
184    echo "Setting hostname to ${HOSTNAME}."
185    echo "$HOSTNAME" > /etc/hostname
186    stage hostname done
187 fi
188
189 # assume the first available kernel as our main kernel
190 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
191 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
192
193 # generate initrd
194 if [ -n "$INITRD" ] ; then
195    stage initrd
196    if [ "$RELEASE" = 'sarge' ] ; then
197       echo "Release sarge detected, will not create an initrd."
198    else
199       echo "Generating initrd."
200       update-initramfs -c -t -k $KERNELVER
201       if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
202          GRUBINITRD="initrd          /boot/initrd.img-$KERNELVER"
203          LILOINITRD="        initrd=/boot/initrd.img-$KERNELVER"
204       fi
205    fi
206    stage initrd done
207 fi
208
209 if [ -z "$GROOT" ] ; then
210    echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
211 else
212    stage grub
213    echo "Adjusting grub configuration for use on ${GROOT}."
214
215    # copy stage-files to /boot/grub/
216    [ -d /boot/grub/ ] || mkdir /boot/grub
217    if [ -d /usr/lib/grub/i386-pc/ ] ; then
218       cp /usr/lib/grub/i386-pc/* /boot/grub/
219    else
220       # sarge ships grub files in another directory
221       cp /lib/grub/i386-pc/* /boot/grub/
222    fi
223
224    # finally install grub
225    update-grub -y
226    sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
227    sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro|g" /boot/grub/menu.lst
228    # not sure why savedefault does not work for me; any ideas?
229    sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
230    update-grub -y
231    stage grub done
232 fi
233
234 # make sure we don't have any running processes left
235 for service in ssh mdadm mdadm-raid ; do
236     [ -x "/etc/init.d/$service" ] && "/etc/init.d/$service" stop
237 done
238
239 # unmount all filesystems in chroot, make sure nothing is left...
240 umount -a    1>/dev/null 2>/dev/null || true
241 umount /proc 1>/dev/null 2>/dev/null || true
242 umount /proc 1>/dev/null 2>/dev/null || true
243 umount -a    1>/dev/null 2>/dev/null || true
244
245 # finally exit the chroot
246 echo "Finished chroot installation, exiting."
247 exit 0
248
249 ## END OF FILE #################################################################