qemu-img create創建磁盤


qemu-img工具是qemu軟件的一部分,更加詳細的使用wiki可以參考qemu官網,以下在pve下操作,首先查看man qemu-img,如下:

注意size 默認單位為byte,比如size為10 表示文件大小10byte  10M表示文件大小10M

 

依上,解釋-u參數,當指定-b backing file時,當不使用-u參數時,backing file會被打開驗證文件是否合法;當使用-u參數時,表示開啟了unsafe backing file mode模式,意味着甚至當backing file文件不予指定時,也可以create一個基於不存在backing file(只是指定文件名,該文件可以不存在)的img文件,但a matching backing file must be created or additional options be used to make the backing specification valid(一個匹配的backing file必須后來創建或者通過額外的選項來保證backing specification合法)。當通過backing file創建img時,可以不必指定size參數。

 

 

 qemu-img使用img后綴文件(也即raw格式文件時)來作為backing file創建disk image時,提示不支持,如下:

 

 

 2)關於qemu的qcow2格式,在man qemu-img手冊中最后有文件格式的相關說明,如下:

       qcow2

           QEMU image format, the most versatile format. Use it to have smaller images (useful if your filesystem does not supports holes, for example on Windows), optional AES encryption, zlib based compression and support of multiple VM snapshots.

            Supported options:

 

            "backing_file"

               File name of a base image (see create subcommand)

            "backing_fmt"

               Image format of the base image

            "encryption"

               If this option is set to "on", the image is encrypted.Encryption uses the AES format which is very secure (128 bit keys). Use a long password (16 characters)

               to get maximum protection.

            "cluster_size"

               Changes the qcow2 cluster size (must be between 512 and 2M). Smaller cluster sizes can improve the  image file size whereas larger cluster sizes generally provide better performance.

            "preallocation"

               Preallocation mode (allowed values: "off", "metadata", "falloc", "full"). An image with preallocated  metadata is initially larger but can improve performance when the image needs to grow. "falloc" and   "full" preallocations are like the same options of "raw" format, but sets up metadata also.

以上參數都是通過-o 選項來指定,

其中的 preallocation解釋如下:

off模式:缺省預分配策略,即不使用預分配策略

metadata模式:分配qcow2的元數據(metadata),預分配后的虛擬磁盤仍然屬於稀疏映像類型(allocates qcow2 metadata, and it's still a sparse image.)

full模式:分配所有磁盤空間並置零,預分配后的虛擬磁盤屬於非稀疏映像類型(allocates zeroes and makes a non-sparse image)

falloc模式:使用posix_fallocate()函數分配文件的塊並標示它們的狀態為未初始化,相對full模式來說,創建虛擬磁盤的速度要快很多(which uses posix_fallocate() to "allocate blocks and marking them as uninitialized", and is relatively faster than writing out zeroes to a file)

總結:off和metadata模式創建的是sparse稀疏模式磁盤文件。而full和falloc創建的是非稀疏磁盤文件,但full模式需要置零,而falloc不需要置零,所以falloc創建的速度要快很多。

qemu-img create -f qcow2 -o preallocation=off /root/q1.qcow2 size=10M

qemu-img create -f qcow2 -o preallocation=metadata /root/q2.qcow2 size=10M

qemu-img create -f qcow2 -o preallocation=full /root/q3.qcow2 size=10M

qemu-img create -f qcow2 -o preallocation=falloc /root/q4.qcow2 size=10M

 

 qemu-img create -f qcow2 -o preallocation=off /root/q1.qcow2 10M

qemu-img create -f qcow2 -o preallocation=metadata /root/q2.qcow2 10M

qemu-img create -f qcow2 -o preallocation=full /root/q3.qcow2 10M

qemu-img create -f qcow2 -o preallocation=falloc /root/q4.qcow2 10M

 

  3)嘗試創建加密磁盤

 

  出現錯誤提示,應該還需要創建key,留后續填坑

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM