Merge branch 'master' of https://github.com/zeha/grml-debootstrap
[grml-debootstrap.git] / packer / debian64_provision.sh
1 #!/bin/bash
2
3 set -e
4
5 [ -f /etc/grml_cd ] || { echo "File /etc/grml_cd doesn't exist, not executing script to avoid data loss." >&2 ; exit 1 ; }
6
7 TARGET=/mnt
8
9 # if we notice an error then do NOT immediately return but provide
10 # user a chance to debug the VM
11 bailout() {
12   echo "* Noticed problem during execution, sleeping for 9999 seconds to provide debugging option"
13   sleep 9999
14   echo "* Finally exiting with return code 1"
15   exit 1
16 }
17 trap bailout ERR
18
19 echo "* Executing automated partition setup"
20 cat > /tmp/partition_setup.txt << EOF
21 disk_config sda disklabel:msdos bootable:1
22 primary / 800M- ext4 rw
23 EOF
24
25 export LOGDIR='/tmp/setup-storage'
26 mkdir -p $LOGDIR
27
28 export disklist=$(/usr/lib/fai/fai-disk-info | sort)
29 PATH=/usr/lib/fai:${PATH} setup-storage -f /tmp/partition_setup.txt -X
30
31 echo "* Making sure we use latest grml-debootstrap version"
32 apt-get update
33 apt-get -y install grml-debootstrap
34
35 # TODO - support testing the version provided by the ISO (without upgrading)
36 if [ -r /tmp/grml-debootstrap ] ; then
37   echo "* Found /tmp/grml-debootstrap - considering for usage as main grml-debootstrap script"
38   GRML_DEBOOTSTRAP="bash /tmp/grml-debootstrap"
39 else
40   GRML_DEBOOTSTRAP=grml-debootstrap
41 fi
42
43 echo "* Installing Debian"
44 $GRML_DEBOOTSTRAP --hostname wheezy --release wheezy --target /dev/sda1 --grub /dev/sda --password grml --force 2>&1 | tee -a /tmp/grml-debootstrap.log
45
46 echo "* Mounting target system"
47 mount /dev/sda1 ${TARGET}
48
49 echo "* Installing make + gcc packages for Virtualbox Guest Additions"
50 chroot ${TARGET} apt-get -y install make gcc dkms
51
52 echo "* Installing Virtualbox Guest Additions"
53 isofile="${HOME}/VBoxGuestAdditions.iso"
54
55 KERNELHEADERS=$(basename $(find $TARGET/usr/src/ -maxdepth 1 -name linux-headers\* ! -name \*common) | sort -u -r -V | head -1)
56 if [ -z "$KERNELHEADERS" ] ; then
57   echo "Error: no kernel headers found for building the VirtualBox Guest Additions kernel module." >&2
58   exit 1
59 fi
60
61 KERNELVERSION=${KERNELHEADERS##linux-headers-}
62 if [ -z "$KERNELVERSION" ] ; then
63   echo "Error: no kernel version could be identified." >&2
64   exit 1
65 fi
66
67 cp /tmp/fake-uname.so "${TARGET}/tmp/fake-uname.so"
68 mkdir -p "${TARGET}/media/cdrom"
69 mountpoint "${TARGET}/media/cdrom" >/dev/null && umount "${TARGET}/media/cdrom"
70 mount -t iso9660 $isofile "${TARGET}/media/cdrom/"
71 UTS_RELEASE=$KERNELVERSION LD_PRELOAD=/tmp/fake-uname.so grml-chroot "$TARGET" /media/cdrom/VBoxLinuxAdditions.run --nox11 || true
72 tail -10 "${TARGET}/var/log/VBoxGuestAdditions.log"
73 umount "${TARGET}/media/cdrom/"
74
75 # work around regression in virtualbox-guest-additions-iso 4.3.10
76 if [ -d ${TARGET}/opt/VBoxGuestAdditions-4.3.10 ] ; then
77   ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions ${TARGET}/usr/lib/VBoxGuestAdditions
78 fi
79
80 echo "* Setting password for user root to 'vagrant'"
81 echo root:vagrant | chroot ${TARGET} chpasswd
82
83 echo "* Installing sudo package"
84 chroot ${TARGET} apt-get -y install sudo
85
86 echo "* Adding Vagrant user"
87 chroot ${TARGET} useradd -d /home/vagrant -m -u 1000 vagrant
88
89 echo "* Installing Vagrant ssh key"
90 mkdir -m 0700 -p ${TARGET}/home/vagrant/.ssh
91 echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key" >> ${TARGET}/home/vagrant/.ssh/authorized_keys
92 chmod 0600 ${TARGET}/home/vagrant/.ssh/authorized_keys
93 chroot ${TARGET} chown vagrant:vagrant /home/vagrant/.ssh /home/vagrant/.ssh/authorized_keys
94
95 echo "* Setting up sudo configuration for user vagrant"
96 echo "vagrant ALL=(ALL) NOPASSWD: ALL" > ${TARGET}/etc/sudoers.d/vagrant
97
98 host="$(cat ${TARGET}/etc/hostname)"
99 if ! grep -q "${host}$" "${TARGET}"/etc/hosts ; then
100   echo "* Setting up localhost entry for hostname $host in /etc/hosts"
101   cat >> "${TARGET}"/etc/hosts << EOF
102 # Added by grml-debootstrap/provision to make sure host is resolvable for sudo:
103 127.0.0.2 ${host}.local $host
104
105 EOF
106 fi
107
108 echo "* Setting up stdin/tty workaround in /root/.profile"
109 sed -i "s;^mesg n$;# modified via grml-debootstrap/provision script to work around stdin/tty issue:\ntty -s \&\& mesg n;g" "${TARGET}"/root/.profile
110
111 if [ -f ${TARGET}/etc/ssh/sshd_config ] && ! grep -q '^UseDNS' ${TARGET}/etc/ssh/sshd_config ; then
112   echo "* Disabling UseDNS in sshd config"
113   echo "UseDNS no" >> ${TARGET}/etc/ssh/sshd_config
114 fi
115
116 echo "* Logging build information to /etc/grml_debootstrap.info"
117 echo "System generated by grml-debootstrap/provision on $(date)" > ${TARGET}/etc/grml_debootstrap.info
118
119 echo "* Cleaning up apt stuff"
120 chroot ${TARGET} apt-get clean
121 rm -f ${TARGET}/var/lib/apt/lists/*Packages \
122       ${TARGET}/var/lib/apt/lists/*Release \
123       ${TARGET}/var/lib/apt/lists/*Sources \
124       ${TARGET}/var/lib/apt/lists/*Index* \
125       ${TARGET}/var/lib/apt/lists/*Translation* \
126       ${TARGET}/var/lib/apt/lists/*.gpg \
127       ${TARGET}/var/cache/apt-show-versions/* \
128       ${TARGET}/var/cache/debconf/*.dat-old \
129       ${TARGET}/var/cache/apt/*.bin \
130       ${TARGET}/var/lib/aptitude/pkgstates.old
131
132 echo "* Unmounting target system"
133 umount ${TARGET}
134
135 echo "* Checking for bats"
136 if dpkg --list bats >/dev/null 2>&1 ; then
137   echo "* bats is already present, nothing to do."
138 else
139   echo "* Installing bats"
140   apt-get update
141   apt-get -y install bats
142   # dpkg -i /tmp/bats*deb
143 fi
144
145 echo "* Running tests to verify grml-debootstrap system"
146 bats /tmp/debian64.bats -t
147
148 echo "* Finished execution of $0"