Release new version 0.108
[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   if ! mountpoint "${TARGET}" &>/dev/null ; then
36     echo "* Mounting target system"
37     mount "${INSTALL_TARGET}" "${TARGET}"
38   fi
39
40   echo "* Installing packages for Virtualbox Guest Additions"
41   chroot ${TARGET} apt-get -y install make gcc dkms
42
43   echo "* Installing Virtualbox Guest Additions"
44   isofile="${HOME}/VBoxGuestAdditions.iso"
45
46   KERNELHEADERS=$(basename $(find $TARGET/usr/src/ -maxdepth 1 -name linux-headers\* ! -name \*common) | sort -u -r -V | head -1)
47   if [ -z "$KERNELHEADERS" ] ; then
48     echo "Error: no kernel headers found for building the VirtualBox Guest Additions kernel module." >&2
49     exit 1
50   fi
51
52   KERNELVERSION=${KERNELHEADERS##linux-headers-}
53   if [ -z "$KERNELVERSION" ] ; then
54     echo "Error: no kernel version could be identified." >&2
55     exit 1
56   fi
57
58   cp /tmp/fake-uname.so "${TARGET}/tmp/fake-uname.so"
59   mkdir -p "${TARGET}/media/cdrom"
60   mountpoint "${TARGET}/media/cdrom" >/dev/null && umount "${TARGET}/media/cdrom"
61   mount -t iso9660 $isofile "${TARGET}/media/cdrom/"
62   UTS_RELEASE=$KERNELVERSION LD_PRELOAD=/tmp/fake-uname.so grml-chroot "$TARGET" /media/cdrom/VBoxLinuxAdditions.run --nox11 || true
63   if grep -q "make: .*vboxguest.*Error 2" "${TARGET}/var/log/vboxadd-install.log" ; then
64     echo "Error: looks like a fatal error happened during installation of VirtualBox Guest Additions." >&2
65     exit 1
66   fi
67   [ -r "${TARGET}/var/log/VBoxGuestAdditions.log" ] && tail -10 "${TARGET}/var/log/VBoxGuestAdditions.log"
68   [ -r "${TARGET}/var/log/vboxadd-install.log" ]    && tail -10 "${TARGET}/var/log/vboxadd-install.log"
69   [ -r "${TARGET}/var/log/vboxadd-setup.log" ]      && tail -10 "${TARGET}/var/log/vboxadd-setup.log"
70   umount "${TARGET}/media/cdrom/"
71
72   # work around bug in VirtualBox 4.3.18 which leaves process behind,
73   # causing unmount of "$TARGET" to fail
74   grml-chroot "$TARGET" /etc/init.d/vboxadd-service stop || true
75   # left behind by VBoxService
76   umount "$TARGET"/dev || true
77
78   # work around regression in virtualbox-guest-additions-iso 4.3.10
79   if [ -d ${TARGET}/opt/VBoxGuestAdditions-4.3.10 ] ; then
80     ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions ${TARGET}/usr/lib/VBoxGuestAdditions
81   fi
82
83   if mountpoint "${TARGET}" &>/dev/null ; then
84     echo "* Unmounting target system"
85     umount "${TARGET}"
86   fi
87 }
88
89 vagrant_setup() {
90   if ! mountpoint "${TARGET}" &>/dev/null ; then
91     echo "* Mounting target system"
92     mount "${INSTALL_TARGET}" "${TARGET}"
93   fi
94
95   echo "* Setting password for user root to 'vagrant'"
96   echo root:vagrant | chroot ${TARGET} chpasswd
97
98   echo "* Installing sudo package"
99   chroot ${TARGET} apt-get -y install sudo
100
101   echo "* Adding Vagrant user"
102   chroot ${TARGET} useradd -d /home/vagrant -m -u 1000 vagrant -s /bin/bash
103
104   echo "* Installing Vagrant ssh key"
105   mkdir -m 0700 -p ${TARGET}/home/vagrant/.ssh
106   echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key" >> ${TARGET}/home/vagrant/.ssh/authorized_keys
107   chmod 0600 ${TARGET}/home/vagrant/.ssh/authorized_keys
108   chroot ${TARGET} chown vagrant:vagrant /home/vagrant/.ssh /home/vagrant/.ssh/authorized_keys
109
110   echo "* Setting up sudo configuration for user vagrant"
111   echo "vagrant ALL=(ALL) NOPASSWD: ALL" > "${TARGET}/etc/sudoers.d/vagrant"
112   chmod 0440 "${TARGET}/etc/sudoers.d/vagrant"
113
114   host="$(cat ${TARGET}/etc/hostname)"
115   if ! grep -q "${host}$" "${TARGET}"/etc/hosts ; then
116     echo "* Setting up localhost entry for hostname $host in /etc/hosts"
117     cat >> "${TARGET}"/etc/hosts << EOF
118 # Added by grml-debootstrap/provision to make sure host is resolvable for sudo:
119 127.0.0.2 ${host}.local $host
120
121 EOF
122   fi
123
124   echo "* Setting up stdin/tty workaround in /root/.profile"
125   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
126
127   if [ -f ${TARGET}/etc/ssh/sshd_config ] && ! grep -q '^UseDNS' ${TARGET}/etc/ssh/sshd_config ; then
128     echo "* Disabling UseDNS in sshd config"
129     echo "UseDNS no" >> ${TARGET}/etc/ssh/sshd_config
130   fi
131
132   if mountpoint "${TARGET}" &>/dev/null ; then
133     echo "* Unmounting target system"
134     umount "${TARGET}"
135   fi
136 }
137
138 partition_setup() {
139   echo "* Executing automated partition setup"
140   cat > /tmp/partition_setup.txt << EOF
141 disk_config sda disklabel:msdos bootable:1
142 primary / 800M- ext4 rw
143 EOF
144
145   export LOGDIR='/tmp/setup-storage'
146   mkdir -p $LOGDIR
147
148   export disklist=$(/usr/lib/fai/fai-disk-info | sort)
149   PATH=/usr/lib/fai:${PATH} setup-storage -f /tmp/partition_setup.txt -X
150 }
151
152 sources_list_setup() {
153   # This is ugly because it's 'testing' no matter what ISO we're using, but otherwise we're running into
154   # 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]
155   echo "* Setting up /etc/apt/sources.list.d/debian.list to avoid snapshot.debian.org usage causing possible failures"
156   cat > /etc/apt/sources.list.d/debian.list << EOF
157 deb http://ftp.debian.org/debian testing main
158 EOF
159 }
160
161 grml_debootstrap_setup() {
162   echo "* grml-debootstrap setup"
163   if [ "$GRML_DEBOOTSTRAP_VERSION" = "latest" ] ; then
164     echo "** GRML_DEBOOTSTRAP_VERSION is set to '$GRML_DEBOOTSTRAP_VERSION'"
165     echo "** Setting up grml-debootstrap from CI repository from jenkins.grml.org"
166     cat > /etc/apt/sources.list.d/grml-debootstrap.list << EOF
167 deb     http://jenkins.grml.org/debian grml-debootstrap main
168 EOF
169     wget -O - http://jenkins.grml.org/debian/C525F56752D4A654.asc | apt-key add -
170     apt-get update
171     apt-get -y install grml-debootstrap
172   elif [ "$GRML_DEBOOTSTRAP_VERSION" = "stable" ] ; then
173     echo "** GRML_DEBOOTSTRAP_VERSION is set to '$GRML_DEBOOTSTRAP_VERSION'"
174     echo "** Using latest stable grml-debootstrap version"
175     apt-get update
176     apt-get -y install grml-debootstrap
177   elif [ "$GRML_DEBOOTSTRAP_VERSION" = "git" ] ; then
178     echo "** GRML_DEBOOTSTRAP_VERSION is set to '$GRML_DEBOOTSTRAP_VERSION'"
179     echo "** Using grml-debootstrap from Git repository"
180     git clone https://github.com/grml/grml-debootstrap.git
181     cd grml-debootstrap
182     GRML_DEBOOTSTRAP="CONFFILES=$(pwd) $(pwd)/grml-debootstrap"
183   elif [ "$GRML_DEBOOTSTRAP_VERSION" = "local" ] ; then
184     echo "** GRML_DEBOOTSTRAP_VERSION is set to '$GRML_DEBOOTSTRAP_VERSION'"
185     echo "** Using /tmp/grml-debootstrap derived from local system"
186     cd /tmp/grml-debootstrap
187     export CONFFILES=$(pwd)/etc/debootstrap
188     GRML_DEBOOTSTRAP="bash $(pwd)/usr/sbin/grml-debootstrap"
189   elif [ "$GRML_DEBOOTSTRAP_VERSION" = "iso" ] ; then
190     echo "** GRML_DEBOOTSTRAP_VERSION is set to '$GRML_DEBOOTSTRAP_VERSION'"
191     echo "** Using grml-debootstrap as provided on ISO"
192   fi
193 }
194
195 grml_debootstrap_execution() {
196   echo "* Installing Debian"
197
198   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
199   $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
200 }
201
202 apply_nic_workaround() {
203   # release specific stuff
204   case "$DEBIAN_VERSION" in
205     stretch|buster|bullseye|bookworm|unstable|sid)
206       ;;
207     *)
208       echo "* Debian $DEBIAN_VERSION doesn't require NIC workaround"
209       return 0
210       ;;
211   esac
212
213   if ! mountpoint "${TARGET}" &>/dev/null ; then
214     echo "* Mounting target system"
215     mount "${INSTALL_TARGET}" "${TARGET}"
216   fi
217
218   echo "* Disabling predictable network interface names for Debian $DEBIAN_VERSION"
219   ln -s /dev/null "${TARGET}/etc/udev/rules.d/80-net-setup-link.rules"
220
221   echo "* Rebuilding initramfs to include udev configuration change"
222   chroot "${TARGET}" update-initramfs -u -k all
223 }
224
225 log_system_information() {
226   if ! mountpoint "${TARGET}" &>/dev/null ; then
227     echo "* Mounting target system"
228     mount "${INSTALL_TARGET}" "${TARGET}"
229   fi
230
231   local debian_version="$(cat ${TARGET}/etc/debian_version)"
232   echo "* Installed Debian version $debian_version"
233
234   echo "* Logging build information to /etc/grml_debootstrap.info"
235   echo "Debian $debian_version installed by grml-debootstrap/provision on $(date)" > ${TARGET}/etc/grml_debootstrap.info
236   $GRML_DEBOOTSTRAP --version | head -1 >> ${TARGET}/etc/grml_debootstrap.info || true
237
238   if mountpoint "${TARGET}" &>/dev/null ; then
239     echo "* Unmounting target system"
240     umount "${TARGET}"
241   fi
242 }
243
244 clean_apt_files() {
245   if ! mountpoint "${TARGET}" &>/dev/null ; then
246     echo "* Mounting target system"
247     mount "${INSTALL_TARGET}" "${TARGET}"
248   fi
249
250   echo "* Cleaning up apt stuff"
251   chroot ${TARGET} apt-get clean
252   rm -f ${TARGET}/var/lib/apt/lists/*Packages \
253     ${TARGET}/var/lib/apt/lists/*Release \
254     ${TARGET}/var/lib/apt/lists/*Sources \
255     ${TARGET}/var/lib/apt/lists/*Index* \
256     ${TARGET}/var/lib/apt/lists/*Translation* \
257     ${TARGET}/var/lib/apt/lists/*.gpg \
258     ${TARGET}/var/cache/apt-show-versions/* \
259     ${TARGET}/var/cache/debconf/*.dat-old \
260     ${TARGET}/var/cache/apt/*.bin \
261     ${TARGET}/var/lib/aptitude/pkgstates.old
262
263   if mountpoint "${TARGET}" &>/dev/null ; then
264     echo "* Unmounting target system"
265     umount "${TARGET}"
266   fi
267 }
268
269 automated_tests() {
270   echo "* Checking for bats"
271   if dpkg --list bats >/dev/null 2>&1 ; then
272     echo "* bats is already present, nothing to do."
273   else
274     echo "* Installing bats"
275     apt-get update
276     apt-get -y install bats
277   fi
278
279   echo "* Running tests to verify grml-debootstrap system"
280   bats /tmp/debian64.bats -t
281 }
282
283 ## main execution
284 sources_list_setup
285 partition_setup
286 grml_debootstrap_setup
287 grml_debootstrap_execution
288 virtualbox_setup
289 vagrant_setup
290 apply_nic_workaround
291 log_system_information
292 clean_apt_files
293 automated_tests
294
295 echo "* Finished execution of $0"