KVM virtual machine image operation, capacity expansion and compression

KVM mirror operation

QEMU img command

Create mirror qemu-img create

# Create an image with a device space of 10G
qemu-img create -f qcow2 centos7-guest.qcow2 10G
# Based on backing_file creates an image with a size of 20G. Use the - b parameter and - F to specify backing_file format
qemu-img create -F qcow2 -b /path/to/backing-kvm.img -f qcow2 /path/to/target.qcow2 20G
# Based on backing_file creates an image with a size of 20G and uses the - o parameter, which is specified in the - o parameter
qemu-img create -f qcow2 -o backing_file=/path/to/rhel6u3.img,backing_fmt=qcow2,size=20G /path/to/target.qcow2

About backing_ Description of file
If the option backing_file is specified, then the image will record only the differences from backing_file. No size needs to be specified in this case. backing_file will never be modified unless you use the "commit" monitor command (or qemu-img commit).

View image information QEMU img info

qemu-img info rhel6u3.img

Convert image format QEMU img convert

Use - f to specify the source file format and - O to specify the destination file format

qemu-img convert -f raw -O qcow2 centos.img centos.qcow2
qemu-img convert -f qcow2 -O raw centos.qcow2 centos.raw

Reduce the actual disk size occupied by the mirror

After performing file related operations (copying and decompression) in the virtual machine, even if the file is deleted, the virtual machine still occupies file space. If you want to reduce the space occupation, you can do so in the following ways

Linux virtual machine terminal

trim a device and free all unused storage on the underlying device

fstrim -av

Fill the unused space with 0 because the deleted file still exists. If you don't write zero, you can't reclaim the space This step will fill all the preset space of the virtual machine. Depending on the disk performance, it may be very slow

dd if=/dev/zero of=/mytempfile
# Delete generated file
rm -f /mytempfile

Windows virtual machine terminal

For Win7/2008R2 and above versions, trim will be automatically cut. You can check whether it is enabled through the following command

fsutil behavior query DisableDeleteNotify

The above should output 0, otherwise use the following command to set

fsutil behavior set DisableDeleteNotify 0

Then execute the following command

  • Defragment with windows native or third-party tools, such as defragger or ultradefrag
  • Use SDelete (recommended) or Eraser to fill the free space with 0

Using SDelete

  • Download SDelete Save to C:\windows\system32\
  • Run the following command to fill in zero
sdelete -z
  • If there are multiple partitions, such as E:, use
sdelete -z e:

Compress virtual machine image

Operate on the host

qemu-img convert -O qcow2 vm_pbc.qcow2 vm_pbc_shrink.qcow2

With compression parameters, it will be slow

qemu-img convert -O qcow2 -c vm_pbc.qcow2 vm_pbc_shrink.qcow2

Virt filesystems command

View the disk size of the mirror

virt-filesystems --long --parts --blkdevs -h -a c7guest1.qcow2c 
Name       Type       MBR  Size  Parent
/dev/sda1  partition  83   8.0G  /dev/sda
/dev/sda   device     -    8.0G  -

Virt resize command

Expand the file system in the old image to the new image to adjust the size of the virtual machine disk

# Create a 50G capacity qcow2 blank image
qemu-img create -f qcow2 /data/images/win2012-50gb.qcow2 50G
# Expand the image to the new image, and specify the partition to be expanded through -- expand
virt-resize --expand /dev/sda2 /data/images/win2012.qcow2 /data/images/win2012-50gb.qcow2

At this time, only the available space on the virtual machine disk is increased, but it is not reflected in the file system. After starting the virtual machine, expand the partition size in the virtual machine

# View partition
df -h
# Check the disk size corresponding to the partition. You can see that / dev/vda1 has increased, but it is not reflected in the output of df -h
fdisk -l
# If / dev/vda1 format is xfs
xfs_growfs /dev/vda1
# If the format of / dev/vda1 is ext2,3,4
resize2fs /dev/vda1

Expand the image

The following example demonstrates how to use vm_n01.qcow2 this image is expanded to 40G

# First check the partition structure of the image so that the specified partition can be expanded later
virt-filesystems --long --parts --blkdevs -h -a vm_n01.qcow2 
# Rename
mv vm_n01.qcow2 vm_n01.qcow2.bak
# Create a 40G blank image with the original name
qemu-img create -f qcow2 vm_n01.qcow2 40G
# Import an old image into a blank image
virt-resize --expand /dev/sda1 vm_n01.qcow2.bak vm_n01.qcow2
# Check for new images after import
virt-filesystems --long --parts --blkdevs -h -a vm_n01.qcow2
# Delete old mirror
rm vm_n01.qcow2.bak

After creating virtual machine with new image import

# View partition
df -h
# The size of the disk that can be seen in DF / dev but not in DF / dev can be seen
fdisk -l
# If / dev/vda1 format is xfs
xfs_growfs /dev/vda1
# If the format of / dev/vda1 is ext2,3,4
resize2fs /dev/vda1

The e2fsck version is too low
The built-in e2fsck version of CentOS 7 is too low, and an error is reported when running resize

virt-resize: error: libguestfs error: resize2fs: e2fsck 1.42.9 
(28-Dec-2013)
/dev/sda1 has unsupported feature(s): metadata_csum
e2fsck: Get a newer version of e2fsck!

Solution: compile the latest version

# wget gcc-c needs to be pre installed++
sudo wget https://mirrors.edge.kernel.org/pub/linux/kernel/people/tytso/e2fsprogs/v1.46.4/e2fsprogs-1.46.4.tar.gz
sudo tar xvf e2fsprogs-1.46.4.tar.gz
sudo cd e2fsprogs-1.46.4
sudo ./configure
sudo make
sudo make install
sudo yum -y install libguestfs-xfs

Use RAW format to expand image capacity

Ubuntu20.04 Focal Cloudimage expansion, refer to https://www.cnblogs.com/milton/p/15382673.html

# resize original image
qemu-img resize focal-server-cloudimg-amd64-50g.qcow2 50G
# Convert to raw format image
qemu-img convert -f qcow2 -O raw focal-server-cloudimg-amd64-50g.qcow2 focal-server-cloudimg-amd64.raw
# Rename to prevent confusion
mv focal-server-cloudimg-amd64.raw focal-server-cloudimg-amd64-50g.raw
# Run parted
parted focal-server-cloudimg-amd64-50g.raw 
######################## start
GNU Parted 3.1
Using /data/backup/vm_images/focal-server-cloudimg-amd64-50g.raw
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p                                                                
Error: The backup GPT table is not at the end of the disk, as it should be.
This might mean that another operating system believes the disk is smaller.
Fix, by moving the backup to the end (and removing the old backup)?
Fix/Ignore/Cancel? Fix                                                    
Warning: Not all of the space available to
/data/backup/vm_images/focal-server-cloudimg-amd64-50g.raw appears to be used,
you can fix the GPT to use all of the space (an extra 100245504 blocks) or
continue with the current setting? 
Fix/Ignore? Fix                                                           
Model:  (file)
Disk /data/backup/vm_images/focal-server-cloudimg-amd64-50g.raw: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
14      1049kB  5243kB  4194kB                     bios_grub
15      5243kB  116MB   111MB   fat32              boot
 1      116MB   2361MB  2245MB  ext4

(parted) resizepart                                                     
Partition number? 1                                                       
End?  [2361MB]? 53.7G                                                     
(parted) p                                                                
Model:  (file)
Disk /data/backup/vm_images/focal-server-cloudimg-amd64-50g.raw: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
14      1049kB  5243kB  4194kB                     bios_grub
15      5243kB  116MB   111MB   fat32              boot
 1      116MB   53.7GB  53.6GB  ext4
(parted) q                                                                
########################### end
# Check whether the partition size in raw format is correct
virt-filesystems --long --parts --blkdevs -h -a focal-server-cloudimg-amd64-50g.raw 
Name        Type       MBR  Size  Parent
/dev/sda1   partition  -    50G   /dev/sda
/dev/sda14  partition  -    4.0M  /dev/sda
/dev/sda15  partition  -    106M  /dev/sda
/dev/sda    device     -    50G   -

# Back to qcow2 format
qemu-img convert -f raw -O qcow2 focal-server-cloudimg-amd64-50g.raw focal-server-cloudimg-amd64-50g-resized.qcow2

# Check again
virt-filesystems --long --parts --blkdevs -h -a focal-server-cloudimg-amd64-50g-resized.qcow2 
Name        Type       MBR  Size  Parent
/dev/sda1   partition  -    50G   /dev/sda
/dev/sda14  partition  -    4.0M  /dev/sda
/dev/sda15  partition  -    106M  /dev/sda
/dev/sda    device     -    50G   -

reference resources

Keywords: Linux kvm

Added by maskme on Sat, 26 Feb 2022 17:59:38 +0200