KVM镜像操作qemu-img命令
创建镜像 qemu-img create# 创建一个设备空间大小为10G的镜像qemu-img create -f qcow2 centos7-guest.qcow2 10G# 基于backing_file创建大小为20G的镜像, 使用-b参数, -F用于指定backing_file的格式qemu-img create -F qcow2 -b /path/to/backing-kvm.img -f qcow2 /path/to/target.qcow2 20G# 基于backing_file创建大小为20G的镜像, 使用-o参数, 都在-o参数内指定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 infoqemu-img info rhel6u3.img
转换镜像格式 qemu-img convert使用-f指定源文件格式, 用-O指定目标文件格式 qemu-img convert -f raw -O qcow2 centos.img centos.qcow2qemu-img convert -f qcow2 -O raw centos.qcow2 centos.raw
缩小镜像实际占用的磁盘大小在虚机中进行文件相关的操作(复制, 解压)后, 即使删除了文件, 虚机依然占用文件空间, 如果要减小空间占用, 可以通过如下的方式
Linux虚机端Linux虚机端, trim a device and free all unused storage on the underlying device 将未使用的空间用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
压缩虚机镜像在宿主机进行操作 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命令将旧镜像中的文件系统扩充到新镜像中, 用于调整虚机磁盘大小 # 创建一个50G容量的qcow2空白镜像qemu-img create -f qcow2 /data/images/win2012-50gb.qcow2 50G# 将镜像扩充到新镜像中去, 通过--expand指定需要扩张的分区virt-resize --expand /dev/sda2 /data/images/win2012.qcow2 /data/images/win2012-50gb.qcow2 此时只是将在虚机磁盘上增加了可用空间, 但是未体现到文件系统, 启动虚机后, 在虚机中扩充分区大小 # 查看分区df -h# 查看分区对应的磁盘大小, 能看出来/dev/vda1已经增长, 但是未体现到df -h的输出中fdisk -l# 如果/dev/vda1格式是xfsxfs_growfs /dev/vda1# 如果/dev/vda1格式是ext2,3,4resize2fs /dev/vda1
对镜像进行扩容操作下面的例子演示了如何将vm_n01.qcow2这个镜像扩容到40G # 先检查镜像的分区结构, 以便后面对指定的分区进行扩容virt-filesystems --long --parts --blkdevs -h -a vm_n01.qcow2 # 更名mv vm_n01.qcow2 vm_n01.qcow2.bak# 用原名创建一个40G的空白镜像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# 查看分区对应的磁盘大小, 能看出来/dev/vda1已经增长, 但是未体现到df -h的输出中fdisk -l# 如果/dev/vda1格式是xfsxfs_growfs /dev/vda1# 如果/dev/vda1格式是ext2,3,4resize2fs /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_csume2fsck: 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.gzsudo tar xvf e2fsprogs-1.46.4.tar.gzsudo cd e2fsprogs-1.46.4sudo ./configuresudo makesudo make installsudo yum -y install libguestfs-xfs
使用RAW格式对镜像扩容Ubuntu20.04 Focal Cloudimage扩容, 参考 https://www.cnblogs.com/milton/p/15382673.html # resize原始镜像qemu-img resize focal-server-cloudimg-amd64-50g.qcow2 50G# 转换为raw格式镜像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# 运行partedparted focal-server-cloudimg-amd64-50g.raw ######################## 开始GNU Parted 3.1Using /data/backup/vm_images/focal-server-cloudimg-amd64-50g.rawWelcome 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) orcontinue with the current setting? Fix/Ignore? Fix Model: (file)Disk /data/backup/vm_images/focal-server-cloudimg-amd64-50g.raw: 53.7GBSector size (logical/physical): 512B/512BPartition Table: gptDisk Flags: Number Start End Size File system Name Flags14 1049kB 5243kB 4194kB bios_grub15 5243kB 116MB 111MB fat32 boot 1 116MB 2361MB 2245MB ext4(parted) resizepart Partition number? 1 End? [2361MB]? 53.7G 1 116MB 53.7GB 53.6GB ext4(parted) q ########################### 结束# 查看raw格式的分区大小是否正确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 -# 转回qcow2格式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
参考 下载地址: Docker容器镜像相关命令基本介绍与使用 ZooKeeper分布式协调服务设计核心概念及安装配置 |