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