P2V - physical to virtual
Sometimes you would like to take a copy of a physical machine and preserve it as a VM so you can archive it, or even run it on upgraded hardware.
P2V
dependencies
On the virt-v2v host conversion machine:
- install prequisites
apt install lib-virt apt install libguestfs-tools
- make the following link so v2v tools work
ln -s /usr/share/ /share
virtio-win
Windows virtio drivers are para-virtualized drivers to make VM disks more efficient. The virtio-win package (if available, e.g. for redhat or fedora) installs a CD-ROM image, virtio-win.iso, in the
/usr/share/virtio-win/
(Note that directory is denied by appamor so move the .iso somewhere else e.g. /virtual/iso/)
These virtio devices can be used to improve the performance of your Windows quest VMs:
wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.185-2/virtio-win-0.1.185.iso
- https://pve.proxmox.com/wiki/Windows_VirtIO_Drivers
- https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.141-1/
- https://github.com/virtio-win/kvm-guest-drivers-windows
create a bootable image
On the v2v host:
- ensure we have spare disk capacity for the KVM virtual disk (I inserted /dev/sdd).
lsblk
- Insert a USB drive, identify its device
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 931.5G 0 disk ├─sda1 8:1 0 15.4G 0 part [SWAP] └─sda2 8:2 0 916.1G 0 part └─hdd-storage 254:2 0 916.1G 0 lvm /hdd sdb 8:16 0 111.8G 0 disk ├─sdb1 8:17 0 487M 0 part /boot/efi ├─sdb2 8:18 0 954M 0 part ├─sdb3 8:19 0 28.9G 0 part │ └─os-root 254:0 0 28.9G 0 lvm / └─sdb4 8:20 0 81.5G 0 part └─ssd-storage 254:1 0 81.5G 0 lvm /ssd sdc 8:32 1 14.5G 0 disk └─sdc1 8:33 1 14.5G 0 part sdd 8:48 0 3.7T 0 disk └─sdd1 8:49 0 3.7T 0 part /mnt/sdd1
- create bootable p2v image
virt-p2v-make-disk -o /dev/sdc ubuntu-16.04
Note: I could not get Debian 10 to boot, will try Debian 8 later since they changed boot images at 10 and legacy systems got left out. When targetting a device choose one that is not too large because the script resizes the image to use the whole drive, which can be very slow, particularly with large USB drives or even slow thumb drives.
The p2v boot image on the USB drive will be used to boot the Windows physical machine to initiate the transfer of the Windows disk images to a KVM guest via ssh shell communications with the target v2v server (where you must have installed the above packages).
Note: I installed the virtualisation software on padme, since padme already had an sshd server established. I just mounted an external USB HDD (with sufficient capacity) to contain the target guest virtual disk, which can then moved somewhere else and executed in lib-virt e.g. using virt-manager on c3po. That way I do not pollute c3po with an sshd server, nor do I have to make a bridge network to any of c3po's running guest VMs that have an sshd server which I could have used instead.
You may also choose use root sshd login if your v2v server needs access to weirdly mounted file systems. In which case some security modifications are necessary:
vi /etc/ssh/sshd_config
PermitRootLogin=yes
Don't forget to disable root ssh login after you are all done, and use a really complex unknown password. It also helps that padme's sshd port 22 is not exposed to the www.
conversion
On the client machine:
- Insert the bootable USB drive in your physical system and configure boot via boot menu (F12 on Dell equipment, F2 and F11 on others).
- fill out the target server credentials:
server: padme port : 22 user: root password: <something-really-obscure> storage: /mnt/sdx/p2v
- select the drives to convert (deselect the cdrom)
- test the connection
test
- start the conversion and transfer process:
next
Other reading
- https://docs.openstack.org/image-guide/modify-images.html
- https://askubuntu.com/questions/195139/how-to-convert-qcow2-virtual-disk-to-physical-machine-and-reversely
- https://askubuntu.com/questions/195139/how-to-convert-qcow2-virtual-disk-to-physical-machine-and-reversely
- https://words.filippo.io/converting-a-partition-image-to-a-bootable-disk-image/
how to repair grub
monitoring
On the v2v server:
- install some utils
apt install sysstat iftop
- monitor the network
iftop -i <ifname>
- monitor disk
iostat 60
- monitor the logs
tail -f /tmp/p2v-date-guid/*log.txt
- monitor the target files
for (( ; ; )) do ls -lrtah /mnt/sdX/p2v; sleep 60; done
Alternative ideas for Microsoft Windows conversions
HyperV
Note: I could not get this to work on the Windows 7 machine, it complained about being unable to access disk 0.
disk2vhd
- download https://download.sysinternals.com/files/Disk2vhd.zip
- extract
- run
VMWare
Use VMWareConverter to convert a live physical machine to a VMWare VM image.
- download http://www.vmwarearena.com/vmware-converter-how-to-perform-p2v-and-v2v-migration/
- install local VMWareConverter
- run and choose local machine
- ensure you select all partitions on your primary drive (deselect drives you do not want to copy e.g. recovery drive and attached drives)
Note: In Windows Vista and later versions, Microsoft switched to using a separate boot partition. In these VMs, typically /dev/sda1 is the boot partition and /dev/sda2 is the main (C:) drive. Resizing the first (boot) partition causes the bootloader to fail with 0xC0000225 error. Resizing the second partition (ie. C: drive) should work.
For Windows 7 and 8, see also
- See also Windows Easy Transfer available under Windows 7 and 8 only.
virt-v2v
You may use virt-v2v to convert a source VM to a target VM.
requirements
- a temorary area that is large enough
- (default) /var/tmp
- mode must be 700
- else mount a tmp on a different ext4 filesystem and set TMPDIR to point to it ensuring mode is 700
- an attached drive large enough to contain the converted VM images
See:
e.g. converting vmdk to qcow2
Note: qcow2 disks are thin provisioned and only take up the space they require; this means they will expand as more extents are added to the filesystem that overlays their image.
- run
cd /mnt/ext/VM/port-ralph export TMPDIR=/mnt/ext/tmp virt-v2v --verbose -i disk -if vmdk port-ralph.vmdk -o local -oa sparse -of qcow2 -os . -on port-ralph-v2v.qcow2
Note that $TMPDIR has more space available than /var/tmp.
Output is:
[ 0.1] Opening the source -i disk -if vmdk port-ralph.vmdk [ 0.1] Creating an overlay to protect the source from being modified [ 0.2] Initializing the target -o local -os . [ 0.2] Opening the overlay [ 10.2] Inspecting the overlay [ 11.9] Checking for sufficient free disk space in the guest [ 11.9] Estimating space required on target for each disk [ 11.9] Converting Windows 7 Home Premium to run on KVM virt-v2v: warning: Neither rhev-apt.exe nor vmdp.exe can be found. Unable to install one of them. virt-v2v: warning: there are no virtio drivers available for this version of Windows (6.1 x86_64 Client). virt-v2v looks for drivers in /usr/share/virtio-win The guest will be configured to use slower emulated devices. virt-v2v: This guest does not have virtio drivers installed. [ 13.9] Mapping filesystem data to avoid copying unused and blank areas [ 15.6] Closing the overlay [ 15.8] Checking if the guest needs BIOS or UEFI to boot [ 15.8] Assigning disks to buses [ 15.8] Copying disk 1/1 to ./port-ralph-v2v.qcow2-sda (qcow2)
- conversion with qemu-img (not sure that workds)
- man page https://linux.die.net/man/1/qemu-img
qemu-img convert -f vmdk port-ralph.vmdk -O qcow2 port-ralph.qcow2
- then in-situe adjustments
virt-v2v -i local -if qcow2 port-ralph.qcow2 --in-place
Disk cloning
Don't forget the simplest: disk cloning.
You can clone the machine's disks and attachment them as a VM disks and execute the image with qemu-systemXXX - see cloning
A completely cloned disk can be executed via qemu and associated with a virt-manager image.
- https://virtuallyfun.com/wordpress/2018/01/15/converting-a-physical-disk-to-a-virtual-disk-with-qemus-qemu-img-on-windows/
- copy from physical device to a qcow2 or raw file (qemu-img is orders of magnitude slower than just performing a dd (which is not sparce of course))
qemu-img convert -p -O raw /dev/sda /srv/vm/mywindowsinstall/disk.raw qemu-img resize /srv/vm/mywindowsinstall/disk.raw +50G
- running the clone (for a 64 bit Intel, 8G RAM, 8 vcpus):
qemu-system-x86_64 -drive file=disk.raw,format=raw,index=0,media=disk -soundhw es1370 -vga cirrus -m 8G -smp 8
- this is what I used for a Windows 7 image
qemu-system-x86_64 -drive file=w7.raw,format=raw,index=0,media=disk -soundhw es1370 -vga cirrus -m 16G -smp cpus=8,cores=8
downsizing a VM qcow2 or vmdk file
This article https://www.qemu.org/docs/master/system/images.html explains the qemu supported images; e.g. it is not necessary to connvert vmdlk to qcow2 as both formats are supported. Note that raw format on an ext4 file system is much more performance, and ext4 supports holes, so the resulting raw format will be sparse. qcow2 provides compression and is sparse on any file-system, including windows - but is less efficient.
The following describes how to shrink sparse disk images and their contained file systems.
user suggestions, the manual, less downtime way
The VM is online while zero-fill unallocated extents of the disk image, then taken offline to shrink the dfisk image:
- https://stackoverflow.com/questions/29124150/how-to-resize-the-qcow2-image-without-impact-the-application
- https://www.jamescoyle.net/how-to/323-reclaim-disk-space-from-a-sparse-image-file-qcow2-vmdk
- sdelete for Windows https://docs.microsoft.com/en-us/sysinternals/downloads/sdelete
the more automatic, albiet longer downtime way
The VM must be taken offline to perform these steps, the first of which is to copy the original disk image and then zero fill the unallocated spaces:
- virt-sparsify https://libguestfs.org/virt-sparsify.1.html
- virt-resize https://libguestfs.org/virt-resize.1.html
- guestfish https://libguestfs.org/guestfish.1.html
resizing boot partitions
In Windows Vista and later versions, Microsoft switched to using a separate boot partition. In these VMs, typically /dev/sda1 is the boot partition and /dev/sda2 is the main (C:) drive. Resizing the first (boot) partition causes the bootloader to fail with 0xC0000225 error. Resizing the second partition (ie. C: drive) should work.
References
Some casual reading, but most of it has been worked out and documented in my cookbook recipes above:
- instructional https://meldco.dk/?Page=/Doc/History/doc/linux/virt-p2v-move-windows-to-linux
- libguests https://libguestfs.org/
- https://libguestfs.org/virt-p2v.1.html
- virtp2v
- (discussion) to KVM https://access.redhat.com/discussions/3488781
- (redhat article) https://access.redhat.com/articles/2702281
- (redhat guide) https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html-single/v2v_guide/index
- http://manpages.ubuntu.com/manpages/bionic/man1/virt-p2v.1.html
- parallels https://kb.parallels.com/119172
- some supported guests in addition to Linux
- Windows conversion to KVM
- https://meldco.dk/?Page=/Doc/History/doc/linux/virt-p2v-move-windows-to-linux
- https://www.howtogeek.com/213145/how-to%C2%A0convert-a-physical-windows-or-linux-pc-to-a-virtual-machine/
- obtaining the virtio-win drivers https://linuxhint.com/install_virtio_drivers_kvm_qemu_windows_vm/
- https://getlabsdone.com/10-easy-steps-to-install-windows-10-on-linux-kvm/
- hyper-v
- vmware
- Windows conversion to KVM
- virtio http://www.linux-kvm.org/page/Virtio
- fedora instructions https://docs.fedoraproject.org/en-US/quick-docs/creating-windows-virtual-machines-using-virtio-drivers/index.html
- debian https://serverfault.com/questions/665319/installing-a-windows-guest-with-virtio-in-a-single-step-debian
- on windows guest http://www.zeta.systems/blog/2018/07/03/Installing-Virtio-Drivers-In-Windows-On-KVM/
- https://docs.openshift.com/container-platform/4.2/cnv/cnv_users_guide/cnv-installing-virtio-drivers-on-existing-windows-vm.html
- livecd - another topic to explore
- https://willhaley.com/blog/custom-debian-live-environment/
- https://superuser.com/questions/1003292/making-live-distribution-from-a-running-linux-os-debian
- https://gparted.org/livecd.php
- https://www.pendrivelinux.com/create-your-own-live-linux-cd-or-usb-distribution/
- https://grml.org/
- debian http://nerotux.tuxfamily.org/index.php/Articles/DebianLiveHowto
