Add physical partition to QEMU/KVM virtual machine in virt-manager

27,347

Solution 1

The solution appears to be passing the whole block device, like /dev/sda or the equivalent from /dev/disk/by-id/, to the <source dev=''> setting.

This allows the Windows setup to see the existing partitions, including the root partition in the host.

There should not be any conflict, as long as:

  • you don't try to mount/modify a partition in the guest that is already mounted in the host; and

  • you don't mount/modify the partition currently in used by the guest from the host, while the VM is powered on.

Regarding the first point, it is of course necessary to unmount the ESP (EFI System Partition) from the host prior to launching the VM. I first wrote a script for that, and then came up with a better solution in the form of a QEMU automation hook. I'm making available at https://gitlab.com/ranolfi/rvirtesp.

What's cool about this is that you can also boot the virtualized OS natively, as in dual-booting, by selecting its new entry in the motherboard's boot menu.

I'm not exactly sure this is "supported". But been using it for almost a year now, without a problem.

Solution 2

If you want to pass a partition as a raw device you can just put /dev/sdaX as a raw image filename it will use it. However if you use ieg sda1 it wont understand which partition it is and consider this partition as a whole disk. If you cant give /dev/sda as source for obvious reasons (using other /dev/sdaY on host machine) you may try iscsi.. But it may also make partition seen as whole disk. Meaning you cant see your files and contents.

To tackle this best option seem to be sharing via samba. Another option is to map the partition on a virtual disk. Another option is share mounted partition as folder, qemu has this option ... Fedora virtio drivers may have this like vmwares vmtools.

I am using ISCSI beyond 4GB/s speeds on virtio network. But i have changed mtu of virbr and virbr-nic0 to 9000 and increased txqueulen. No need to change guest mtu ...

Anyways if you wanna give a try to iscsi here it is :

you first install open-iscsi and tgt

sudo apt-get install open-iscsi tgt

then you configure it

nano /etc/tgt/conf.d/iscsi.conf

put your partition name instead of sdb1 (target name will be different):

<target iqn.2019-11.example.com:lun1>
     # Provided device as an iSCSI target
     backing-store /dev/sdb1                             
     initiator-address 192.168.0.102 
    incominguser iscsi-user password
     outgoinguser iscsi-target secretpass
</target>

then restart the service

sudo systemctl restart tgt 

then check it

tgtadm --mode target --op show

open windows and use iscsi initiator to connect it. Windows will see it as an internal disk ,have fun with high speed block access.

if you add a NAT nic and set it virtio installing virtio redhat drivers it probably will be lightning fast. Do not forget to connect via 192.168.123.XXX Instead of your bridge LAN ip.

Notes: Be careful and first test with a test partition .... iqn must be a-z or 1-9 you can. Leave passwords and secretpass blank There is a standard for these they may not accept space or underscore ...

Share:
27,347

Related videos on Youtube

Marc.2377
Author by

Marc.2377

Marc ".2377" Ranolfi - Cross-platform developer and open source hacker; aspiring engineer. Contributor to codidact.org, currently on a hiatus. (Also on Discord.)

Updated on September 18, 2022

Comments

  • Marc.2377
    Marc.2377 over 1 year

    I followed the instructions in this answer to a related question: Add physical disk to KVM virtual machine

    The resulting code in the .xml file for the VM is the following:

    <disk type='block' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <source dev='/dev/disk/by-partuuid/d8b63353-34n6-6587-ac07-810dmf36d544'/>
      <target dev='vdb' bus='virtio'/>
      <boot order='2'/>
      <address type='pci' domain='0x0000' bus='0x05' slot='0x00' function='0x0'/>
    </disk>
    

    Notice how I used /dev/disk/by-partuuid/d8b63353-34n6-6587-ac07-810dmf36d544 for the <source> element, instead of /dev/sda4.

    What is bothering me is that my use case is to pass a partition to QEMU, instead of a disk. I installed Windows 10 on it, the Setup installer wouldn't recognize the storage so I had to load the viostor driver from the FedoraProject.

    The Windows installation went fine, and the performance is amazing, but when the VM is turned off, the host system doesn't recognize the partition format.

    I found it interesting that the Windows installer - with the viostor driver loaded - created the additional partitions normally required by Windows on a bare disk. In other words, it created partitions inside what's supposedly a partition. This shouldn't be possible, or so I thought.

    /dev/sda4 is present in the host, but is not recognized as neither an NTFS filesystem (not expected, indeed), nor LVM, nor LDM (tested with ldmtool).

    The VM is booting correctly in UEFI mode with the Q35 chipset.

    Question: Is my procedure to add a partition to virt-manager correct, or should it only be done for disks - and not for partitions? Also, what partition type or format did I end up with?

    • Panther
      Panther almost 7 years
      Personally I use virtual disks rather then raw partitions for exactly the behavior you noticed. If you use a physical partition I advise you use LVM
  • John Moser
    John Moser almost 4 years
    It would be nice if KVM supported a configuration to make a particular partition read-write but make the rest of the physical disk read-only