KVM镜像操作
qemu-img命令
创建镜像 qemu-img create
qemu-img create -f qcow2 centos7-guest.qcow2 10G
qemu-img create -F qcow2 -b /path/to/backing-kvm.img -f qcow2 /path/to/target.qcow2 20G
qemu-img create -f qcow2 -o backing_file=/path/to/rhel6u3.img,backing_fmt=qcow2,size=20G /path/to/target.qcow2
关于backing_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).
查看镜像信息 qemu-img info
qemu-img info rhel6u3.img
转换镜像格式 qemu-img convert
使用-f指定源文件格式, 用-O指定目标文件格式
qemu-img convert -f raw -O qcow2 centos.img centos.qcow2
qemu-img convert -f qcow2 -O raw centos.qcow2 centos.raw
缩小镜像实际占用的磁盘大小
在虚机中进行文件相关的操作(复制, 解压)后, 即使删除了文件, 虚机依然占用文件空间, 如果要减小空间占用, 可以通过如下的方式
Linux虚机端
Linux虚机端, trim a device and free all unused storage on the underlying device
fstrim -av
将未使用的空间用0填充, 因为删除的文件依然存在, 如果不写零, 无法回收这些空间. 这一步会将虚机预设的空间全部填充, 取决于磁盘性能, 可能会很慢
dd if=/dev/zero of=/mytempfile
rm -f /mytempfile
Windows虚机端
对于Win7/2008R2 及以上版本会自动裁剪(trim), 可以通过下面命令查看是否开启
fsutil behavior query DisableDeleteNotify
上面应当会输出0, 否则使用下面的命令设置
fsutil behavior set DisableDeleteNotify 0
然后执行下面的命令
- 整理碎片, 用windows自带的或者第三方工具例如 Defraggler or UltraDefrag.
- 使用SDelete(推荐)或者Eraser, 将空闲空间填充0
使用SDelete
sdelete -z
sdelete -z e:
压缩虚机镜像
在宿主机进行操作
qemu-img convert -O qcow2 vm_pbc.qcow2 vm_pbc_shrink.qcow2
带压缩参数, 会比较慢
qemu-img convert -O qcow2 -c vm_pbc.qcow2 vm_pbc_shrink.qcow2
virt-filesystems命令
查看镜像的磁盘大小
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命令
将旧镜像中的文件系统扩充到新镜像中, 用于调整虚机磁盘大小
qemu-img create -f qcow2 /data/images/win2012-50gb.qcow2 50G
virt-resize --expand /dev/sda2 /data/images/win2012.qcow2 /data/images/win2012-50gb.qcow2
此时只是将在虚机磁盘上增加了可用空间, 但是未体现到文件系统, 启动虚机后, 在虚机中扩充分区大小
df -h
fdisk -l
xfs_growfs /dev/vda1
resize2fs /dev/vda1
对镜像进行扩容操作
下面的例子演示了如何将vm_n01.qcow2这个镜像扩容到40G
virt-filesystems --long --parts --blkdevs -h -a vm_n01.qcow2
mv vm_n01.qcow2 vm_n01.qcow2.bak
qemu-img create -f qcow2 vm_n01.qcow2 40G
virt-resize --expand /dev/sda1 vm_n01.qcow2.bak vm_n01.qcow2
virt-filesystems --long --parts --blkdevs -h -a vm_n01.qcow2
rm vm_n01.qcow2.bak
用新镜像导入创建虚机后
df -h
fdisk -l
xfs_growfs /dev/vda1
resize2fs /dev/vda1
遇到e2fsck版本太低的问题 centos7自带的e2fsck版本过低, 运行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!
解决: 编译最新版本
# 需要预先安装 wget gcc-c++
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
使用RAW格式对镜像扩容
Ubuntu20.04 Focal Cloudimage扩容, 参考 https://www.cnblogs.com/milton/p/15382673.html
qemu-img resize focal-server-cloudimg-amd64-50g.qcow2 50G
qemu-img convert -f qcow2 -O raw focal-server-cloudimg-amd64-50g.qcow2 focal-server-cloudimg-amd64.raw
mv focal-server-cloudimg-amd64.raw focal-server-cloudimg-amd64-50g.raw
parted focal-server-cloudimg-amd64-50g.raw
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
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 -
qemu-img convert -f raw -O qcow2 focal-server-cloudimg-amd64-50g.raw focal-server-cloudimg-amd64-50g-resized.qcow2
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 -
参考
|