Jump to content











Photo
- - - - -

Debian From RAM -initrd and VHD ram booting

linux ramboot debian ramboot

  • Please log in to reply
1 reply to this topic

#1 sphaaz

sphaaz
  • Members
  • 3 posts
  •  
    Azerbaijan

Posted 24 October 2018 - 07:52 PM

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.



#2 sphaaz

sphaaz
  • Members
  • 3 posts
  •  
    Azerbaijan

Posted 28 October 2018 - 07:21 PM

UPDATE!!! COMPLETE SCRIPT THAT MAKES TWO INITRDS

ONLY THING YOU HAVE TO DO IS ADD ENTRIES TO GRUD4DOS/GRUB2 TO BOOT!!

 

TESTED ON DEBIAN STABLE, THIS SCRIPT MAKES 2 INITRDS

ONE WITH squashfs IMAGE OF ROOTFS INSIDE and ANOTHER THAT

TAKES ARGUMENTS PASSED VIA GRUB4DOS/GRUB2

#!/bin/sh

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-RAMROOT /tmp/"
echo "----------------------------------------------------------------------"

#variables
SOURCE.INITRD=$1
DEST.INITRD=$2
WORK.DIR=$3

echo "----------------------------------------------------------------------"

if [ -z $1 ]
  then echo "!!! SOURCE_INTRD missing !!!"
  exit 1
fi

if [ -z $2 ]
  then echo "!!! DEST_INTRD missing !!!"
  exit 1
fi

if [ -z $3 ]
  then echo "!!! WORK_DIR missing !!!"
  exit 1
fi

echo "----------------------------------------------------------------------"

#installing dependencies - packages needed for ramroot images creation
apt update
apt install squashfs-tools ntfs-3g 

#modifyig mkinitramfs generation configs

#saving old modeules to modules.ORIGINAL
mv /etc/initramfs-tools/modules /etc/initramfs-tools/modules.ORIGINAL

#Adding required modules for reading from usb and for making overlayfs
echo "loop" > /etc/initramfs-tools/modules
echo "squashfs" >> /etc/initramfs-tools/modules
echo "fuse" >> /etc/initramfs-tools/modules
echo "overlay" >> /etc/initramfs-tools/modules
echo "vfat" >> /etc/initramfs-tools/modules
echo "nfat" >> /etc/initramfs-tools/modules
echo "nls_cp437" >> /etc/initramfs-tools/modules
echo "nls_ascii" >> /etc/initramfs-tools/modules
echo "nls_iso8859_1" >> /etc/initramfs-tools/modules

#generating ramroot initrd boot script
echo '#!/bin/sh' > /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'PREREQ="ramroot_initrd"' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'prereqs()' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo '{' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'echo "$PREREQ"' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo '}' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'case $1 in' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'prereqs)' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'prereqs' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'exit 0' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo ';;' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'esac' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo '. /scripts/functions' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'mkdir /root.ro' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'mount -t squashfs -o loop /debian9_ramroot.sqsh /root.ro' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'mkdir /root.rw' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'mount -t tmpfs -o size=75% tmpfs /root.rw' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'mkdir /root.rw/upper' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'mkdir /root.rw/work' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'mount -t overlay -o lowerdir=/root.ro,upperdir=/root.rw/upper,workdir=/root.rw/work none ${rootmnt}' >> /etc/initramfs-tools/scripts/local-bottom/ramroot
echo 'exit 0' >> /etc/initramfs-tools/scripts/local-bottom/ramroot

#making the script executable
chmod +x /etc/initramfs-tools/scripts/local-bottom/ramroot

#generate ramroot_initrd init script
echo '#!/bin/sh' > /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'PREREQ=""' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'prereqs()' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo '{' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'echo "$PREREQ"' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo '}' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'case $1 in' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'prereqs)' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'prereqs' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'exit 0' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo ';;' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'esac' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo '. /scripts/functions' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'mkdir /root_dev' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'mkdir /root_cpio' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'if [ -z $root_devfs ]' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'then' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'exit 0' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'else' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'if [ -z $root_dev ]' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'then' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'exit 0' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'else' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'mount -t $root_devfs $root_dev /root_dev' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'fi' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'fi' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'cd /root_cpio' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'if [ -z $root_cpio ]' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'then' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'exit 0' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'else' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'gzip -cd /root_dev/$root_cpio | cpio -i' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'mv /root_cpio/debian9_ramroot.sqsh /' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'fi' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'umount /root_dev' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'rm -r /root_cpio' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd
echo 'exit 0' >> /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd

#making the script executable
chmod +x /etc/initramfs-tools/scripts/local-bottom/ramroot_initrd

#GENERATING RAMINITRD IMAGE
#this image loads minimal initrd into ram and searches initrd with squashfs
#root on specified device, specify root_dev=/disk/with/initrd_packed_with 
#squashfs_root root_devfs=filesystem_type root_cpio=/initrd_packed_with_sqsh 
#root.

mkinitramfs -o $2-RAMINITRD

#cleaning apt-caches
apt-get clean
#cleaning if there is leftovers in workdir
rm -r $3/initrd-src

#unpacking source initrd archive
mkdir $3/initrd-src
cd  $3/initrd-src
gzip -cd $1 | cpio -i

#removing sqsh archived old root filesystem in case we 
#selected initrd with sqsh root
rm $3/initrd-src/debian9_ramroot.sqsh

#here we pause if any modifications or checks are needed 
#in WORK_DIR
read -p "Modify andything in workdir and Press any key to CONTINUE..."

#generating exclusion list for root sqsh creation
echo "boot/*RAMM0Th*" > $3/initrd-src/exclude.sqsh
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

#creating squashed rootfs
mksquashfs / $3/initrd-src/debian9_ramroot.sqsh -regex -wildcards -ef $3/initrd-src/exclude.sqsh

#generating new initrd from contents of $WORK_DIR
cd $3/initrd-src
find . | cpio --dereference -o -H newc | gzip > $2

#cleaning up $WORKDIR
rm -r $3/initrd-src
ls -l $2





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users