管理虚拟磁盘

 

[root@kvm1 ~]# qemu-img --help | grep Supported
Supported formats: vvfat vpc vmdk vhdx vdi ssh sheepdog rbd raw host_cdrom host_floppy host_device file qed qcow2 qcow parallels nbd iscsi gluster dmg tftp ftps ftp https http cloop bochs blkverify blkdebug
[root@kvm1 ~]#

[root@kvm1 vm]# qemu-img create t1.img 1g
Formatting 't1.img', fmt=raw size=1073741824   #默认是raw格式
[root@kvm1 vm]# qemu-img info t1.img      #查看文件信息
image: t1.img
file format: raw
virtual size: 1.0G (1073741824 bytes)
disk size: 0          <=====注意
[root@kvm1 vm]# du -h t1.img
0       t1.img
[root@kvm1 vm]#                   #ext4 支持稀疏文件

[root@kvm1 vm]# qemu-img create -f qcow2 -o ?
Supported options:
size             Virtual disk size
compat           Compatibility level (0.10 or 1.1)
backing_file     File name of a base image        #指定后端镜像文件
backing_fmt      Image format of the base image    #指定后端镜像文件的格式
encryption       Encrypt the image            #用于设置加密
cluster_size     qcow2 cluster size          #指定镜像族的大小,取值在512到2M  默认是64K
preallocation    Preallocation mode (allowed values: off, metadata, falloc, full)   #指定镜像文件的预分配模式
lazy_refcounts   Postpone refcount updates
[root@kvm1 vm]# dd if=/dev/zero of=flat1.img bs=1024k count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 1.77259 s, 592 MB/s
[root@kvm1 vm]# qemu-img info flat1.img
image: flat1.img
file format: raw
virtual size: 1.0G (1048576000 bytes)
disk size: 1.0G      <=======注意
[root@kvm1 vm]# ll
total 3105308
-rw------- 1 root root 8591507456 Jun  5 20:20 centos6.10a.qcow2
-rw-r--r-- 1 root root 1136132096 Jun  5 20:55 centos6.10b.qcow2
-rw-r--r-- 1 root root 1048576000 Jun  6 09:30 flat1.img
drwx------ 2 root root      16384 Jun  5 15:14 lost+found
-rw-r--r-- 1 root root 1073741824 Jun  6 09:18 t1.img
[root@kvm1 vm]# du flat1.img
1024004 flat1.img
[root@kvm1 vm]# dd if=/dev/zero of=flat2.img bs=1024k count=0 seek=1024   #直接跳到文件尾 ,块为0
0+0 records in
0+0 records out
0 bytes (0 B) copied, 7.691e-05 s, 0.0 kB/s
[root@kvm1 vm]# qemu-img info flat2.img
image: flat2.img
file format: raw
virtual size: 1.0G (1073741824 bytes)
disk size: 0         <=====注意
[root@kvm1 vm]# du -lh flat*
1001M   flat1.img
0       flat2.img

[root@kvm1 vm]# cp flat1.img flat1a.img
[root@kvm1 vm]# cp flat2.img flat2a.img
[root@kvm1 vm]# du -lh flat*
1001M   flat1a.img
1001M   flat1.img
0       flat2a.img
0       flat2.img
[root@kvm1 vm]# man cp
 sparse SOURCE files are detected by a crude  heuristic  and
       the corresponding DEST file is made sparse as well.  That is the behav‐
       ior selected by --sparse=auto.  Specify  --sparse=always  to  create  a
       sparse  DEST  file  whenever  the  SOURCE  file  contains a long enough
       sequence of zero bytes.  Use  --sparse=never  to  inhibit  creation  of
       sparse files.
[root@kvm1 vm]# cp flat1.img flat1b.img --sparse=always   #确保拷贝的是稀疏文件
[root@kvm1 vm]# du -lh flat*
1001M   flat1a.img
0       flat1b.img
1001M   flat1.img
0       flat2a.img
0       flat2.img

[root@kvm1 vm]# du -lh flat*
1001M   flat1a.img
0       flat1b.img
1001M   flat1.img
0       flat2a.img
1.1G    flat2b.img
0       flat2.img
[root@kvm1 vm]#

[root@kvm1 vm]# man qemu-img
"preallocation"
               Preallocation mode (allowed values: "off", "falloc", "full").
               "falloc" mode preallocates space for image by calling
               posix_fallocate().  "full" mode preallocates space for image by
               writing zeros to underlying storage.

 

[root@kvm1 vm]# qemu-img create -f qcow2 test1.qcow2 1g
Formatting 'test1.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 lazy_refcounts=off
[root@kvm1 vm]# qemu-img info test1.qcow2
image: test1.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@kvm1 vm]# qemu-img create -f qcow2 test2.qcow2 1g -o preallocation=off
Formatting 'test2.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 preallocation='off' lazy_refcounts=off
[root@kvm1 vm]# qemu-img info test2.qcow2
image: test2.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

[root@kvm1 vm]# qemu-img create -f qcow2 test3.qcow2 1g -o preallocation=metadata
Formatting 'test3.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 preallocation='metadata' lazy_refcounts=off
[root@kvm1 vm]# qemu-img info test3.qcow2
image: test3.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 328K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@kvm1 vm]# qemu-img create -f qcow2 test4.qcow2 1g -o preallocation=falloc
Formatting 'test4.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 preallocation='falloc' lazy_refcounts=off
[root@kvm1 vm]# qemu-img info test4.qcow2
image: test4.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 1.0G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@kvm1 vm]# qemu-img create -f qcow2 test5.qcow2 1g -o preallocation=full
Formatting 'test5.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 preallocation='full' lazy_refcounts=off
[root@kvm1 vm]# qemu-img info test5.qcow2
image: test5.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 1.0G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@kvm1 vm]# ll -h test*
-rw-r--r-- 1 root root 193K Jun  6 09:57 test1.qcow2
-rw-r--r-- 1 root root 193K Jun  6 09:58 test2.qcow2
-rw-r--r-- 1 root root 1.1G Jun  6 10:00 test3.qcow2
-rw-r--r-- 1 root root 1.1G Jun  6 10:01 test4.qcow2
-rw-r--r-- 1 root root 1.1G Jun  6 10:02 test5.qcow2

[root@kvm1 vm]# du -lh test*
196K    test1.qcow2
196K    test2.qcow2
332K    test3.qcow2
1.1G    test4.qcow2
1.1G    test5.qcow2
[root@kvm1 vm]#

 

[root@kvm1 vm]# cp centos6.10b.qcow2 base-cetos6.10b.qcow2
[root@kvm1 vm]# ll
total 3190752
-rw-r--r-- 1 root root 1136132096 Jun  6 10:20 base-cetos6.10b.qcow2
-rw------- 1 root root 8591507456 Jun  5 20:20 centos6.10a.qcow2
-rw-r--r-- 1 root root 1136132096 Jun  5 20:55 centos6.10b.qcow2
drwx------ 2 root root      16384 Jun  5 15:14 lost+found
-rw-r--r-- 1 root root 1073741824 Jun  6 09:18 t1.img
[root@kvm1 vm]# qemu-img create -f qcow2  -o backing_file=base-cetos6.10b.qcow2  oa-disk0.qcow2
Formatting 'oa-disk0.qcow2', fmt=qcow2 size=10737418240 backing_file='base-cetos6.10b.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off
[root@kvm1 vm]# qemu-img info base-cetos6.10b.qcow2
image: base-cetos6.10b.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.1G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@kvm1 vm]# qemu-img create -f qcow2  -o backing_file=base-cetos6.10b.qcow2  erp-disk0.qcow2
Formatting 'erp-disk0.qcow2', fmt=qcow2 size=10737418240 backing_file='base-cetos6.10b.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off
[root@kvm1 vm]# ll *disk0*
-rw-r--r-- 1 root root 197120 Jun  6 10:25 erp-disk0.qcow2
-rw-r--r-- 1 root root 197120 Jun  6 10:23 oa-disk0.qcow2
[root@kvm1 vm]#

 

克隆
[root@kvm1 vm]# virt-install --import --name=oa --vcpus=1 --ram=512 --disk path=/vm/oa-disk0.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --os-type=linux

[root@kvm1 vm]# virt-install --import --name=erp --vcpus=1 --ram=512 --disk path=/vm/erp-disk0.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --os-type=linux

[root@kvm1 vm]# qemu-img resize oa-disk0.qcow2 +10G

posted @   Sailing-101  阅读(49)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示