How to Clone a VM? How Do I Install More VM Tools?


Recommended Posts

Does anyone know of a tool installed on unRaid that will clone a VM?  I am new to KVM, but I am used to cloning VMs on esxi via the command line.  I have read a few suggestions of just copying the .img file, but that will essentially convert the thin provisioned disk to a thick one.

 

Through some googling i have found some packages that may be what i am looking for:  virtinst, which contains a virt-clone command; or libguestfs-tools which contains a virt-sparsify command which shrinks a thick provisioned disk to a thin one.  I found this slack package, but I don't know if its compatible with unRaid or how to install it.

 

Ideally what i really want to do is create a template and deploy from that, but I will settle for being able to clone a VM.

Link to comment

I asked about this a while back and never got a response so I did some testing of my own.

 

I created a VM -- installed Windows 7 + drivers etc. Then I just copied the vdisk and created a new VM except that I selected the copy of the vdisk that I made. Boot up the VM and it worked.

 

So as far as I can tell this is all that is required. I'm pretty new to VMs so I could be wrong but it worked fine for me.

 

Link to comment

Hi i was just wondering the same so i googled it :D

 

This method uses the power of BTRFS filesystem: we create one basevm and make snapshots for our Clone VM's. This wil not use any additional space on your harddrive. Except for changes made in the clone.

 

So if you create one Windows 10 machine (around 17 GB) you can make for example 15 clones of it and still only use 17 GB of disk space.

 

BEFORE YOU START: Im not sure if this is the best way to to this but it works !

 

Download putty and ssh to your Unraid server.

 

Make sure u are using btrfs filesystem (https://lime-technology.com/forum/index.php?topic=33806.0) and cd to your btrfs location!

 

For me this is:

# cd /mnt/disk1/

 

Do the following with commandline (ssh to your unraid server):

 

Step 1 --make te initial (sub)volume (this wil create an folder: /mnt/disk1/btrfs)

# btrfs subvolume create btrfs

 

Now you can make your folder tree within this folder with subvolumes (Volumes for basevm folder and its clones folders)

 

Step2 --So next we will create a base folder for our vm disk image

# btrfs subvolume create btrfs/basevm

 

Step 3 --Now copy your vm disk image to this folder you just created. I created an Windows 10 vm called win10, and copied the vdisk1.img to my basevm subvolume (vdisk1.img is the Virtual harddisk of VM.)

# cp win10/vdisk1.img btrfs/basevm/vdisk1.img

 

Step 4 --Once done copying or configuring your vm (Make sure its shutdown) execute the follow from the commandline (ssh)

# btrfs subvolume snapshot btrfs/basevm btrfs/cloneofbasevm1

 

Step 5 --Now you can make a second vm within the Unraid webgui. In the "Primary vDisk Location:" field make sure you are pointing to the cloneofbasevm1 folder.

Also make sure you leave the "OS Install ISO:" empty, as the OS is already installed.

 

Now if you want to create a second VM just repeat Step 4.

 

Example:

 

Second VM Clone:

# btrfs subvolume snapshot btrfs/basevm btrfs/cloneofbasevm2

 

Third VM Clone:

# btrfs subvolume snapshot btrfs/basevm btrfs/cloneofbasevm3

 

Good Luck !!

 

Source: https://www.linux.com/learn/how-create-and-manage-btrfs-snapshots-and-rollbacks-linux-part-2

 

 

Link to comment

I asked about this a while back and never got a response so I did some testing of my own.

 

I created a VM -- installed Windows 7 + drivers etc. Then I just copied the vdisk and created a new VM except that I selected the copy of the vdisk that I made. Boot up the VM and it worked.

 

So as far as I can tell this is all that is required. I'm pretty new to VMs so I could be wrong but it worked fine for me.

 

Yes that will work, but the problem is that it increases the disk space required by the VDisk.  For example if a VM has a 30GB virtual hard drive but is only using 13GB of it.  It actually only takes up 13GB on the cache drive.  The copy will take up the 30GB on the cache drive.  The same is true in vmware esxi, if you just try to copy the thin disk it will expand its size.

 

Note that ls -l will show you it takes up 30GB, but du will show you what it really uses.

Link to comment

Hi i was just wondering the same so i googled it :D

 

This method uses the power of BTRFS filesystem: we create one basevm and make snapshots for our Clone VM's. This wil not use any additional space on your harddrive. Except for changes made in the clone.

 

So if you create one Windows 10 machine (around 17 GB) you can make for example 15 clones of it and still only use 17 GB of disk space.

 

BEFORE YOU START: Im not sure if this is the best way to to this but it works !

 

Before any one else tries this, I am pretty sure the answer is no this is not the best way.  Maybe someone with more experience can chime in, but that sounds like a bad way to do it.

 

What you are describing is making snapshots of the BTRFS file system.  Essentially each "clone" you make is a diff of the base sub file system.  And in the end it will probably end up being a full copy of the original disk, since the .img file will change enough from the original where it will be a copy instead of a diff.  I don't know enough about BTRFS, but i suspect there will be a performance hit there. 

 

The first few references from googling "BTRFS snapshots KVM" all pretty much say the same thing: Don't use BTRFS for VM storage.  Or at least don't enable copy on write for storing the image files because they are essentially one giant file.

Link to comment
  • 2 months later...

I asked about this a while back and never got a response so I did some testing of my own.

 

I created a VM -- installed Windows 7 + drivers etc. Then I just copied the vdisk and created a new VM except that I selected the copy of the vdisk that I made. Boot up the VM and it worked.

 

So as far as I can tell this is all that is required. I'm pretty new to VMs so I could be wrong but it worked fine for me.

 

Yes that will work, but the problem is that it increases the disk space required by the VDisk.  For example if a VM has a 30GB virtual hard drive but is only using 13GB of it.  It actually only takes up 13GB on the cache drive.  The copy will take up the 30GB on the cache drive.  The same is true in vmware esxi, if you just try to copy the thin disk it will expand its size.

 

Note that ls -l will show you it takes up 30GB, but du will show you what it really uses.

 

Easiest way to copy a vdisk with it taking up more space is to install libguestfs-tools to a ubuntu vm mount your shared vm folder to the vm. open terminal. cd to the mounted vm share and run

 

virt-sparsify --format raw --convert qcow2 --compress vdisk1.img vdiskcopy.qcow2

 

This will sparsify convert raw to qcow2 then compress the image so your copy is correct size

 

You will need as much space in your ubuntu vm as the raw image is as it uses a temp file to do this.

  • Like 1
Link to comment

I asked about this a while back and never got a response so I did some testing of my own.

 

I created a VM -- installed Windows 7 + drivers etc. Then I just copied the vdisk and created a new VM except that I selected the copy of the vdisk that I made. Boot up the VM and it worked.

 

So as far as I can tell this is all that is required. I'm pretty new to VMs so I could be wrong but it worked fine for me.

 

I'm with you on this. I have been keeping full size backups and making clone VMs simply by making copies of my initial vdisk inside mc from the get go. Since my VMs are only 60GB, I don't feel like I am using too much space that I even bother with compression, etc. Especially when it takes less than 5 mins to make a copy. I think I have like 15 full backups on the array since I sorted out all of my unRAID VM issues, and have found copying to be the quickest solution for me. Most important thing is keeping a copy of those XMLs, especially if you've unlocked a free Win10 upgrade for that VM's specific uuid. After that, it's just copy and paste and you can restore a backup, make a cloned VM, whatever.

Link to comment
  • 4 weeks later...

Just thought I'd chime in here with an even easier method that doesn't require using btrfs subvolumes or btrfs snapshots:

 

cp --reflink /path/to/vdisk.img /path/to/snapshot.img

 

The --reflink command to cp instructs CP to use the COW features of BTRFS to create a "reflink" copy of the file, which is essentially a file-level snapshot.  You can even delete the underlying base image and the snapshot will continue to work since btrfs remembers the dependencies that reflinks have on underlying block-level data.

  • Like 3
Link to comment
  • 1 year later...
  • 10 months later...
  • 5 months later...
On 5/17/2016 at 10:05 AM, Naldinho said:

I asked about this a while back and never got a response so I did some testing of my own.

 

I created a VM -- installed Windows 7 + drivers etc. Then I just copied the vdisk and created a new VM except that I selected the copy of the vdisk that I made. Boot up the VM and it worked.

 

So as far as I can tell this is all that is required. I'm pretty new to VMs so I could be wrong but it worked fine for me.

 

this worked really well and straight forward. 

Link to comment
  • 1 year later...

 

On 5/17/2016 at 5:05 PM, Naldinho said:

I created a VM -- installed Windows 7 + drivers etc. Then I just copied the vdisk and created a new VM except that I selected the copy of the vdisk that I made. Boot up the VM and it worked.

So as far as I can tell this is all that is required. I'm pretty new to VMs so I could be wrong but it worked fine for me.

 

With that approach I end up in UEFI shell at power on for a Debian vdisk1.img copy.

Removing the Unraid share makes no difference.(v6.8.3)

Edit:

It seems to be such cause

 

I found that when I run the following in the presented UEFI shell for my copied Debian image:

fs0:

cd efi/debian

grubx64.efi

that it will boot, but it's not persistent...

 

The fix: https://wiki.debian.org/GrubEFIReinstall

# Reinstalling grub-efi on your hard drive

# Check that the computer booted in computer in EFI mode:
[ -d /sys/firmware/efi ] && echo "EFI boot on HDD" || echo "Legacy boot on HDD"
# should return "EFI boot on HDD".

# After starting a root shell ( if you boot from a live media, you should start a chroot shell instead, as explained in https://help.ubuntu.com/community/Grub2/Installing#via_ChRoot ) check that your EFI system partition (most probably /dev/sda1) is mounted on /boot/efi. If the /boot/efi directory does not exist, you will need to create it.
# find partition
lsblk # sda1 => vda1
mount /dev/vda1 /boot/efi
# Reinstall the grub-efi package
apt-get install --reinstall grub-efi

# Put the debian bootloader in /boot/efi and create an appropriate entry in the computer NVRAM
grub-install

#Re create a grub config file based on your disk partitioning schema
update-grub

#You should check afterwards that:
#Check 1. the bootloader is existing in /boot/efi/EFI/debian/grubx64.efi

file /boot/efi/EFI/debian/grubx64.efi

# /boot/efi/EFI/debian/grubx64.efi: PE32+ executable (EFI application) x86-64 (stripped to external PDB), for MS Windows

# Check 2. the nvram entry was properly created.
efibootmgr --verbose | grep debian
# You can now reboot, and Grub should greet you. 

 

Edited by 54tgedrg45
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.