The new setup could look like this:
Install the grub-live package (the script below can be later added to the package)
Create a script: /etc/initramfs-tools/scripts/init-premount/livetest
with the following content:
#!/bin/sh
set -e
case “${1}” in
prereqs)
exit 0
;;
esac
echo "Testing for live boot. "
mkdir /livetest
mount -t ext4 -n -o rw $ROOT /livetest
if [ -n “$(mount | grep “(ro,”)” ]; then
echo "Mounting root read-write failed. Assuming live-mode. "
umount /livetest
if [ -z “$(dmesg | grep “BIOS VirtualBox”)” ]; then
echo ‘live_disk=$(blkid /dev/vda1 -o value -s UUID)’ >> /scripts/local
else
echo ‘live_disk=$(blkid /dev/sda1 -o value -s UUID)’ >> /scripts/local
fi
echo “BOOT=live” >> /scripts/local
echo ‘LIVE_BOOT_CMDLINE=“root=/dev/disk/by-uuid/$live_disk boot=live ip=frommedia plainroot union=overlay”’ >> /scripts/local
else
echo "Filesystem can be mounted read-write. Proceeding normal boot. "
umount /livetest
fi
exit 0
chmod +x the script.
run:
sudo update-initramfs -uk all
Add “alias /var/lib -> /rw/var/lib,” to /etc/apparmor.d/tunables/home.d/grub-live
Otherwise apparmor will complain and tor will not start.
Poweroff the machine. For KVM just toggle the virtual hard disk to read-only. For VirtualBox run:
VBoxManage setextradata VMName “VBoxInternal/Devices/lsilogicsas/0/LUN#0/AttachedDriver/Config/ReadOnly” 1
I guess the path should be the same for everyone. But to be sure you can check the VBox.log.
If you now boot the VM you don’t need to select the live mode. Just let it boot normally. During boot the script checks if the disk can be mounted read-write. If this is succesful it just boots into persistent mode as always. However, if the disk was set to read-only on the host the check will fail. It then sets some variables required for live boot, the right disk device depending on if it runs on KVM or VirtualBox and proceeds to boot as a live system.
The script would also making changes to the grub menu (how it is currently done) obsolete.
To enable read-write again for VirtualBox do:
VBoxManage setextradata VMName “VBoxInternal/Devices/lsilogicsas/0/LUN#0/AttachedDriver/Config/ReadOnly”