Fixed some typos in the manpage; thanks Alexander Steinboeck
[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: Sam Apr 28 20:19:58 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 # 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 # install additional packages {{{
121 packages() {
122   if [ "$PACKAGES" = 'yes' ] ; then
123      if ! [ -r /etc/debootstrap/packages ] ; then
124        echo "Error: /etc/debootstrap/packages not found, exiting."
125        exit 1
126      else
127        $APTUPDATE
128        DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(cat /etc/debootstrap/packages) $GRMLPACKAGES
129      fi
130   fi
131 }
132 # }}}
133
134 # install extra packages {{{
135 extrapackages() {
136     if [ "$EXTRAPACKAGES" = 'yes' ] ; then
137         PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
138         if [ -n "$PACKAGELIST" ]; then
139             dpkg -i $PACKAGELIST
140             # run apt again to resolve any deps
141             DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
142         fi
143     fi
144 }
145 # }}}
146
147 #  sarge specific stuff: mkinitrd {{{
148 mkinitrd() {
149   if [ "$RELEASE" = 'sarge' ] ; then
150      sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
151   fi
152 }
153 # }}}
154
155 # install kernel packages {{{
156 kernel() {
157   # do not override $KERNEL if set via config file
158   if [ -z "$KERNEL" ] ; then
159      if [ "$ARCH" = 'i386' ] ; then
160         KERNEL='2.6-686'
161      elif [ "$ARCH" = 'amd64' ] ; then
162         KERNEL='2.6-amd64'
163      fi
164   fi
165
166   if [ -n "$KERNEL" ] ; then
167      $APTUPDATE
168      if [ "$RELEASE" = 'sarge' ] ; then
169         KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
170      else
171         KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
172      fi
173       DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
174   fi
175 }
176 # }}}
177
178 # reconfigure packages {{{
179 reconfigure() {
180   if [ -n "$RECONFIGURE" ] ; then
181      for package in $RECONFIGURE ; do
182          dpkg --list $package 1>/dev/null 2>/dev/null && \
183          DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
184          echo "Warning: $package does not exist, can not reconfigure it."
185      done
186   fi
187 }
188 # }}}
189
190 # set password of user root {{{
191 setpassword() {
192 # Set a password, via chpasswd.
193 # Use perl rather than echo, to avoid the password
194 # showing in the process table. (However, this is normally
195 # only called when first booting the system, when root has no
196 # password at all, so that should be an unnecessary precaution).
197 #
198 # Pass in three arguments: the user, the password, and 'true' if the
199 # password has been pre-crypted (by preseeding).
200 #
201 # Taken from /var/lib/dpkg/info/passwd.config
202         SETPASSWD_PW="$2"
203         export SETPASSWD_PW
204
205         # This is very annoying. chpasswd cannot handle generating md5
206         # passwords as it is not PAM-aware. Thus, I have to work around
207         # that by crypting the password myself if md5 is used.
208         USE_MD5=1
209         export USE_MD5
210
211         if [ "$3" = true ]; then
212                 PRECRYPTED=1
213         else
214                 PRECRYPTED=''
215         fi
216         export PRECRYPTED
217         LC_ALL=C LANGUAGE=C LANG=C perl -e '
218                 sub CreateCryptSalt {
219                         my $md5 = shift;
220
221                         my @valid = split(//, "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
222                         my ($in, $out);
223
224                         my $cryptsaltlen = ($md5 ? 8 : 2);
225
226                         open (F, "</dev/urandom") || die "No /dev/urandom found!";
227                         foreach (1..$cryptsaltlen) {
228                                 read(F, $in, 1);
229                                 $out .= $valid[ord($in) % ($#valid + 1)];
230                         }
231                         close F;
232                         return ($md5 ? "\$1\$$out\$" : $out);
233                 }
234
235                 open(P,"| chpasswd -e");
236                 if ($ENV{PRECRYPTED}) {
237                         print P shift().":$ENV{SETPASSWD_PW}\n";
238                 } else {
239                         print P shift().":".
240                                 crypt($ENV{SETPASSWD_PW}, CreateCryptSalt($ENV{USE_MD5})).
241                                 "\n";
242                 }
243                 close P;
244         ' "$1"
245         SETPASSWD_PW=''
246         USE_MD5=''
247         PRECRYPTED=''
248 }
249
250 passwords() {
251   echo "Activating shadow passwords."
252   shadowconfig on
253
254   if [ -n "$ROOTPASSWORD" ] ; then
255      setpassword root "$ROOTPASSWD" false
256      export ROOTPASSWD=''
257   else
258      echo "Setting password for user root:"
259      set +e # do not exit if passwd returns error due to missmatching passwords
260      passwd
261      echo ""
262      set -e # restore default behaviour again
263   fi
264 }
265 # }}}
266
267 # set up /etc/hosts {{{
268 hosts() {
269   if ! [ -f /etc/hosts ] ; then
270      echo "Setting up /etc/hosts"
271      echo "127.0.0.1       localhost  $HOSTNAME" > /etc/hosts
272   fi
273 }
274 # }}}
275
276 # set up /etc/network/interfaces {{{
277 interfaces() {
278   touch /etc/network/interfaces
279   # make sure we add the entries only once
280   if ! grep -q eth0 /etc/network/interfaces ; then
281      echo "Setting up /etc/network/interfaces"
282      cat >> /etc/network/interfaces << EOF
283
284 # loopback device:
285 iface lo inet loopback
286 auto lo
287
288 # eth0:
289 # iface eth0 inet dhcp
290 # auto eth0
291
292 EOF
293   fi
294 }
295 # }}}
296
297 # adjust timezone {{{
298 timezone() {
299   if [ -n "$TIMEZONE" ] ; then
300      echo "Adjusting /etc/localtime"
301      ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
302   fi
303 }
304 # }}}
305
306 # helper function for fstab() {{{
307 createfstab(){
308      echo "Setting up /etc/fstab"
309 cat > /etc/fstab << EOF
310 $TARGET      /            auto    defaults,errors=remount-ro 0   1
311 /sys           /sys         sysfs   rw,nosuid,nodev,noexec     0   0
312 proc           /proc        proc    defaults                   0   0
313 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto             0   0
314 # some other examples:
315 # /dev/sda2       none         swap    sw                   0   0
316 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
317 # //1.2.3.4/pub   /smb/pub     smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
318 # linux:/pub      /beer        nfs     defaults             0  0
319 # tmpfs           /tmp         tmpfs   size=300M            0  0
320 EOF
321 }
322 # }}}
323
324 # generate /etc/fstab {{{
325 fstab() {
326   # set up /etc/fstab if file is not present (cdebootstrap)
327   if [ ! -f /etc/fstab  ] ; then
328      createfstab
329   fi
330
331   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
332   if grep -q UNCONFIGURED /etc/fstab ; then
333      createfstab
334   fi
335 }
336 # }}}
337
338 # set up hostname {{{
339 hostname() {
340   if [ -n "$HOSTNAME" ] ; then
341      echo "Setting hostname to ${HOSTNAME}."
342      echo "$HOSTNAME" > /etc/hostname
343   fi
344 }
345 # }}}
346
347 # generate initrd/initramfs {{{
348 initrd() {
349   # assume the first available kernel as our main kernel
350   KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
351   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
352
353   # generate initrd
354   if [ -n "$INITRD" ] ; then
355      if [ "$RELEASE" = 'sarge' ] ; then
356         echo "Release sarge detected, will not create an initrd."
357      else
358         echo "Generating initrd."
359         update-initramfs -c -t -k $KERNELVER
360         if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
361            GRUBINITRD="initrd          /boot/initrd.img-$KERNELVER"
362            LILOINITRD="        initrd=/boot/initrd.img-$KERNELVER"
363         fi
364      fi
365   fi
366 }
367 # }}}
368
369 # grub configuration/installation {{{
370 grub() {
371   if [ -z "$GROOT" ] ; then
372      echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
373   else
374      echo "Adjusting grub configuration for use on ${GROOT}."
375
376      # copy stage-files to /boot/grub/
377      [ -d /boot/grub/ ] || mkdir /boot/grub
378      # i386 specific:
379      [ -d /usr/lib/grub/i386-pc ]   && cp /usr/lib/grub/i386-pc/* /boot/grub/
380      # amd64 specific:
381      [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
382      # sarge ships grub files in another directory
383      [ "$RELEASE" = 'sarge' ]       && cp /lib/grub/i386-pc/* /boot/grub/
384
385      # finally install grub
386      if [ -x /usr/sbin/update-grub ] ; then
387         UPDATEGRUB='/usr/sbin/update-grub'
388      else
389         UPDATEGRUB='/sbin/update-grub'
390      fi
391      $UPDATEGRUB -y
392      if [ -f /boot/grub/menu.lst ] ; then
393         sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
394         sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
395         # not sure why savedefault does not work for me; any ideas?
396         sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
397         $UPDATEGRUB -y
398      fi
399   fi
400 }
401 # }}}
402
403 # make sure we don't have any running processes left {{{
404 services() {
405   for service in ssh mdadm mdadm-raid ; do
406     if [ -x /etc/init.d/"$service" ] ; then
407        /etc/init.d/"$service" stop || /bin/true
408     fi
409   done
410 }
411 # }}}
412
413 # unmount all filesystems in chroot, make sure nothing is left {{{
414 finalize() {
415   # make sure we don't leave any sensible data
416   rm -f /etc/debootstrap/variables
417   umount -a    1>/dev/null 2>/dev/null || true
418   umount /proc 1>/dev/null 2>/dev/null || true
419   umount /proc 1>/dev/null 2>/dev/null || true
420   umount -a    1>/dev/null 2>/dev/null || true
421 }
422 # }}}
423
424 # execute the functions {{{
425  for i in chrootmirror grmlrepos kernelimg_conf makedev packages extrapackages \
426      mkinitrd kernel reconfigure hosts interfaces timezone fstab hostname \
427      initrd grub passwords services finalize ; do
428     if stage $i ; then
429        $i && stage $i done || exit 1
430     fi
431   done
432 # }}}
433
434 # finally exit the chroot {{{
435   echo "Finished chroot installation, exiting."
436   exit 0
437 # }}}
438
439 ## END OF FILE #################################################################
440 # vim: ai tw=80 expandtab foldmethod=marker