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