one package per line
[grml-debootstrap.git] / packer / debian64_provision.sh
1 #!/bin/bash
2
3 set -e
4 set -o pipefail
5 set -o errtrace
6 set -o functrace
7
8 # defaults
9 GRML_DEBOOTSTRAP='grml-debootstrap'
10 TARGET='/mnt'
11 INSTALL_TARGET='/dev/sda1'
12 GRUB_TARGET='/dev/sda'
13
14 ## validation / checks
15 [ -f /etc/grml_cd ] || { echo "File /etc/grml_cd doesn't exist, not executing script to avoid data loss." >&2 ; exit 1 ; }
16
17 if [ -z "$DEBIAN_VERSION" ] ; then
18   echo "* Error: DEBIAN_VERSION environment variable is undefined. Exiting." >&2
19   exit 1
20 fi
21
22 ## debugging
23 # if we notice an error then do NOT immediately return but provide
24 # user a chance to debug the VM
25 bailout() {
26   echo "* Noticed problem during execution (line ${1}, exit code ${2}), sleeping for 9999 seconds to provide debugging option"
27   sleep 9999
28   echo "* Finally exiting with return code 1"
29   exit "$2"
30 }
31 trap 'bailout ${LINENO} $?' ERR
32
33 ## helper functions
34 virtualbox_setup() {
35   case "$DEBIAN_VERSION" in
36     lenny)
37       echo "* Debian lenny doesn't support Virtualbox Guest Additions, skipping."
38       return 0
39       ;;
40   esac
41
42   if ! mountpoint "${TARGET}" &>/dev/null ; then
43     echo "* Mounting target system"
44     mount "${INSTALL_TARGET}" "${TARGET}"
45   fi
46
47   echo "* Installing packages for Virtualbox Guest Additions"
48   chroot ${TARGET} apt-get -y install make gcc dkms
49
50   echo "* Installing Virtualbox Guest Additions"
51   isofile="${HOME}/VBoxGuestAdditions.iso"
52
53   KERNELHEADERS=$(basename $(find $TARGET/usr/src/ -maxdepth 1 -name linux-headers\* ! -name \*common) | sort -u -r -V | head -1)
54   if [ -z "$KERNELHEADERS" ] ; then
55     echo "Error: no kernel headers found for building the VirtualBox Guest Additions kernel module." >&2
56     exit 1
57   fi
58
59   KERNELVERSION=${KERNELHEADERS##linux-headers-}
60   if [ -z "$KERNELVERSION" ] ; then
61     echo "Error: no kernel version could be identified." >&2
62     exit 1
63   fi
64
65   cp /tmp/fake-uname.so "${TARGET}/tmp/fake-uname.so"
66   mkdir -p "${TARGET}/media/cdrom"
67   mountpoint "${TARGET}/media/cdrom" >/dev/null && umount "${TARGET}/media/cdrom"
68   mount -t iso9660 $isofile "${TARGET}/media/cdrom/"
69   UTS_RELEASE=$KERNELVERSION LD_PRELOAD=/tmp/fake-uname.so grml-chroot "$TARGET" /media/cdrom/VBoxLinuxAdditions.run --nox11 || true
70   tail -10 "${TARGET}/var/log/VBoxGuestAdditions.log"
71   umount "${TARGET}/media/cdrom/"
72
73   # work around bug in VirtualBox 4.3.18 which leaves process behind,
74   # causing unmount of "$TARGET" to fail
75   grml-chroot "$TARGET" /etc/init.d/vboxadd-service stop || true
76   # left behind by VBoxService
77   umount "$TARGET"/dev || true
78
79   # work around regression in virtualbox-guest-additions-iso 4.3.10
80   if [ -d ${TARGET}/opt/VBoxGuestAdditions-4.3.10 ] ; then
81     ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions ${TARGET}/usr/lib/VBoxGuestAdditions
82   fi
83
84   if mountpoint "${TARGET}" &>/dev/null ; then
85     echo "* Unmounting target system"
86     umount "${TARGET}"
87   fi
88 }
89
90 vagrant_setup() {
91   if ! mountpoint "${TARGET}" &>/dev/null ; then
92     echo "* Mounting target system"
93     mount "${INSTALL_TARGET}" "${TARGET}"
94   fi
95
96   echo "* Setting password for user root to 'vagrant'"
97   echo root:vagrant | chroot ${TARGET} chpasswd
98
99   echo "* Installing sudo package"
100   chroot ${TARGET} apt-get -y install sudo
101
102   echo "* Adding Vagrant user"
103   chroot ${TARGET} useradd -d /home/vagrant -m -u 1000 vagrant -s /bin/bash
104
105   echo "* Installing Vagrant ssh key"
106   mkdir -m 0700 -p ${TARGET}/home/vagrant/.ssh
107   echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key" >> ${TARGET}/home/vagrant/.ssh/authorized_keys
108   chmod 0600 ${TARGET}/home/vagrant/.ssh/authorized_keys
109   chroot ${TARGET} chown vagrant:vagrant /home/vagrant/.ssh /home/vagrant/.ssh/authorized_keys
110
111   echo "* Setting up sudo configuration for user vagrant"
112   if ! [ -d "${TARGET}/etc/sudoers.d" ] ; then # lenny:
113     echo "vagrant ALL=(ALL) NOPASSWD: ALL" >> "${TARGET}/etc/sudoers"
114   else # wheezy and newer:
115     echo "vagrant ALL=(ALL) NOPASSWD: ALL" > "${TARGET}/etc/sudoers.d/vagrant"
116     chmod 0440 "${TARGET}/etc/sudoers.d/vagrant"
117   fi
118
119   host="$(cat ${TARGET}/etc/hostname)"
120   if ! grep -q "${host}$" "${TARGET}"/etc/hosts ; then
121     echo "* Setting up localhost entry for hostname $host in /etc/hosts"
122     cat >> "${TARGET}"/etc/hosts << EOF
123 # Added by grml-debootstrap/provision to make sure host is resolvable for sudo:
124 127.0.0.2 ${host}.local $host
125
126 EOF
127   fi
128
129   echo "* Setting up stdin/tty workaround in /root/.profile"
130   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
131
132   if [ -f ${TARGET}/etc/ssh/sshd_config ] && ! grep -q '^UseDNS' ${TARGET}/etc/ssh/sshd_config ; then
133     echo "* Disabling UseDNS in sshd config"
134     echo "UseDNS no" >> ${TARGET}/etc/ssh/sshd_config
135   fi
136
137   if mountpoint "${TARGET}" &>/dev/null ; then
138     echo "* Unmounting target system"
139     umount "${TARGET}"
140   fi
141 }
142
143 partition_setup() {
144   echo "* Executing automated partition setup"
145   cat > /tmp/partition_setup.txt << EOF
146 disk_config sda disklabel:msdos bootable:1
147 primary / 800M- ext4 rw
148 EOF
149
150   export LOGDIR='/tmp/setup-storage'
151   mkdir -p $LOGDIR
152
153   export disklist=$(/usr/lib/fai/fai-disk-info | sort)
154   PATH=/usr/lib/fai:${PATH} setup-storage -f /tmp/partition_setup.txt -X
155 }
156
157 sources_list_setup() {
158   # This is ugly because it's 'testing' no matter what ISO we're using, but otherwise we're running into
159   # W: Failed to fetch http://snapshot.debian.org/archive/debian/20141114/dists/testing/main/binary-amd64/Packages  404  Not Found [IP: 193.62.202.30 80]
160   echo "* Setting up /etc/apt/sources.list.d/debian.list to avoid snapshot.debian.org usage causing possible failures"
161   cat > /etc/apt/sources.list.d/debian.list << EOF
162 deb http://ftp.debian.org/debian testing main
163 EOF
164 }
165
166 grml_debootstrap_setup() {
167   echo "* grml-debootstrap setup"
168   if [ "$GRML_DEBOOTSTRAP_VERSION" = "latest" ] ; then
169     echo "** GRML_DEBOOTSTRAP_VERSION is set to '$GRML_DEBOOTSTRAP_VERSION'"
170     echo "** Setting up grml-debootstrap from CI repository from jenkins.grml.org"
171     cat > /etc/apt/sources.list.d/grml-debootstrap.list << EOF
172 deb     http://jenkins.grml.org/debian grml-debootstrap main
173 EOF
174     wget -O - http://jenkins.grml.org/debian/C525F56752D4A654.asc | apt-key add -
175     apt-get update
176     apt-get -y install grml-debootstrap
177   elif [ "$GRML_DEBOOTSTRAP_VERSION" = "stable" ] ; then
178     echo "** GRML_DEBOOTSTRAP_VERSION is set to '$GRML_DEBOOTSTRAP_VERSION'"
179     echo "** Using latest stable grml-debootstrap version"
180     apt-get update
181     apt-get -y install grml-debootstrap
182   elif [ "$GRML_DEBOOTSTRAP_VERSION" = "git" ] ; then
183     echo "** GRML_DEBOOTSTRAP_VERSION is set to '$GRML_DEBOOTSTRAP_VERSION'"
184     echo "** Using grml-debootstrap from Git repository"
185     git clone git://git.grml.org/grml-debootstrap.git
186     cd grml-debootstrap
187     GRML_DEBOOTSTRAP="CONFFILES=$(pwd) $(pwd)/grml-debootstrap"
188   elif [ "$GRML_DEBOOTSTRAP_VERSION" = "local" ] ; then
189     echo "** GRML_DEBOOTSTRAP_VERSION is set to '$GRML_DEBOOTSTRAP_VERSION'"
190     echo "** Using /tmp/grml-debootstrap derived from local system"
191     cd /tmp/grml-debootstrap
192     export CONFFILES=$(pwd)/etc/debootstrap
193     GRML_DEBOOTSTRAP="bash $(pwd)/usr/sbin/grml-debootstrap"
194   elif [ "$GRML_DEBOOTSTRAP_VERSION" = "iso" ] ; then
195     echo "** GRML_DEBOOTSTRAP_VERSION is set to '$GRML_DEBOOTSTRAP_VERSION'"
196     echo "** Using grml-debootstrap as provided on ISO"
197   fi
198 }
199
200 verify_debootstrap_version() {
201   local required_version=1.0.65
202   local present_version=$(dpkg-query --show --showformat='${Version}' debootstrap)
203
204   if dpkg --compare-versions $present_version lt $required_version ; then
205     echo "** debootstrap version $present_version is older than minimum required version $required_version - upgrading."
206     apt-get update
207     apt-get -y install debootstrap
208   fi
209 }
210
211 grml_debootstrap_execution() {
212   echo "* Installing Debian"
213
214   # release specific stuff
215   case "$DEBIAN_VERSION" in
216     lenny)
217       GRML_DEB_OPTIONS="--mirror http://archive.debian.org/debian/ --filesystem ext3"
218       ;;
219     stretch)
220       verify_debootstrap_version
221       ;;
222   esac
223
224   echo "** Executing: $GRML_DEBOOTSTRAP --hostname $DEBIAN_VERSION --release $DEBIAN_VERSION --target ${INSTALL_TARGET} --grub ${GRUB_TARGET} --password grml --force $GRML_DEB_OPTIONS" | tee -a /tmp/grml-debootstrap.log
225   $GRML_DEBOOTSTRAP --hostname "${DEBIAN_VERSION}" --release "${DEBIAN_VERSION}" --target "${INSTALL_TARGET}" --grub "${GRUB_TARGET}" --password grml --force $GRML_DEB_OPTIONS 2>&1 | tee -a /tmp/grml-debootstrap.log
226 }
227
228 apply_nic_workaround() {
229   # release specific stuff
230   case "$DEBIAN_VERSION" in
231     stretch|buster|bullseye|unstable|sid)
232       ;;
233     *)
234       echo "* Debian $DEBIAN_VERSION doesn't require NIC workaround"
235       return 0
236       ;;
237   esac
238
239   if ! mountpoint "${TARGET}" &>/dev/null ; then
240     echo "* Mounting target system"
241     mount "${INSTALL_TARGET}" "${TARGET}"
242   fi
243
244   echo "* Disabling predictable network interface names for Debian $DEBIAN_VERSION"
245   ln -s /dev/null "${TARGET}/etc/udev/rules.d/80-net-setup-link.rules"
246
247   echo "* Rebuilding initramfs to include udev configuration change"
248   chroot "${TARGET}" update-initramfs -u -k all
249 }
250
251 log_system_information() {
252   if ! mountpoint "${TARGET}" &>/dev/null ; then
253     echo "* Mounting target system"
254     mount "${INSTALL_TARGET}" "${TARGET}"
255   fi
256
257   local debian_version="$(cat ${TARGET}/etc/debian_version)"
258   echo "* Installed Debian version $debian_version"
259
260   echo "* Logging build information to /etc/grml_debootstrap.info"
261   echo "Debian $debian_version installed by grml-debootstrap/provision on $(date)" > ${TARGET}/etc/grml_debootstrap.info
262   $GRML_DEBOOTSTRAP --version | head -1 >> ${TARGET}/etc/grml_debootstrap.info || true
263
264   if mountpoint "${TARGET}" &>/dev/null ; then
265     echo "* Unmounting target system"
266     umount "${TARGET}"
267   fi
268 }
269
270 clean_apt_files() {
271   if ! mountpoint "${TARGET}" &>/dev/null ; then
272     echo "* Mounting target system"
273     mount "${INSTALL_TARGET}" "${TARGET}"
274   fi
275
276   echo "* Cleaning up apt stuff"
277   chroot ${TARGET} apt-get clean
278   rm -f ${TARGET}/var/lib/apt/lists/*Packages \
279     ${TARGET}/var/lib/apt/lists/*Release \
280     ${TARGET}/var/lib/apt/lists/*Sources \
281     ${TARGET}/var/lib/apt/lists/*Index* \
282     ${TARGET}/var/lib/apt/lists/*Translation* \
283     ${TARGET}/var/lib/apt/lists/*.gpg \
284     ${TARGET}/var/cache/apt-show-versions/* \
285     ${TARGET}/var/cache/debconf/*.dat-old \
286     ${TARGET}/var/cache/apt/*.bin \
287     ${TARGET}/var/lib/aptitude/pkgstates.old
288
289   if mountpoint "${TARGET}" &>/dev/null ; then
290     echo "* Unmounting target system"
291     umount "${TARGET}"
292   fi
293 }
294
295 automated_tests() {
296   echo "* Checking for bats"
297   if dpkg --list bats >/dev/null 2>&1 ; then
298     echo "* bats is already present, nothing to do."
299   else
300     echo "* Installing bats"
301     apt-get update
302     apt-get -y install bats
303   fi
304
305   echo "* Running tests to verify grml-debootstrap system"
306   bats /tmp/debian64.bats -t
307 }
308
309 ## main execution
310 sources_list_setup
311 partition_setup
312 grml_debootstrap_setup
313 grml_debootstrap_execution
314 virtualbox_setup
315 vagrant_setup
316 apply_nic_workaround
317 log_system_information
318 clean_apt_files
319 automated_tests
320
321 echo "* Finished execution of $0"