I have a KVM Windows Vista guest whose virtual disk is a volume on a physical disk I have on my server (eg: /dev/VG/lvm_guest-name). The volume is about 80GB, but the space that is really used is about 30GB.
My task is to shrink this "disk image" from 80GB to something like 40GB and then to use this disk image on another external disk (on a Linux 83 partition which exists on a external USB disk). So at the end of the day I can release that logical volume so I will gain another 80GB on the server.
How to move kvm guest to usb
Here are involved several tasks:
- resize the Windows partition from 80GB to 40GB; so /dev/VG/lvm_guest-name will have 80GB but the Windows disk manager will claim just 40GB of this logical disk
- copy only the first 40GB of the Windows logical disk (which are stored on /dev/VG/lvm_guest-name) to an external USB disk partition/raw image file by using dd command; I need a progress of raw data transfer too (dd will take considerable amount of time)
- use the external USB/raw image disk partition (from the USB external disk) as my KVM guest disk image instead of the old logical volume
- remove the logical volume from the server so that space can be used to extend another logical volume size or even to create a new logical volume.
1. Resize the disk partition
Boot into your KVM guest. While in Windows Vista open Computer Management and then select Disk Management. Select your 80GB disk and right-click the partition C. Select "Shrink Volume" on the popup menu. Specify 40960 (40GB) on the "Enter the amount of space to shrink in MB". So your disk will have "Total size after shrink in MB" exactly half (40960 MB). Click Shrink button. This could take a while. When is finished you will se a C 40GB partition and a 40GB Unallocated space.
2. Copy a logical volume disk
There is no better toy than dd when comes to play with the low-level raw data. Even if you will not have (by default) a GUI for this tool you can find many front-ends for dd on Google (just type "GUI for dd"). I like the console interface, btw.
dd have (among many other options) two options which help us to specify the input file reader (if) and the output file writer (of). We need to copy at low-level the first 40GB of raw data from our logical volume to the external USD disk partition all we have to do is:
dd if=/dev/VG/lvm_guest-name of=<dest> bs=512 count=$[40*1024*1024*1024]
If you take a look at dd manual (man dd) you will find a statement like this:
Sending a USR1 signal to a running 'dd' process makes it print I/O statistics to standard error and then resume copying.
So all we have to to is to send this signal to the running dd process. How to send a signal to the process? In fact the same manual teach as this thing : kill -USR1 $pid (where $pid should be the PID of dd process).
First we will find the dd PID (pgrep '^dd$' or ps aux|grep "dd if=") and later we'll send a USR1 signal to that process by kill -USR1
~# pgrep '^dd$'
~# kill -USR1 2127
On the terminal where dd runs you will see an output like:
2882098688 bytes (33 GB) copied, 5242.94 s, 6.3 MB/s
If you want to make dd to print a statistic every minute then all you have to do is to send that signal every minute. This can be done automatically by:
~# watch -n 60 kill -USR1 2127
where 2127 will be your dd PID (2127 in my example).
3. Use the new image as your guest disk
If you have copied the /dev/VG/lvm_guest-name to partition /dev/sdc3 (or /path/guest-raw-image) then you can use that disk partition/image as your guest disk. This can be done easily by editing the guest configuration file (via virsh or VMM). Boot your guest again. It should work like a charm.
4. Remove the logical volume from the server
So you have moved the 80GB disk to an external USB disk partition/image and successfully booted your KVM guest from the new disk/image. Cool!
Now release the space you don't need any more by:
~$ lvremove /dev/VG/lvm_guest-name
That's all folks!
Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.
Latest posts by Eugen Mihailescu (see all)
- Symfony authentication redirect after each request - January 22, 2018
- Emerging Perl without Perl in Gentoo - September 26, 2017
- How to choose your decoupling capacitor - September 9, 2017