7192cdc81ce12a63dbc22325be7127ab09be7ace
[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: Fre Nov 03 23:42:36 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 passwd
73 echo ""
74
75 if ! [ -f /etc/hosts ] ; then
76    echo "Setting up /etc/hosts"
77    echo "127.0.0.1       localhost  $HOSTNAME" > /etc/hosts
78 fi
79
80 if ! [ -f /etc/network/interfaces ] ; then
81    echo "Setting up /etc/network/interfaces"
82    cat >> /etc/network/interfaces << EOF
83 iface lo inet loopback
84 iface eth0 inet dhcp
85 auto lo
86 auto eth0
87 EOF
88 fi
89
90 # adjust timezone
91 if [ -n "$TIMEZONE" ] ; then
92    echo "Adjusting /etc/localtime"
93    ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
94 fi
95
96 function createfstab(){
97    echo "Setting up /etc/fstab"
98 cat > /etc/fstab << EOF
99 $TARGET      /            auto    defaults,errors=remount-ro 0   1
100 /sys           /sys         sysfs   rw,nosuid,nodev,noexec     0   0
101 proc           /proc        proc    defaults                   0   0
102 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto             0   0
103 # some other examples:
104 # /dev/sda2       none         swap    sw                   0   0
105 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
106 # //1.2.3.4/pub   /smb/pub     smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
107 # linux:/pub      /beer        nfs     defaults             0  0
108 # tmpfs           /tmp         tmpfs   size=300M            0  0
109 EOF
110 }
111
112 # set up /etc/fstab if file is not present (cdebootstrap)
113 if [ ! -f /etc/fstab  ] ; then
114    createfstab
115 fi
116
117 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
118 if grep -q UNCONFIGURED /etc/fstab ; then
119    createfstab
120 fi
121
122 # set up hostname
123 if [ -n "$HOSTNAME" ] ; then
124    echo "Setting hostname to ${HOSTNAME}."
125    echo "$HOSTNAME" > /etc/hostname
126 fi
127
128 # assume the first available kernel as our main kernel
129 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
130 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
131
132 # generate initrd
133 if [ -n "$INITRD" ] ; then
134    if [ "$RELEASE" = 'sarge' ] ; then
135       einfo "Release sarge detected, will not create an initrd."
136       return 0
137    fi
138    echo "Generating initrd."
139    update-initramfs -c -t -k $KERNELVER
140    if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
141       GRUBINITRD="initrd          /boot/initrd.img-$KERNELVER"
142       LILOINITRD="        initrd=/boot/initrd.img-$KERNELVER"
143    fi
144 fi
145
146 if [ -n "$GROOT" ] ; then
147    echo "Installing grub"
148
149    # copy stage-files to /boot/grub/
150    [ -d /boot/grub/ ] || mkdir /boot/grub
151    cp /usr/lib/grub/i386-pc/* /boot/grub/
152
153    # finally install grub
154    update-grub -y
155    sed -i "s/groot=.*/groot=(${GROOT})/g" /boot/grub/menu.lst
156    # not sure why savedefault does not work for me; any ideas?
157    sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
158    update-grub -y
159
160    if [ -z "$MBR" ] ; then
161       echo "Notice: \$MBR not set, will not install grub therefor."
162    else
163       cp /proc/mounts /etc/mtab
164       grub-install "$MBR"
165       rm /etc/mtab
166    fi
167 fi
168
169 # make sure we don't have any running processes left
170 [ -x /etc/init.d/ssh ] && /etc/init.d/ssh stop
171
172 # unmount all filesystems in chroot, make sure nothing is left...
173 umount -a    1>/dev/null 2>/dev/null || true
174 umount /proc 1>/dev/null 2>/dev/null || true
175 umount /proc 1>/dev/null 2>/dev/null || true
176 umount -a    1>/dev/null 2>/dev/null || true
177
178 # finally exit the chroot
179 echo "Finished chroot installation, exiting."
180 exit 0
181
182 ## END OF FILE #################################################################