Squeeze VM Image (Space Reclamation)

最後更新: 2019-10-10

目錄

  • vbox - 為 HardDisk image 減肥
  • zerofree
  • sdelete
  • qemu-img
  • virt-sparsify
  • virt-filesystems

 


vbox - 為 HardDisk image 減肥

 

減肥前:

-rw------- 1 root root 1564487680 Jan  1 20:53 squeeze.vdi

減肥中:

usage: vbox modifyhd <image> --compact

i.e.

vbox modifyhd /mnt/vm/debian1/hd/squeeze.vdi --compact

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

減肥後:

-rw------- 1 root root 1336946688 Jan  1 20:54 squeeze.vdi

P.S.

如果想得到更好的減肥效果, 那就要在 VM 上行 zerofree, 然後再 compact 它

(在 Linux 上用 zerofree, Window 上用 sdelete -z 去清 0)

 * 過程係會有 I/O write 的, 真是會寫入東西, Image 檔會到它的最大 size !!

 


zerofree (ext2/3)

 

zerofree: zero free blocks from ext2/3/4 file-systems

finds the unallocated,  blocks with non-zero value content

zerofree 前一定umount / readonly device 先 !!

mount -o remount,ro /home

zerofree -v /dev/sda6

  • -v        Be verbose
  • -n        Perform a dry run

# number of blocks modified by zerofree / number of free blocks /  total number of blocks on the filesystem

19432/205823/213504

modified by zerofree <- 第2次行時會變成 0

free blocks & total number of blocks <- tune2fs -l /dev/sdXn # Free blocks  / Block count

次後再 compact image file 效果會相當明顯 !!

-rw------- 1 root root 1056976896 Jan  1 21:11 squeeze.vdi

Remark

如果 FS 沒有類似 zerofree 等軟件, 那可以用 dd 去建立一個全是 zero 的 dummy file, 之後再刪除它

此方法缺點: 1. disk image grow to its maximal extent 2. uses all free space on the disk

dd if=/dev/zero of=dummy.file bs=100m count=N

rm -f /dummy

 


sdelete

 

Window: sdelete

工具: download

sdelete -z C: 

opts:

-z   Zero free space

Resize:

usage: vbox modifyhd <image> --resize <?? MB>

Example:

vbox modifyhd /mnt/vm/winxp/hd/winxp.sp3-ide8g.idv --resize 15000

Remark:

以上指令只適用於 VDI  format 的 HD imgage

 


qemu-img

 

 * be sure to shutdown the VM first

方法1: virt-sparsify

link

方法2: Noop conversion (qcow2-to-qcow2) removes sparse space:

qemu-img convert -p -O qcow2 orig_disk.qcow2 new_disk.qcow2

-p  display progress bar

# Shrink your disk with compression

# -c  indicates that target image must be compressed

# smaller disk size, takes longer to shrink, performance impact on slower systems

qemu-img convert -p -O qcow2 -c orig_disk.qcow2 new_disk.qcow2

 


virt-sparsify

 

sparsify = 稀疏化

# Install

apt-get install libguestfs-tools

# Get Version

virt-sparsify -V

virt-sparsify 1.34.6

# Query the capabilities of the virt-sparsify

virt-sparsify --machine-readable      # 要等一段時間才有 output

virt-sparsify
linux-swap
zero
check-tmpdir
in-place
tmp-option
ntfs
btrfs

Usage

# The format of the input disk is detected and the same format is used for the output disk.

# Virt-sparsify tries to zero and sparsify free space on filesystems (eg. ext2/3/4, btrfs, NTFS, etc.)

virt-sparsify indisk outdisk

# --ignore "filesystem" = don't zero free space on "filesystem"

i.e.

# "virt-filesystems" to get a list of filesystems within a disk image

virt-sparsify --ignore /dev/sda1 indisk outdisk

# convert (outdisk: raw, qcow2)

virt-sparsify indisk.raw --convert qcow2 outdisk.qcow2

# compress: This only works if the output format is qcow2

virt-sparsify --compress indisk.qcow2 outdisk.qcow2

# Pass Opts to qemu-img ("-o option[,option,...]")

-o cluster_size=512,preallocation=metadata

Example

virt-sparsify -o preallocation=metadata win7.qcow2 win7-new.qcow2

Output

[   0.2] Create overlay file in /tmp to protect source disk
[   0.2] Examine source disk
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
[   7.0] Fill free space in /dev/sda1 with zero
[   7.8] Fill free space in /dev/sda2 with zero
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[ 327.2] Copy to destination and make sparse
[1202.0] Sparsify operation completed with no errors.
virt-sparsify: Before deleting the old disk, carefully check that the
target disk boots and works correctly.

"ps" 會見有以下 proccess 在執行

[Step1] Fill free space in /dev/sda? with zero

/usr/bin/qemu-system-x86_64
...
-kernel /var/tmp/.guestfs-0/appliance.d/kernel
-initrd /var/tmp/.guestfs-0/appliance.d/initrd
...

[Step2] Copy to destination and make sparse

qemu-img convert -f

 


virt-filesystems

 

filesystems (eg. ext2/3/4, btrfs, NTFS, etc.), and also in LVM physical volumes.

# Show fs

# -a or -d options to specify the disk image or libvirt guest respectively (a=? , d=domname)

virt-filesystems -a centos7.qcow2    # 要等一段時間 ...

/dev/sda1
/dev/centos/root

# extra information

# -l or --long

virt-filesystems -a win7.qcow2 -l

Name       Type        VFS   Label            Size         Parent
/dev/sda1  filesystem  ntfs  System Reserved  104857600    -
/dev/sda2  filesystem  ntfs  OS               42842718208  -

# Show partitions (partitions are shown instead of filesystems)

virt-filesystems -a centos7.qcow2 --partitions

/dev/sda1
/dev/sda2

 


 

 

 

Creative Commons license icon Creative Commons license icon