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