3

Since Microsoft Windows 7 came out, the Windows platform has supported a very awesome feature called Native VHD Boot. This allows you to install Windows into a VHDX file, rather than installing it directly onto a physical volume.

Using this configuration, the Windows installation runs entirely on "bare metal" (not inside a hypervisor).

enter image description here

I would like to do something similar with Linux, although I have no experience with doing so. How can I accomplish this?

  • You need to explain it better. VHD is a disk format used by vmware (and supported by VirtualBox). What are you using to boot your VMs? vmware? Are you trying to boot a Linux machine by generating a VHD file? That's definitely possible but I do not think that is what you are searching for. – grochmal Sep 14 '16 at 19:49
  • 2
    It's explained succinctly. The boot loader boots to a Windows operating system installation that's contained within a VHDX file. By the way, VHD is a Microsoft imaging format, along with WIM. We're not talking about VMware or VirtualBox in any capacity here. Read up on Native VHD Boot in Windows 7 and later. – Trevor Sullivan Sep 14 '16 at 21:36

4 Answers4

2

One way would be to use MEMDISK. You can easily create raw images from an existing system using dd or something similar. You can also easily create virtual machines with libvirt/virt-manager or virtualbox to name a few. For example, with virt-manager, you can create a virtual disk and install an operating system to it with an ISO file (among other methods). I am not 100% sure what formats memdisk supports, so I presume that you would be best off using the raw image format with a fixed size allocated up front.

If your goal is simply to have multiple bootable Linux systems on one physical partition, then LVM is a more straightforward choice. This involves creating a physical partition, a logical volume group(s), multiple logical volumes (and file system on each), and installing or copying a system to each (in the latter case, you'll need to edit grub). It sounds like a lot of steps, but most Linux distribution installers allow you to easily set this up.

Paul Nordin
  • 1,212
  • This is very interesting, thank you. That gives me something to work off of, at least. Just keep in mind that I don't want to create VMs. In fact, I don't want a hypervisor at all -- I just want the operating system's filesystem to be stored inside an image format (eg. raw on Linux, as you pointed out). – Trevor Sullivan Sep 14 '16 at 21:37
  • @TrevorSullivan Our view of the term VM/Virtual Machine may differ. Although probably technically incorrect, I view a VM as any virtual disk (whether raw or formatted) with an OS on it (that can be booted somehow). You can take that virtual disk and do whatever with it, you are not limited to a hypervisor. – Paul Nordin Sep 14 '16 at 22:08
  • 1
    Yeah, what you're describing would be just a virtualized block storage device. That's all a generic Virtual Hard Disk (VHD) is, regardless of format Microsoft VHD[X], VMDK, VDI, et al. But yeah, what I want to do is decouple the idea of a VM running on a hypervisor with the underlying block storage device containing a filesystem(s) that contains the operating system. – Trevor Sullivan Sep 14 '16 at 23:04
2

I've been playing this idea for the past few days and I've finally got it to work.

First you need to compile a binary that will mount VHD files. But it can also mount VMDK, VDI and raw disk images. You can get the source to compile this program from here. https://github.com/SophosLabs-zz/vdfuse

But before you compile vdfuse you need to run this command.

sudo apt install libfuse-dev virtualbox pkg-config

libfuse-dev has the required header files for vdfuse to compile.
virtualbox has pre-compiled drivers needed for mounting disk images.
pkg-config is used by a script when compiling vdfuse.
Now with all the required files and binaries, let compile vdfuse.

So the commands you need to run to compile vdfuse are as follows:

./autogen.sh
./configure
make
sudo make install

Next we need to add some scripts to run before init from within the intird.img.

Create these files as follows:

/usr/share/initramfs-tools/hooks/vdfuse

#!/bin/sh -e
PREREQ=""
prereqs()
{
    echo "$PREREQ"
}

case "$1" in
    prereqs)
    prereqs
    exit 0
    ;;
esac

. /usr/share/initramfs-tools/hook-functions

copy_exec /usr/local/bin/vdfuse /sbin

/usr/share/initramfs-tools/scripts/init-top/vdfuse

#!/bin/sh -e
PREREQ="udev"
prereqs()
{
    echo "$PREREQ"
}

case "$1" in
    prereqs)
    prereqs
    exit 0
    ;;
esac

if [ ! -z $vdisk ]; then
    mkdir /vdhost
    mkdir /dev/vdhost
    mount -t ntfs $host /vdhost/
    vdfuse -t VHD -f /vdhost$vdisk /dev/vdhost
    mount -t ext4 $ROOT /root
fi

/usr/share/initramfs-tools/scripts/init-bottom/vdhost

#!/bin/sh -e
PREREQ=""
prereqs()
{
    echo "$PREREQ"
}

case "$1" in
    prereqs)
    prereqs
    exit 0
    ;;
esac

if [ -d ${rootmnt}/vdhost ]; then
    mount -n -o move /vdhost ${rootmnt}/vdhost
fi

Next you need to make these files executeable by running the following:

sudo chmod +x /usr/share/initramfs-tools/hooks/vdfuse
sudo chmod +x /usr/share/initramfs-tools/scripts/init-top/vdfuse
sudo chmod +x /usr/share/initramfs-tools/scripts/init-bottom/vdhost

Now we need to build a new kernel using this command: sudo update-initramfs -k all -c

That's everything you need to do inside Linux. The final thing is setting up grub to boot the virtual disk image.

Here is the commands that need to added to the grub.cfg file.

 set vhd=/vhd/Ubuntu.vhd
 set vdhost
 search --no-floppy -s root -f $vhd
 loopback lp0 $vhd tdisk=VHD
 linux   (lp0,1)/vmlinuz root=/dev/vdhost/Partition1 vdisk=$vhd host=/dev/sda1 quiet splash
 initrd  (lp0,1)/initrd.img

Change /dev/sda1 to whichever device the VHD resides on.

Dean
  • 21
0

I would also like to boot Linux from a VHD... and store the VHD in a HD/USB/SD/etc...

a Debian based would be my 1st choice...

There are are interesting experiments with GRUB4DOS in
http://reboot.pro/topic/20603-linux-from-vhd-how-to/
using a pre-built image named "UBT-small.vhd"...

I think it can be adapted for other boot loaders with minor tweaks...

ZEE
  • 257
0

I think WUBI of Ubuntu is in principle a Linux version of native vdisk boot. This feature has long been discontinued by official Ubuntu though but there is a continued third party version wubi uefi. Wubi was designed to let ordinary people run Ubuntu so it has different intention than native boot.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Dec 21 '21 at 20:00