2

I am running a linux system as KVM-host and have created a vm running the physical disk of an older PC (using --import). That disk has several partitions with dual-boot of several OS and a scratch partition and some video data ... What I really want to virtualize is the linux-system on that disk. I think I understand how to make an image of the entire disk, but what I really need is just the MBR, scratch, and the partitions holding / and /boot. Is there a way to only 'extract' some partitions to an image for virtualisation? I am thinking of temporarily copying the partitions of interest to a blank disk, install grub and then virtualize that. But maybe there is more simple way to achieve what I want?

Kind regards,

Wolfgang

2

Assuming that the disk containing the old PC's system is physically connected to the KVM host, you could do something like this:

  1. Use virsh or virt-manager to create a VM with an appropriately sized disk-image file AND an ISO containing a good rescue CD (I use clonezilla or gparted for things like this. Any good "live" system should do. Best not to use an installer ISO for any distribution).

    Do NOT attach the physical hard disk to the VM yet.

  2. Boot the system using the ISO image. Partition and format the virtual disk (which will probably be /dev/vda in the VM) as required. Mount it as /target (and other partitions, if any, under /target)

  3. On the KVM host, attach the physical disk containing the old system to the VM using the virsh attach-disk command. There's not enough details in your question to give you even an approximate command-line but run virsh help attach-disk for a summary of the options. It will end up being something like:

    virsh attach-disk vmname /dev/sdX /dev/vdb --targetbus sata

    where /dev/sdX is the physical disk, /dev/vdb is the device as it will be seen by the VM, and the target bus will be sata or scsi or even virtio.

  4. Back on the VM, mount the OS partition you want to virtualise as /source and any other partitions under that. Mount them all read-only with mount -o ro /dev/vdbN /target/XXXXX for safety against typos/mistakes.

  5. rsync -a /source/ /target (the trailing / on /source/ is important)

  6. umount everything under /source. you shouldn't need it any longer.

  7. for i in proc dev sys dev/pts ; do mount -o bind /$i /target/$i done

    This ensures that all the system pseudo-filesystems that grub-install and other tools (e.g. lsblk or blkid) need are bind-mounted and visible inside the chroot we're about to enter in the next step.

  8. chroot /target

  9. edit /etc/fstab to adjust the mount entries to use the new block device names (or labels, or UUIDs).

    You may want to change other things here like network configuration, or you may want to leave that stuff for later when the basic system is running. I'd advise taking a copy of any major changes you make, just in case things don't go smoothly the first time and you you have to try again. Alternatively, just make a note of what you changed so you know what needs to be done if you have to start over.

  10. grub-install /dev/vda

  11. exit # leave the chroot and return you to the shell on the rescue CD image.

  12. for i in proc dev/pts dev proc sys; do umount /target/$i ; done

  13. umount everything under /target

  14. On the KVM host, detach the extra disk with virsh detach-disk.

You should now be able to reboot the VM. You may need to tell it to boot from the hard-disk instead of the ISO image virtual "CD-ROM".

| improve this answer | |
  • BTW, you can run step 14. immediately after step 6. if you want. it makes no significant difference. – cas Aug 15 '17 at 13:21
  • Thanks for the detail advise! As far as I see the basic concept is to copy the partitions of interest into an empty image using rsync and then making that 'bootable' with chroot and grub. I will give it a try – wolfgang6444 Aug 16 '17 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.