I would like to share scripts wrote for making vhd installed debian system (in virtualbox)
into ram boot capable images.
Idea is to modify initrd and pack your root filesystem that is squash fsed inside initrd.
that way it can be booted using grub4dos under BIOS and grub2 under UEFI.
Also the same VHD file can be grub mapped and we can load vmlinuz and initrd from withn vhd.
So basically you have VHD file wich you map under grub2/grub4dos and then you map vmlinuz and initrd
(with packed root inside) so when initrd loads it finds squashfs root and mounts it and works from ram.
Only problem so far is that initrd can become big... (mine is 600mb) so some bioses wont load it directly.
Workaround is initrd that would map partition holding vhd, then map vhd, and then copy sqsh root in memory and
then continue. Im working on that but thats a bit of a problem because mounting vhd inside initrd requires heavy initrd modification.
Tutorial is on debian 9 stable but should be useable on all debian/linuxes (with some modification)
1.Install squashfs-tools!
>apt install squashfs-tools
2. Add modules to /etc/initramfs-tools/modules
>echo 'loop' > /etc/initramfs-tools/modules
>echo 'squashfs' > /etc/initramfs-tools/modules
3.Add ramboot script to /etc/initramfs-tools/scripts/local-bottom/ramboot
-------------------------------------------------------------------------------------------------------------------------------------------
#!/bin/sh
PREREQ=""
prereqs()
{
echo "$PREREQ"
}
case $1 in
prereqs)
prereqs
exit 0
;;
esac
. /scripts/functions
#mounting squashfs ro root filesystem image
mkdir /root.ro
mount -t squashfs -o loop /debian9_ramroot.sh /root.ro
#mounting rw tmpfs for making squasfs "rw"
mkdir /root.rw
mount -t tmpfs -o size=75% tmpfs /root.rw
#mounting rw over ro for overlayed fs root
mkdir /root.rw/upper
mkdir /root.rw/work
mount -t overlay -o lowerdir=/root.ro,upperrdir=/root.rw/upper,workdir=/root.rw/work none ${rootmnt}
exit 0
-------------------------------------------------------------------------------------------------------------------------------------------
4.rebuild initramfs
mkinitramfs -o initrd.img-SQSH (or whatever name you want)
5.Putting custom made mkramroot.sh to /usr/local/sbin (script that creates sqsh from current root)
-------------------------------------------------------------------------------------------------------------------------------------------
#!/bin/bash
echo "----------------------------------------------------------------------"
echo "!!! RAMROOT GENERATION SCRIPT !!!"
echo "----------------------------------------------------------------------"
echo "example: mkramroot.sh source_initrd dest_initrd workdir"
echo "example: mkramroot.sh /boot/initrd.img-4.9.0-4-amd64 /boot/initrd.img-4.9.0-4-amd64-MOD /mnt/sdb1/"
echo "----------------------------------------------------------------------"
#variables
SOURCE.INITRD=$1
DEST.INITRD=$2
WORK.DIR=$3
echo "----------------------------------------------------------------------"
if [[ $1 -eq 0 ]]
then echo "!!! SOURCE_INTRD missing !!!"
exit 1
fi
if [[ $2 -eq 0 ]]
then echo "!!! DEST_INTRD missing !!!"
exit 1
fi
if [[ $3 -eq 0 ]]
then echo "!!! WORK_DIR missing !!!"
exit 1
fi
echo "----------------------------------------------------------------------"
#echo "SOURCE.INITRD=$1 - DEST.INITRD=$2"
#read -p "Press ENTER to continue..."
#cleaning apt-caches
apt-get clean
rm -r $3/initrd-src
#read -p "Press ENTER to continue..."
#unpacking initrd archive
mkdir $3/initrd-src
cd $3/initrd-src
gzip -cd $1 | cpio -i
echo "SOURCE_INITRD unpacked !"
#ls -l /tmp/initrd-src
#moving sqsh archived root filesystem
rm $3/initrd-src/debian9_ramroot.sqsh
read -p "Press ENTER to coninue..."
echo "boot/*RAMM0Th*" > $3/initrd-src/exclude.sqsh # ignore this !!
echo "tmp/*" >> $3/initrd-src/exclude.sqsh
echo "dev/*" >> $3/initrd-src/exclude.sqsh
echo "proc/*" >> $3/initrd-src/exclude.sqsh
echo "sys/*" >> $3/initrd-src/exclude.sqsh
echo "mnt/*" >> $3/initrd-src/exclude.sqsh
echo "media/*" >> $3/initrd-src/exclude.sqsh
mksquashfs / $3/initrd-src/debian9_ramroot.sqsh -regex -wildcards -ef $3/initrd-src/exclude.sqsh
#tar cvzf $3/initrd-src/debian9_ramroot.img.gz --exclude='/tmp/*' --exclude='/boot/*-RAMM0Th' --one-file-system /
#packing initrd back up
cd $3/initrd-src
find . | cpio --dereference -o -H newc | gzip > $2
rm -r $3/initrd-src
ls -l $3
#echo "NEW.INITRD created
----------------------------------------------------------------------------------------------------------------------------------------------
6.make your new initrd from current root
mkramroot.sh /boot/initrd.img-SQSH /boot/initrd.img-RAMBOOT /tmp/
Wich will create /boot/initrd.img-RAMBOOT from /boot/initrd.img-SQSH wich has all the modules and custom mount script (its the initrd we created before) all this will happen in /tmp/initrd-src and script will stop and wait for your prompt before packing root fs and creating new initrd in case you want to edit anything manually.
----------------------------------------------------------------------------------------------------------------------------------------------
7.in grub4dos menu.lst
find-setroot /initrd.img-RAMBOOT
kernel /vmlinuz_bla bla (whichever it is)
initrd /initrd.img-RAMBOOT
in case you want to do it from inside VHD, you just have to map VHD and set root to mapped file and then again
the same kernel and initrd lines
----------------------------------------------------------------------------------------------------------------------------------------------
8.If you want dual boot bios/uefi
just make fat32 USB with grub4dos mbr and copy efi folder with boot/grub on usb and uefi will recognize
mapping is similar and you can map vhd with loop in grub2
I apologize for long post and a little disorganized writing...
The method leaves a lot to be upgraded, i primarly would like to make one script that does all of that
makes the changes to modules, adds script for initramfs rebuilds initramfs and then creates root in sqsh etc...
Also priority is definetly a smaller initrd with support to mount original usb/hdd and find initrd-RAMBOOT and unpacks sqsh to ram inside smaller initrd (for computers that have limited memory on boot).
I hope it helps to someone
Idea is to have a VHD linux that can be virtualized and booted natively from ram.
I have a similar windows XP/7/8.1/10 images.