差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
linux:qemu-kvm:qemu-img [2021/12/09 14:06] – [仮想ディスクイメージの圧縮] ともやんlinux:qemu-kvm:qemu-img [2024/07/07 16:28] (現在) – [参考文献] ともやん
行 3: 行 3:
 ===== 仮想ディスクの作成 ==== ===== 仮想ディスクの作成 ====
 <WRAP color_term> <WRAP color_term>
-<WRAP color_command><code+<WRAP color_command><html><pre
-$ sudo qemu-img create -f qcow2 filename.qcow2 256G +<b class=GRN>$</b> <b class=HIY>sudo</b> qemu-img create <b class=HIK>-f</b> qcow2 filename.qcow2 256G 
-</code></WRAP>+</pre></html></WRAP>
 <WRAP color_result><code> <WRAP color_result><code>
 Formatting 'filename.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=274877906944 lazy_refcounts=off refcount_bits=16 Formatting 'filename.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=274877906944 lazy_refcounts=off refcount_bits=16
行 13: 行 13:
 ===== 仮想ディスクの容量表示 ===== ===== 仮想ディスクの容量表示 =====
 <WRAP color_term> <WRAP color_term>
-<WRAP color_command><code+<WRAP color_command><html><pre
-$ sudo qemu-img info filename.qcow2 +<b class=GRN>$</b> <b class=HIY>sudo</b> qemu-img info filename.qcow2 
-</code></WRAP>+</pre></html></WRAP>
 <WRAP color_result><code> <WRAP color_result><code>
 image: filename.qcow2 image: filename.qcow2
行 33: 行 33:
  
 ===== 仮想ディスクの拡張 ===== ===== 仮想ディスクの拡張 =====
-<code+<WRAP color_term
-$ sudo qemu-img resize filename.qcow2 +256G +<WRAP color_command><html><pre> 
-</code+<b class=GRN>$</b> <b class=HIY>sudo</b> qemu-img resize filename.qcow2 +256G 
-<code>+</pre></html></WRAP
 +<WRAP color_result><code>
 Image resized. Image resized.
-</code>+</code></WRAP> 
 +</WRAP> 
 仮想ディスクの容量確認 仮想ディスクの容量確認
-<code+<WRAP color_term
-$ sudo qemu-img info filename.qcow2 +<WRAP color_command><html><pre> 
-</code+<b class=GRN>$</b> <b class=HIY>sudo</b> qemu-img info filename.qcow2 
-<code>+</pre></html></WRAP
 +<WRAP color_result><code>
 image: filename.qcow2 image: filename.qcow2
 file format: qcow2 file format: qcow2
行 54: 行 58:
     refcount bits: 16     refcount bits: 16
     corrupt: false     corrupt: false
-</code>+</code></WRAP> 
 +</WRAP>
  
 ===== 仮想ディスクイメージの圧縮 ===== ===== 仮想ディスクイメージの圧縮 =====
 仮想ディスクイメージの圧縮 (Shrink) とは、仮想ディスクイメージ内の使われなくなった領域を開放し、イメージサイズをコンパクトにして物理ディスク領域も開放する。\\ 仮想ディスクイメージの圧縮 (Shrink) とは、仮想ディスクイメージ内の使われなくなった領域を開放し、イメージサイズをコンパクトにして物理ディスク領域も開放する。\\
 <WRAP color_term> <WRAP color_term>
-<WRAP color_command><code+<WRAP color_command><html><pre
-ll +<b class=GRN>#</b> <b class=HIY>ls</b> <b class=HIK>-al</b> 
-</code></WRAP>+</pre></html></WRAP>
 <WRAP color_result><code> <WRAP color_result><code>
 合計 30426336 合計 30426336
 -rw-------. 1 root root 274920112128 12月  2 16:18 Windows_10_Pro_x64.qcow2 -rw-------. 1 root root 274920112128 12月  2 16:18 Windows_10_Pro_x64.qcow2
 </code></WRAP> </code></WRAP>
-</WRAP> 
  
-<WRAP color_term> +<WRAP color_command><html><pre
-<WRAP color_command><code+<b class=GRN>#</b> <b class=HIY>qemu-img</b> info Windows_10_Pro_x64.qcow2 
-# qemu-img info Windows_10_Pro_x64.qcow2 +</pre></html></WRAP>
-</code></WRAP>+
 <WRAP color_result><code> <WRAP color_result><code>
 image: Windows_10_Pro_x64.qcow2 image: Windows_10_Pro_x64.qcow2
行 92: 行 95:
 [[wpjp>スパースファイル|スパースファイル - Wikipedia]]\\ [[wpjp>スパースファイル|スパースファイル - Wikipedia]]\\
 <WRAP color_term> <WRAP color_term>
-<WRAP color_command><code+<WRAP color_command><html><pre
-# pwd +<b class=GRN>#</b> <b class=HIY>pwd</b> 
-</code></WRAP>+</pre></html></WRAP>
 <WRAP color_result><code> <WRAP color_result><code>
 /var/lib/libvirt/images /var/lib/libvirt/images
 </code></WRAP> </code></WRAP>
-<WRAP color_command><code+<WRAP color_command><html><pre
-# ls -l --block-size=G +<b class=GRN>#</b> <b class=HIY>ls</b> <b class=HIK>-l --block-size=g</b> 
-</code></WRAP>+</pre></html></WRAP>
 <WRAP color_result><code> <WRAP color_result><code>
 合計 13G 合計 13G
行 110: 行 113:
  
 <WRAP color_term> <WRAP color_term>
-<WRAP color_command><code+<WRAP color_command><html><pre
-# df -BG +<b class=GRN>#</b> <b class=HIY>df</b> <b class=HIK>-BG</b> 
-</code></WRAP>+</pre></html></WRAP>
 <WRAP color_result><code> <WRAP color_result><code>
 ファイルシス   1G-ブロック  使用 使用可 使用% マウント位置 ファイルシス   1G-ブロック  使用 使用可 使用% マウント位置
行 127: 行 130:
 </WRAP> </WRAP>
  
-==== 圧縮方法 ==== 
 <WRAP color_term> <WRAP color_term>
-<WRAP color_command><code+<WRAP color_command><html><pre
-qemu-img convert -O qcow2 Windows_10_Pro_x64.qcow2 Windows_10_Pro_x64.qcow2_shrink+<b class=GRN>#</b> <b class=HIY>find</b> . <b class=HIK>-type</b> f <b class=HIK>-printf</b> "%S\t%p\n" | <b class=HIY>gawk</b> '$1 &lt; 1.0 {print}' 
 +</pre></html></WRAP> 
 +<WRAP color_result><code> 
 +0.0416002 ./Windows_10_Pro_x64.qcow2 
 +0.00872493 ./fedora35_minimal.qcow2
 </code></WRAP> </code></WRAP>
-<WRAP color_command><code+<WRAP color_command><html><pre
-ll+<b class=GRN>#</b> <b class=HIY>qemu-img</b> info filename.qcow2  
 +</pre></html></WRAP> 
 +<WRAP color_result><code> 
 +image: filename.qcow2 
 +file format: qcow2 
 +virtual size: 256 GiB (274877906944 bytes) 
 +disk size: 196 KiB 
 +cluster_size: 65536 
 +Format specific information: 
 +    compat: 1.1 
 +    compression type: zlib 
 +    lazy refcounts: false 
 +    refcount bits: 16 
 +    corrupt: false 
 +    extended l2: false
 </code></WRAP> </code></WRAP>
 +</WRAP>
 +
 +==== 圧縮方法 ====
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>#</b> <b class=HIY>qemu-img</b> convert -O qcow2 Windows_10_Pro_x64.qcow2 Windows_10_Pro_x64.qcow2_shrink
 +<b class=GRN>#</b> <b class=HIY>ls</b> <b class=HIK>-al</b>
 +</pre></html></WRAP>
 <WRAP color_result><code> <WRAP color_result><code>
 合計 30426336 合計 30426336
行 143: 行 171:
  
 <WRAP color_term> <WRAP color_term>
-<WRAP color_command><code+<WRAP color_command><html><pre
-# qemu-img convert -O qcow2 -c Windows_10_Pro_x64.qcow2 Windows_10_Pro_x64.qcow2_shrink-c +<b class=GRN>#</b> <b class=HIY>qemu-img</b> convert <b class=HIK>-O</b> qcow2 <b class=HIK>-c</b> Windows_10_Pro_x64.qcow2 Windows_10_Pro_x64.qcow2_shrink-c 
-</code></WRAP> +</pre></html></WRAP> 
-<WRAP color_command><code+<WRAP color_command><html><pre
-ll +<b class=GRN>#</b> <b class=HIY>ls</b> <b class=HIK>-al</b> 
-</code></WRAP>+</pre></html></WRAP>
 <WRAP color_result><code> <WRAP color_result><code>
 合計 30426336 合計 30426336
行 161: 行 189:
 ※ qcow2 形式は必要に応じて膨らむので、実際に使われている容量のファイルができあがる。\\ ※ qcow2 形式は必要に応じて膨らむので、実際に使われている容量のファイルができあがる。\\
 <WRAP color_term> <WRAP color_term>
-<WRAP color_command><code+<WRAP color_command><html><pre
-$ sudo qemu-img convert -O qcow2 filename.vmdk filename.qcow2 +<b class=GRN>$</b> <b class=HIY>sudo</b> qemu-img convert <b class=HIK>-O</b> qcow2 filename.vmdk filename.qcow2 
-</code></WRAP>+</pre></html></WRAP>
 </WRAP> </WRAP>
  
行 169: 行 197:
 ※ vmdk で定義されているディスク容量(使われていない領域を含む)の raw イメージができあがる。\\ ※ vmdk で定義されているディスク容量(使われていない領域を含む)の raw イメージができあがる。\\
 <WRAP color_term> <WRAP color_term>
-<WRAP color_command><code+<WRAP color_command><html><pre
-$ sudo qemu-img convert -O raw filename.vmdk filename.img +<b class=GRN>$</b> <b class=HIY>sudo</b> qemu-img convert <b class=HIK>-O</b> raw filename.vmdk filename.img 
-</code></WRAP>+</pre></html></WRAP> 
 +</WRAP> 
 + 
 +===== Linux で qcow2 ディスクイメージをマウントする方法 ===== 
 +参考: [[https://gist.github.com/shamil/62935d9b456a6f9877b5|How to mount a qcow2 disk image]]\\ 
 + 
 +ホスト上で NBD を有効にして、QCOW2 を NBD として接続する🤔\\ 
 +<WRAP color_term> 
 +<WRAP color_command><html><pre> 
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">modprobe</font> nbd max_part=8 
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">qemu-nbd</font> <font color="#A347BA">--connect=/dev/nbd0</font> <u style="text-decoration-style:solid">/var/lib/libvirt/images/Windows_10_Pro_x64.qcow2</u> 
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">fdisk</font> <u style="text-decoration-style:solid">/dev/nbd0</u> <font color="#A347BA">-l</font> 
 +</pre></html></WRAP> 
 +<WRAP color_result><html><pre> 
 +<b>Disk /dev/nbd0: 256 GiB, 274877906944 bytes, 536870912 sectors</b> 
 +Units: sectors of 1 * 512 = 512 bytes 
 +Sector size (logical/physical): 512 bytes / 512 bytes 
 +I/O size (minimum/optimal): 512 bytes / 512 bytes 
 +Disklabel type: dos 
 +Disk identifier: 0x4ac41c2f 
 + 
 +<b>Device     </b> <b>Boot</b> <b> Start</b> <b>      End</b> <b>  Sectors</b> <b>  Size</b> <b>Id</b> <b>Type</b> 
 +/dev/nbd0p1 *      2048    206847    204800   100M  7 HPFS/NTFS/exFAT 
 +/dev/nbd0p2      206848 536868863 536662016 255.9G  7 HPFS/NTFS/exFAT 
 +</pre></html></WRAP> 
 +</WRAP> 
 + 
 +NBD を切断する🤔\\ 
 +<WRAP color_term> 
 +<WRAP color_command><html><pre> 
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">qemu-nbd</font> <font color="#A347BA">--disconnect</font> <u style="text-decoration-style:solid">/dev/nbd0</u> 
 +</pre></html></WRAP> 
 +<WRAP color_result><html><pre> 
 +/dev/nbd0 disconnected 
 +</pre></html></WRAP> 
 +<WRAP color_command><html><pre> 
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">rmmod</font> nbd 
 +</pre></html></WRAP>
 </WRAP> </WRAP>
  
行 177: 行 242:
 [[https://wiki.archlinux.jp/index.php/%E3%82%B9%E3%83%91%E3%83%BC%E3%82%B9%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB|スパースファイル - ArchWiki]]\\ [[https://wiki.archlinux.jp/index.php/%E3%82%B9%E3%83%91%E3%83%BC%E3%82%B9%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB|スパースファイル - ArchWiki]]\\
 [[https://pve.proxmox.com/wiki/Shrink_Qcow2_Disk_Files|Shrink Qcow2 Disk Files - Proxmox VE]]\\ [[https://pve.proxmox.com/wiki/Shrink_Qcow2_Disk_Files|Shrink Qcow2 Disk Files - Proxmox VE]]\\
 +[[https://unix.stackexchange.com/questions/612888/tell-if-a-file-is-a-sparse-file|There is an existing question for &quot;Finding all sparse files&quot;, and I think I could use it, but the scripts were too complex to understand. I don't think the GUI of Ubuntu file manager prov...]]\\
 +[[https://superuser.com/questions/72152/windows-7-fails-to-install-on-kvm-with-qemu|ubuntu - Windows 7 fails to install on KVM with qemu - Super User]]\\
 +[[https://askubuntu.com/questions/1310440/using-virtio-win-drivers-with-win7-sp1-x64|virtualization - Using virtio-win drivers with Win7 SP1 x64 - Ask Ubuntu]]\\
  
 +==== 付録 ====
 +[[tw>tomoyan596/status/1465803218206617605|KVMの中で飼いならします🤤]]\\
 +[[tw>tomoyan596sp/status/1697355608306209081|スパースファイルの整理はこんなところにしてたのかぁ🤔記憶にはあるけど...どこに行ったかわからなくなってたり...😅]]\\
  • linux/qemu-kvm/qemu-img.1639026383.txt.gz
  • 最終更新: 2021/12/09 14:06
  • by ともやん