How to use a real partition with Windows 7 installed, in a virtualbox vm?

The command you want is

VBoxManage internalcommands createrawvmdk -filename Win7.vmdk -rawdisk /dev/sda -partitions 1

This will create a special VMDK virtual disk file (Win7.vmdk) which is actually a pointer to the host disk partition /dev/sda1.

In theory, you can then use this as the disk file for a VM to run directly from the actual disk partition, but...

  • (a) I've never tried this, so don't know how reliable it is
  • (b) you may get problems with Windows Activation depending on your license key and whether Windows decides that the detected 'hardware' has significantly changed

Yes you can do that with the internal createrawvmdk command, which will not create an entire disk image, but a pointer to the actual hardware.

There are two ways to do that

  • A. Full disk image (of /dev/sdb)

    sudo VBoxManage internalcommands createrawvmdk -filename sdb.vmdk -rawdisk /dev/sdb
    
  • B. Partition image

    As @StarNamer showed, you ca use only one or few partitions.

    To create image of just one (/dev/sda1) partition:

    sudo VBoxManage internalcommands createrawvmdk -filename sda1.vmdk -rawdisk /dev/sda -partitions 1
    

    To create custom partition table which will map /dev/sda2 and /dev/sda1 in that order:

    sudo VBoxManage internalcommands createrawvmdk -filename sda2_1.vmdk -rawdisk /dev/sda -partitions 2,1
    

Most striking difference will be that full disk image will use bootloader and partition table exactly as they are in your disk, so in theory (I did that previously only in qemu) you will be able to setup OS from your virtual machine. And from my limited experience I can say that full disk image will work exactly as qemu -hda /dev/sdb.

Note: If you're using user to access the disk, you need to add it to disk and vboxusers groups, e.g.

sudo usermod -aG disk,vboxusers ubuntu

then make sure you re-login or restart your computer.

Further reading:

  • VirtualBox documentation: Using a raw host hard disk from a guest
  • VirtualBox boots only in UEFI Interactive shell (just including the ESP for UEFI installs will not work)
  • Install the Windows MBR bootloader (temporarily) from within Ubuntu (1, 2).
  • Better, you can install MBR into a file and use the -mbr option (see 1) when creating the VMDK.
  • If you've got VERR_ACCESS_DENIED error, check: Virtualbox doesn't work with a real partition.