bab8dc4324d0fb917c538354e327a3d8c32787a4
[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 23 11:12: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 "$CHROOTMIRROR" ] ; then
49      echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" > /etc/apt/sources.list
50   fi
51 }
52 # }}}
53
54 # set up grml repository {{{
55 grmlrepos() {
56   if [ -n "$GRMLREPOS" ] ; then
57      cat >> /etc/apt/sources.list << EOF
58
59 # grml: stable repository:
60   deb     http://deb.grml.org/ grml-stable  main
61   deb-src http://deb.grml.org/ grml-stable  main
62
63 # grml: testing/development repository:
64   deb     http://deb.grml.org/ grml-testing main
65   deb-src http://deb.grml.org/ grml-testing main
66
67 EOF
68      # make sure we have the keys available for aptitude
69      gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
70      gpg --export F61E2E7CECDEA787 | apt-key add - || /bin/true # not yet sure
71      # why it's necessary, sometimes we get an error even though it works [mika]
72   fi
73 }
74 # }}}
75
76 # set up kernel-img.conf {{{
77 kernelimg_conf() {
78   if ! [ -r /etc/kernel-img.conf ] ; then
79      echo "Setting up /etc/kernel-img.conf"
80      cat > /etc/kernel-img.conf << EOF
81 # Kernel Image management overrides
82 # See kernel-img.conf(5) for details
83 do_initrd = Yes
84 do_symlinks = Yes
85 EOF
86   fi
87 }
88 # }}}
89
90 # create default devices {{{
91 makedev() {
92   if ! [ -r /dev/hda20 ] ; then
93      echo "Creating generic devices in /dev - this might take a while..."
94      cd /dev && MAKEDEV generic
95   fi
96 }
97 # }}}
98
99 # install additional packages {{{
100 packages() {
101   if [ "$PACKAGES" = 'yes' ] ; then
102      if ! [ -r /etc/debootstrap/packages ] ; then
103        echo "Error: /etc/debootstrap/packages not found, exiting."
104        exit 1
105      else
106        $APTUPDATE
107        DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(cat /etc/debootstrap/packages) $GRMLPACKAGES
108      fi
109   fi
110 }
111 # }}}
112
113 # install extra packages {{{
114 extrapackages() {
115     if [ "$EXTRAPACKAGES" = 'yes' ] ; then
116         PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
117         if [ -n "$PACKAGELIST" ]; then
118             dpkg -i $PACKAGELIST
119             # run apt again to resolve any deps
120             DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
121         fi
122     fi
123 }
124 # }}}
125
126 #  sarge specific stuff: mkinitrd {{{
127 mkinitrd() {
128   if [ "$RELEASE" = 'sarge' ] ; then
129      sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
130   fi
131 }
132 # }}}
133
134 # install kernel packages {{{
135 kernel() {
136   # do not override $KERNEL if set via config file
137   if [ -z "$KERNEL" ] ; then
138      if [ "$ARCH" = 'i386' ] ; then
139         KERNEL='2.6-686'
140      elif [ "$ARCH" = 'amd64' ] ; then
141         KERNEL='2.6-amd64'
142      fi
143   fi
144
145   if [ -n "$KERNEL" ] ; then
146      $APTUPDATE
147      if [ "$RELEASE" = 'sarge' ] ; then
148         KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
149      else
150         KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
151      fi
152       DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
153   fi
154 }
155 # }}}
156
157 # reconfigure packages {{{
158 reconfigure() {
159   if [ -n "$RECONFIGURE" ] ; then
160      for package in $RECONFIGURE ; do
161          dpkg --list $package 1>/dev/null 2>/dev/null && \
162          DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
163          echo "Warning: $package does not exist, can not reconfigure it."
164      done
165   fi
166 }
167 # }}}
168
169 # set password of user root {{{
170 setpassword() {
171 # Set a password, via chpasswd.
172 # Use perl rather than echo, to avoid the password
173 # showing in the process table. (However, this is normally
174 # only called when first booting the system, when root has no
175 # password at all, so that should be an unnecessary precaution).
176 #
177 # Pass in three arguments: the user, the password, and 'true' if the
178 # password has been pre-crypted (by preseeding).
179 #
180 # Taken from /var/lib/dpkg/info/passwd.config
181         SETPASSWD_PW="$2"
182         export SETPASSWD_PW
183
184         # This is very annoying. chpasswd cannot handle generating md5
185         # passwords as it is not PAM-aware. Thus, I have to work around
186         # that by crypting the password myself if md5 is used.
187         USE_MD5=1
188         export USE_MD5
189
190         if [ "$3" = true ]; then
191                 PRECRYPTED=1
192         else
193                 PRECRYPTED=''
194         fi
195         export PRECRYPTED
196         LC_ALL=C LANGUAGE=C LANG=C perl -e '
197                 sub CreateCryptSalt {
198                         my $md5 = shift;
199
200                         my @valid = split(//, "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
201                         my ($in, $out);
202
203                         my $cryptsaltlen = ($md5 ? 8 : 2);
204
205                         open (F, "</dev/urandom") || die "No /dev/urandom found!";
206                         foreach (1..$cryptsaltlen) {
207                                 read(F, $in, 1);
208                                 $out .= $valid[ord($in) % ($#valid + 1)];
209                         }
210                         close F;
211                         return ($md5 ? "\$1\$$out\$" : $out);
212                 }
213
214                 open(P,"| chpasswd -e");
215                 if ($ENV{PRECRYPTED}) {
216                         print P shift().":$ENV{SETPASSWD_PW}\n";
217                 } else {
218                         print P shift().":".
219                                 crypt($ENV{SETPASSWD_PW}, CreateCryptSalt($ENV{USE_MD5})).
220                                 "\n";
221                 }
222                 close P;
223         ' "$1"
224         SETPASSWD_PW=''
225         USE_MD5=''
226         PRECRYPTED=''
227 }
228
229 passwords() {
230   echo "Activating shadow passwords."
231   shadowconfig on
232
233   if [ -n "$ROOTPASSWORD" ] ; then
234      setpassword root "$ROOTPASSWD" false
235      export ROOTPASSWD=''
236   else
237      echo "Setting password for user root:"
238      set +e # do not exit if passwd returns error due to missmatching passwords
239      passwd
240      echo ""
241      set -e # restore default behaviour again
242   fi
243 }
244 # }}}
245
246 # set up /etc/hosts {{{
247 hosts() {
248   if ! [ -f /etc/hosts ] ; then
249      echo "Setting up /etc/hosts"
250      echo "127.0.0.1       localhost  $HOSTNAME" > /etc/hosts
251   fi
252 }
253 # }}}
254
255 # set up /etc/network/interfaces {{{
256 interfaces() {
257   if ! [ -f /etc/network/interfaces ] ; then
258      echo "Setting up /etc/network/interfaces"
259      cat >> /etc/network/interfaces << EOF
260 iface lo inet loopback
261 iface eth0 inet dhcp
262 auto lo
263 auto eth0
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 cat > /etc/fstab << EOF
282 $TARGET      /            auto    defaults,errors=remount-ro 0   1
283 /sys           /sys         sysfs   rw,nosuid,nodev,noexec     0   0
284 proc           /proc        proc    defaults                   0   0
285 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto             0   0
286 # some other examples:
287 # /dev/sda2       none         swap    sw                   0   0
288 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
289 # //1.2.3.4/pub   /smb/pub     smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
290 # linux:/pub      /beer        nfs     defaults             0  0
291 # tmpfs           /tmp         tmpfs   size=300M            0  0
292 EOF
293 }
294 # }}}
295
296 # generate /etc/fstab {{{
297 fstab() {
298   # set up /etc/fstab if file is not present (cdebootstrap)
299   if [ ! -f /etc/fstab  ] ; then
300      createfstab
301   fi
302
303   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
304   if grep -q UNCONFIGURED /etc/fstab ; then
305      createfstab
306   fi
307 }
308 # }}}
309
310 # set up hostname {{{
311 hostname() {
312   if [ -n "$HOSTNAME" ] ; then
313      echo "Setting hostname to ${HOSTNAME}."
314      echo "$HOSTNAME" > /etc/hostname
315   fi
316 }
317 # }}}
318
319 # generate initrd/initramfs {{{
320 initrd() {
321   # assume the first available kernel as our main kernel
322   KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
323   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
324
325   # generate initrd
326   if [ -n "$INITRD" ] ; then
327      if [ "$RELEASE" = 'sarge' ] ; then
328         echo "Release sarge detected, will not create an initrd."
329      else
330         echo "Generating initrd."
331         update-initramfs -c -t -k $KERNELVER
332         if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
333            GRUBINITRD="initrd          /boot/initrd.img-$KERNELVER"
334            LILOINITRD="        initrd=/boot/initrd.img-$KERNELVER"
335         fi
336      fi
337   fi
338 }
339 # }}}
340
341 # grub configuration/installation {{{
342 grub() {
343   if [ -z "$GROOT" ] ; then
344      echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
345   else
346      echo "Adjusting grub configuration for use on ${GROOT}."
347
348      # copy stage-files to /boot/grub/
349      [ -d /boot/grub/ ] || mkdir /boot/grub
350      # i386 specific:
351      [ -d /usr/lib/grub/i386-pc ]   && cp /usr/lib/grub/i386-pc/* /boot/grub/
352      # amd64 specific:
353      [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
354      # sarge ships grub files in another directory
355      [ "$RELEASE" = 'sarge' ]       && cp /lib/grub/i386-pc/* /boot/grub/
356
357      # finally install grub
358      if [ -x /usr/sbin/update-grub ] ; then
359         UPDATEGRUB='/usr/sbin/update-grub'
360      else
361         UPDATEGRUB='/sbin/update-grub'
362      fi
363      $UPDATEGRUB -y
364      if [ -f /boot/grub/menu.lst ] ; then
365         sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
366         sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
367         # not sure why savedefault does not work for me; any ideas?
368         sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
369         $UPDATEGRUB -y
370      fi
371   fi
372 }
373 # }}}
374
375 # make sure we don't have any running processes left {{{
376 services() {
377   for service in ssh mdadm mdadm-raid ; do
378       [ -x "/etc/init.d/$service" ] && "/etc/init.d/$service" stop
379   done
380 }
381 # }}}
382
383 # unmount all filesystems in chroot, make sure nothing is left {{{
384 finalize() {
385   # make sure we don't leave any sensible data
386   rm -f /etc/debootstrap/variables
387   umount -a    1>/dev/null 2>/dev/null || true
388   umount /proc 1>/dev/null 2>/dev/null || true
389   umount /proc 1>/dev/null 2>/dev/null || true
390   umount -a    1>/dev/null 2>/dev/null || true
391 }
392 # }}}
393
394 # execute the functions {{{
395  for i in chrootmirror grmlrepos kernelimg_conf makedev packages extrapackages \
396      mkinitrd kernel reconfigure hosts interfaces timezone fstab hostname \
397      initrd grub passwords services finalize ; do
398     if stage $i ; then
399        $i && stage $i done || exit 1
400     fi
401   done
402 # }}}
403
404 # finally exit the chroot {{{
405   echo "Finished chroot installation, exiting."
406   exit 0
407 # }}}
408
409 ## END OF FILE #################################################################
410 # vim: ai tw=80 expandtab foldmethod=marker