Some minor code cleanups
[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: Tue Aug 26 20:59:31 CEST 2008 [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 # variable checks {{{
18
19 # use aptitude only if it's available
20 if [ -x /usr/bin/aptitude ] ; then
21    APTINSTALL='aptitude -y install '
22    APTUPDATE='aptitude update'
23 else
24    APTINSTALL='apt-get --force-yes -y install'
25    APTUPDATE='apt-get update'
26 fi
27
28 if [ -z "$STAGES" ] ; then
29    STAGES='/etc/debootstrap/stages'
30    [ -d "$STAGES" ] || mkdir -p "$STAGES"
31 fi
32 # }}}
33
34 # helper functions {{{
35 stage() {
36   if [ -n "$2" ] ; then
37      echo "$2" > "$STAGES/$1"
38      return 0
39   elif grep -q done "$STAGES/$1" 2>/dev/null ; then
40      echo "[*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
41      return 1
42   fi
43 }
44 # }}}
45
46 # define chroot mirror {{{
47 chrootmirror() {
48   if [ -n "$ISO" ] ; then
49      echo "deb $ISO $RELEASE main contrib" > /etc/apt/sources.list
50      [ -n "$CHROOTMIRROR" ] && echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" >> /etc/apt/sources.list
51   else
52     if [ -n "$CHROOTMIRROR" ] ; then
53        echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" > /etc/apt/sources.list
54     fi
55   fi
56 }
57 # }}}
58
59 # set up grml repository {{{
60 grmlrepos() {
61   if [ -n "$GRMLREPOS" ] ; then
62      cat >> /etc/apt/sources.list << EOF
63
64 # grml: stable repository:
65   deb     http://deb.grml.org/ grml-stable  main
66   deb-src http://deb.grml.org/ grml-stable  main
67
68 # grml: testing/development repository:
69   deb     http://deb.grml.org/ grml-testing main
70   deb-src http://deb.grml.org/ grml-testing main
71
72 EOF
73      # make sure we have the keys available for aptitude
74      gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
75      gpg --export F61E2E7CECDEA787 | apt-key add - || /bin/true # not yet sure
76      # why it's necessary, sometimes we get an error even though it works [mika]
77
78      # make sure we install packages from grml's pool only if not available
79      # from Debian!
80      if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
81         cat >> /etc/apt/preferences << EOF
82 // debian pool (default):
83 Package: *
84 Pin: release o=Debian
85 Pin-Priority: 996
86
87 // main grml-repository:
88 Package: *
89 Pin: origin deb.grml.org
90 Pin-Priority: 991
91 EOF
92      fi
93   fi
94 }
95 # }}}
96
97 # set up kernel-img.conf {{{
98 kernelimg_conf() {
99   if ! [ -r /etc/kernel-img.conf ] ; then
100      echo "Setting up /etc/kernel-img.conf"
101      cat > /etc/kernel-img.conf << EOF
102 # Kernel Image management overrides
103 # See kernel-img.conf(5) for details
104 do_initrd = Yes
105 do_symlinks = Yes
106 EOF
107   fi
108 }
109 # }}}
110
111 # create default devices {{{
112 makedev() {
113   if ! [ -r /dev/hda20 ] ; then
114      echo "Creating generic devices in /dev - this might take a while..."
115      cd /dev && MAKEDEV generic
116   fi
117 }
118 # }}}
119
120 # make sure services do not start up {{{
121 install_policy_rcd() {
122   if ! [ -r /usr/sbin/policy-rc.d ] ; then
123      export POLICYRCD=1
124      cat > /usr/sbin/policy-rc.d << EOF
125 #!/bin/sh
126 exit 101
127 EOF
128      chmod 775 /usr/sbin/policy-rc.d
129   fi
130 }
131 # }}}
132
133 # install additional packages {{{
134 packages() {
135   if [ "$PACKAGES" = 'yes' ] ; then
136      if ! [ -r /etc/debootstrap/packages ] ; then
137        echo "Error: /etc/debootstrap/packages not found, exiting."
138        exit 1
139      else
140        $APTUPDATE
141        DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(cat /etc/debootstrap/packages) $GRMLPACKAGES
142      fi
143   fi
144 }
145 # }}}
146
147 # install extra packages {{{
148 extrapackages() {
149     if [ "$EXTRAPACKAGES" = 'yes' ] ; then
150         PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
151         if [ -n "$PACKAGELIST" ]; then
152             dpkg -i $PACKAGELIST
153             # run apt again to resolve any deps
154             DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
155         fi
156     fi
157 }
158 # }}}
159
160 #  sarge specific stuff: mkinitrd {{{
161 mkinitrd() {
162   if [ "$RELEASE" = 'sarge' ] ; then
163      sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
164   fi
165 }
166 # }}}
167
168 # install kernel packages {{{
169 kernel() {
170   # do not override $KERNEL if set via config file
171   if [ -z "$KERNEL" ] ; then
172      if [ "$ARCH" = 'i386' ] ; then
173         KERNEL='2.6-686'
174      elif [ "$ARCH" = 'amd64' ] ; then
175         KERNEL='2.6-amd64'
176      fi
177   fi
178
179   if [ -n "$KERNEL" ] ; then
180      $APTUPDATE
181      if [ "$RELEASE" = 'sarge' ] ; then
182         KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
183      else
184         KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
185      fi
186       DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
187   fi
188 }
189 # }}}
190
191 # reconfigure packages {{{
192 reconfigure() {
193   if [ -n "$RECONFIGURE" ] ; then
194      for package in $RECONFIGURE ; do
195          dpkg --list $package 1>/dev/null 2>/dev/null && \
196          DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
197          echo "Warning: $package does not exist, can not reconfigure it."
198      done
199   fi
200 }
201 # }}}
202
203 # set password of user root {{{
204 passwords() {
205   echo "Activating shadow passwords."
206   shadowconfig on
207
208   if [ -n "$ROOTPASSWORD" ] ; then
209      echo root:"$ROOTPASSWD" | chpasswd -m
210      export ROOTPASSWD=''
211   else
212      echo "Setting password for user root:"
213      echo -n "Enter new UNIX password for user root: "
214      read -s a
215      echo root:"$a" | chpasswd -m
216      unset a
217      echo
218   fi
219 }
220 # }}}
221
222 # set up /etc/hosts {{{
223 hosts() {
224   if ! [ -f /etc/hosts ] ; then
225      echo "Setting up /etc/hosts"
226      echo "127.0.0.1       localhost  $HOSTNAME" > /etc/hosts
227   fi
228 }
229 # }}}
230
231 # set up /etc/network/interfaces {{{
232 interfaces() {
233   if ! [ -r /etc/network/interfaces ] ; then
234      echo "Setting up /etc/network/interfaces"
235      cat >> /etc/network/interfaces << EOF
236
237 # loopback device:
238 iface lo inet loopback
239 auto lo
240
241 # eth0:
242 # iface eth0 inet dhcp
243 # auto eth0
244
245 EOF
246   fi
247 }
248 # }}}
249
250 # adjust timezone {{{
251 timezone() {
252   if [ -n "$TIMEZONE" ] ; then
253      echo "Adjusting /etc/localtime"
254      ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
255   fi
256 }
257 # }}}
258
259 # helper function for fstab() {{{
260 createfstab(){
261      echo "Setting up /etc/fstab"
262 cat > /etc/fstab << EOF
263 $TARGET      /            auto    defaults,errors=remount-ro 0   1
264 proc           /proc        proc    defaults                      0   0
265 /sys           /sys         sysfs   noauto,rw,nosuid,nodev,noexec 0   0
266 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto                0   0
267 # some other examples:
268 # /dev/sda2       none         swap    sw                   0   0
269 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
270 # //1.2.3.4/pub   /smb/pub     smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
271 # linux:/pub      /beer        nfs     defaults             0  0
272 # tmpfs           /tmp         tmpfs   size=300M            0  0
273 EOF
274 }
275 # }}}
276
277 # generate /etc/fstab {{{
278 fstab() {
279   # set up /etc/fstab if file is not present (cdebootstrap)
280   if [ ! -f /etc/fstab  ] ; then
281      createfstab
282   fi
283
284   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
285   if grep -q UNCONFIGURED /etc/fstab ; then
286      createfstab
287   fi
288 }
289 # }}}
290
291 # set up hostname {{{
292 hostname() {
293   if [ -n "$HOSTNAME" ] ; then
294      echo "Setting hostname to ${HOSTNAME}."
295      echo "$HOSTNAME" > /etc/hostname
296   fi
297 }
298 # }}}
299
300 # generate initrd/initramfs {{{
301 initrd() {
302   # assume the first available kernel as our main kernel
303   KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
304   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
305
306   # generate initrd
307   if [ -n "$INITRD" ] ; then
308      if [ "$RELEASE" = 'sarge' ] ; then
309         echo "Release sarge detected, will not create an initrd."
310      else
311         echo "Generating initrd."
312         update-initramfs -c -t -k $KERNELVER
313         if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
314            GRUBINITRD="initrd          /boot/initrd.img-$KERNELVER"
315            LILOINITRD="        initrd=/boot/initrd.img-$KERNELVER"
316         fi
317      fi
318   fi
319 }
320 # }}}
321
322 # grub configuration/installation {{{
323 grub() {
324   if [ -z "$GROOT" ] ; then
325      echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
326   else
327      echo "Adjusting grub configuration for use on ${GROOT}."
328
329      # copy stage-files to /boot/grub/
330      [ -d /boot/grub/ ] || mkdir /boot/grub
331      # i386 specific:
332      [ -d /usr/lib/grub/i386-pc ]   && cp /usr/lib/grub/i386-pc/* /boot/grub/
333      # amd64 specific:
334      [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
335      # sarge ships grub files in another directory
336      [ "$RELEASE" = 'sarge' ]       && cp /lib/grub/i386-pc/* /boot/grub/
337
338      # finally install grub
339      if [ -x /usr/sbin/update-grub ] ; then
340         UPDATEGRUB='/usr/sbin/update-grub'
341      else
342         UPDATEGRUB='/sbin/update-grub'
343      fi
344      $UPDATEGRUB -y
345      if [ -f /boot/grub/menu.lst ] ; then
346         sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
347         sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
348         # not sure why savedefault does not work for me; any ideas?
349         sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
350         $UPDATEGRUB -y
351      fi
352   fi
353 }
354 # }}}
355
356 # make sure we don't have any running processes left {{{
357 services() {
358   for service in ssh mdadm mdadm-raid ; do
359     if [ -x /etc/init.d/"$service" ] ; then
360        /etc/init.d/"$service" stop || /bin/true
361     fi
362   done
363 }
364 # }}}
365
366 # unmount all filesystems in chroot, make sure nothing is left {{{
367 finalize() {
368   # make sure we don't leave any sensible data
369   rm -f /etc/debootstrap/variables
370   [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
371   umount -a    1>/dev/null 2>/dev/null || true
372   umount /proc 1>/dev/null 2>/dev/null || true
373   umount /proc 1>/dev/null 2>/dev/null || true
374   umount -a    1>/dev/null 2>/dev/null || true
375 }
376 # }}}
377
378 # execute the functions {{{
379  for i in chrootmirror grmlrepos kernelimg_conf makedev install_policy_rcd \
380      packages extrapackages mkinitrd kernel reconfigure hosts interfaces   \
381      timezone fstab hostname initrd grub passwords services finalize ; do
382     if stage $i ; then
383        $i && stage $i done || exit 1
384     fi
385   done
386 # }}}
387
388 # finally exit the chroot {{{
389   echo "Finished chroot installation, exiting."
390   exit 0
391 # }}}
392
393 ## END OF FILE #################################################################
394 # vim: ai tw=80 expandtab foldmethod=marker