Support stages, update manapge for lenny
[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 Nov 18 10:32:33 CET 2006 [mika]
8 ################################################################################
9
10 set -e # exit on any error
11
12 . /etc/debootstrap/config || exit 1
13
14 [ -r /proc/1 ] || mount -t proc   none /proc
15
16 if [ -n "$CHROOTMIRROR" ] ; then
17    echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" > /etc/apt/sources.list
18 fi
19
20 if [ -n "$GRMLREPOS" ] ; then
21    echo 'deb     http://grml.org/repos/ ./' >> /etc/apt/sources.list
22 fi
23
24 if ! [ -r /etc/kernel-img.conf ] ; then
25    echo "Setting up /etc/kernel-img.conf"
26    cat > /etc/kernel-img.conf << EOF
27 # Kernel Image management overrides
28 # See kernel-img.conf(5) for details
29 do_initrd = Yes
30 do_symlinks = Yes
31 EOF
32 fi
33
34 # create default devices
35 if ! [ -r /dev/hda20 ] ; then
36    echo "Creating generic devices in /dev - this might take a while..."
37    cd /dev && MAKEDEV generic
38 fi
39
40 # install additional packages
41 if [ "$PACKAGES" = 'yes' ] ; then
42    if ! [ -r /etc/debootstrap/packages ] ; then
43      echo "Error: /etc/debootstrap/packages not found, exiting."
44      exit 1
45    else
46      apt-get update
47      apt-get --force-yes -y install $(cat /etc/debootstrap/packages) $GRMLPACKAGES
48    fi
49 fi
50
51 # sarge specific stuff
52 if [ "$RELEASE" = 'sarge' ] ; then
53    sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
54 fi
55
56 if [ -n "$KERNEL" ] ; then
57    apt-get update
58    if [ "$RELEASE" = 'sarge' ] ; then
59       KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
60    else
61       KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
62    fi
63    apt-get --force-yes -y install $KERNELPACKAGES
64 fi
65
66 if [ -n "$RECONFIGURE" ] ; then
67    for package in $RECONFIGURE ; do
68        dpkg --list $package 1>/dev/null 2>/dev/null && dpkg-reconfigure $package || echo "Warning: $package does not exist, can not reconfigure it."
69    done
70 fi
71
72 # set password of user root
73 echo "Activating shadow passwords."
74 shadowconfig on
75 echo "Setting password for user root:"
76 set +e # do not exit if passwd returns error due to missmatching passwords
77 passwd
78 echo ""
79 set -e # restore default behaviour again
80
81 if ! [ -f /etc/hosts ] ; then
82    echo "Setting up /etc/hosts"
83    echo "127.0.0.1       localhost  $HOSTNAME" > /etc/hosts
84 fi
85
86 if ! [ -f /etc/network/interfaces ] ; then
87    echo "Setting up /etc/network/interfaces"
88    cat >> /etc/network/interfaces << EOF
89 iface lo inet loopback
90 iface eth0 inet dhcp
91 auto lo
92 auto eth0
93 EOF
94 fi
95
96 # adjust timezone
97 if [ -n "$TIMEZONE" ] ; then
98    echo "Adjusting /etc/localtime"
99    ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
100 fi
101
102 function createfstab(){
103    echo "Setting up /etc/fstab"
104 cat > /etc/fstab << EOF
105 $TARGET      /            auto    defaults,errors=remount-ro 0   1
106 /sys           /sys         sysfs   rw,nosuid,nodev,noexec     0   0
107 proc           /proc        proc    defaults                   0   0
108 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto             0   0
109 # some other examples:
110 # /dev/sda2       none         swap    sw                   0   0
111 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
112 # //1.2.3.4/pub   /smb/pub     smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
113 # linux:/pub      /beer        nfs     defaults             0  0
114 # tmpfs           /tmp         tmpfs   size=300M            0  0
115 EOF
116 }
117
118 # set up /etc/fstab if file is not present (cdebootstrap)
119 if [ ! -f /etc/fstab  ] ; then
120    createfstab
121 fi
122
123 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
124 if grep -q UNCONFIGURED /etc/fstab ; then
125    createfstab
126 fi
127
128 # set up hostname
129 if [ -n "$HOSTNAME" ] ; then
130    echo "Setting hostname to ${HOSTNAME}."
131    echo "$HOSTNAME" > /etc/hostname
132 fi
133
134 # assume the first available kernel as our main kernel
135 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
136 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
137
138 # generate initrd
139 if [ -n "$INITRD" ] ; then
140    if [ "$RELEASE" = 'sarge' ] ; then
141       echo "Release sarge detected, will not create an initrd."
142    else
143       echo "Generating initrd."
144       update-initramfs -c -t -k $KERNELVER
145       if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
146          GRUBINITRD="initrd          /boot/initrd.img-$KERNELVER"
147          LILOINITRD="        initrd=/boot/initrd.img-$KERNELVER"
148       fi
149    fi
150 fi
151
152 if [ -z "$GROOT" ] ; then
153    echo "Warning: \$GROOT is not defined, can not adjust grub configuration therefor."
154 else
155    echo "Adjusting grub configuration for use on ${GROOT}."
156
157    # copy stage-files to /boot/grub/
158    [ -d /boot/grub/ ] || mkdir /boot/grub
159    if [ -d /usr/lib/grub/i386-pc/ ] ; then
160       cp /usr/lib/grub/i386-pc/* /boot/grub/
161    else
162       # sarge ships grub files in another directory
163       cp /lib/grub/i386-pc/* /boot/grub/
164    fi
165
166    # finally install grub
167    update-grub -y
168    sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
169    sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro|g" /boot/grub/menu.lst
170    # not sure why savedefault does not work for me; any ideas?
171    sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
172    update-grub -y
173 fi
174
175 # make sure we don't have any running processes left
176 for service in ssh mdadm mdadm-raid ; do
177     [ -x "/etc/init.d/$service" ] && "/etc/init.d/$service" stop
178 done
179
180 # unmount all filesystems in chroot, make sure nothing is left...
181 umount -a    1>/dev/null 2>/dev/null || true
182 umount /proc 1>/dev/null 2>/dev/null || true
183 umount /proc 1>/dev/null 2>/dev/null || true
184 umount -a    1>/dev/null 2>/dev/null || true
185
186 # finally exit the chroot
187 echo "Finished chroot installation, exiting."
188 exit 0
189
190 ## END OF FILE #################################################################