#!/bin/bash
##
## Summary: is there a way to tell grub-pc on installation that it should
## not grub-probe and grub-install if I do not care about functional grub?
##
## The goal is to install a kernel by package name (linux-generic)
## into the "lxd tarball" (-root.tar.xz file) at
## http://cloud-images.ubuntu.com/daily/server/xenial/current/
##
## The reason is to get the vmlinuz and a initramfs file.
##
## On amd64,
## linux-generic -> linux-image-generic -> linux-image-4.3.0-5-generic
## the 'linux-image-4.3.0-5-generic' package:
## Recommends: grub-pc | grub-efi-amd64 | grub-efi-ia32 | grub | lilo (>= 19.1)
##
## That installation of grub-pc causes me to fail like:
## Setting up grub-pc (2.02~beta2-32ubuntu1) ...
## Creating config file /etc/default/grub with new version
## grub-probe: error: cannot find a device for / (is /dev mounted?).
## grub-probe: error: cannot find a device for /boot (is /dev mounted?).
## grub-probe: error: cannot find a device for /boot/grub (is /dev mounted?).
## /usr/sbin/grub-probe: error: cannot find a device for / (is /dev mounted?).
## dpkg: error processing package grub-pc (--configure):
## subprocess installed post-installation script returned error exit status 1
## Processing triggers for libc-bin (2.21-0ubuntu5) ...
## /bin/df: no file systems processed
## Processing triggers for ureadahead (0.100.0-19) ...
## Processing triggers for systemd (228-3ubuntu1) ...
## Processing triggers for dbus (1.10.6-1ubuntu1) ...
## Errors were encountered while processing:
## grub-pc
## E: Sub-process /usr/bin/dpkg returned an error code (1)
##
## I do not care specifically about grub-pc. I'd like to keep 'recommends' to
## to have whatever other goodness that would bring (assuming that therea are
## valid reasons for the other packages that get installed:
##
## crda grub-common grub-gfxpayload-lists grub-pc grub-pc-bin grub2-common
## iw libnl-3-200 libnl-genl-3-200 linux-firmware linux-headers-4.3.0-5
## linux-headers-4.3.0-5-generic linux-headers-generic linux-image-4.3.0-5-generic
## linux-image-extra-4.3.0-5-generic linux-image-generic thermald wireless-regdb
##
XDIR="$PWD/x"
umount_r() {
local p
for p in "$@"; do
[ -n "$p" ] || continue
tac /proc/mounts | sh -c '
p=$1
while read s mp t opt a b ; do
[ "${mp}" = "${p}" -o "${mp#${p}/}" != "${mp}" ] ||
continue
umount "$mp" || exit 1
done
exit 0' umount_r "${p%/}"
[ $? -eq 0 ] || return
done
}
cleanup() {
umount_r "$XDIR"
}
trap cleanup EXIT
fail() { echo "$@" 1>&2; exit 1; }
msg() { echo "$@" 1>&2; }
vrun() { msg "$@"; "$@"; }
set -e
url=http://cloud-images.ubuntu.com/daily/server/xenial/current/xenial-server-cloudimg-amd64-root.tar.xz
img=${url##*/}
if [ ! -f "${img}" ]; then
wget "$url" -O "$img.tmp" && mv "$img.tmp" "$img" || fail "failed download $url"
fi
[ "$(id -u)" = "0" ] || fail "must be root"
if [ -e "$XDIR" ]; then
msg "removing existing $XDIR"
rm -Rf "$XDIR"
fi
mkdir "$XDIR"
msg "extracting $img to $XDIR"
tar -C "$XDIR" -xpf "$img"
mv "$XDIR/etc/resolv.conf" "$XDIR/etc/resolv.conf.dist"
cp "/etc/resolv.conf" "$XDIR/etc/resolv.conf"
mount -t proc /proc $XDIR/proc
vrun chroot "$XDIR" env DEBIAN_FRONTEND=noninteractive sh -ec \
'apt-get update -q; apt-get install -qy linux-generic </dev/null'