How to modify linux kernel for mounting a vhd file at boot time and load the system from it?

StackOverflow https://stackoverflow.com/questions/12111233

  •  28-06-2021
  •  | 
  •  

Question

I want to modify linux kernel so that it can mount vhd files and starts from it...please give me some idea!!! As we know that linux kernel mount hardisk partitions after the initrd.gz is loaded...i want that instead of mounting the linux partiton it should mount a vhd file located some where in the harddisk which containes ubuntu installed and start the system from it. Windows 7 support this feature of booting from vhd but ubuntu does not..so that y i was asking that how we can modify the kernel for this purpose. Thank You

Was it helpful?

Solution

This will not be easy at all.

To start loading Linux a bootloader is used. This program locates the vmlinux or vmlinuz file and the initrd file and loads them into RAM.

I assume your VHD file is stored on a NTFS filesystem. Inside the VHD file are the vmlinuz and initrd files.

So first, the bootloader will need to understand NTFS. Next, the bootloader will need to understand VHD.

Only after that can Linux even begin to load.

At that point, things become easier because Linux has NTFS filesystem drivers that you can use. Probably the NTFS-3G user-space driver combined with FUSE. I am not sure of the VHD formats but you could use vdfuse from VirtualBox to mount it.

The NTFS and VHD mount programs would have to go inside the initrd. Then the initrd can mount the root partition from the VHD and proceed into normal Linux user-space.

OTHER TIPS

If you are trying to boot Ubuntu Linux, here is what I did:

  1. Download vboot-v1 install in windows, or google for grub2 solution
  2. Install Virtualbox
  3. Install Ubuntu with Virtualbox, choose vhd type of disk
  4. Install virtualbox-fuse in Ubuntu
  5. In ubuntu, add this files to filesystem:

/etc/initramfs-tools/hooks/vdfuse

 #!/bin/sh
 set -e
 PREREQ=""
 prereqs()
 {
    echo "${PREREQ}"
 }
 case "${1}" in
    prereqs)
            prereqs
            exit 0
            ;;
 esac
 . /usr/share/initramfs-tools/hook-functions
 if [ -x /usr/ ]
 then
    copy_exec /usr/bin/vdfuse /bin
 fi

/etc/initramfs-tools/sc~ts/init-bottom/vhdhost

 #!/bin/sh
 PREREQ=""
 # Output pre-requisites
 prereqs()
 {
    echo "$PREREQ"
 }
 case "$1" in
      prereqs)
           prereqs
           exit 0
           ;;
 esac
 mount -n -o move /vhdhost ${rootmnt}/vhdhost
 exit 0

/etc/initramfs-tools/scripts/init-top/vdfuse

 #!/bin/sh
 PREREQS=""
 prereqs()
 {
    echo $PREREQS
 }

 case $1 in
 # get pre-requisites
 prereqs)
    prereqs
    exit 0
    ;;
 esac

 mkdir /vhdhost
 mount /dev/sda2 /vhdhost/ -t ntfs-3g
 mkdir /dev/vhdhost
 vdfuse -f /vhdhost/$vloop /dev/vhdhost

 exit 0

run update-initramfs -k all -c

Now, you just have to modify bootloader to load your vhd file, for example, add this to grub.cfg:

 menuentry "VHD Ubuntu 12.10 64-bit" {
    insmod vhd
    vhd vhd0 (hd0,2)/vhd/Ubuntu.vhd --partitions
    linux (vhd0,1)/boot/vmlinuz-3.5.0-23-generic root=/dev/vhdhost/Partition1 vloop=/vhd/Ubuntu.vhd quiet splash
    initrd (vhd0,1)/boot/initrd.img-3.5.0-23-generic
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top