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