LoginSignup
0
0

More than 5 years have passed since last update.

qcow2のshrink

Last updated at Posted at 2018-12-20

やりたいこと、40GBから20GBにへらす
シャットダウン前提です。オンラインではできない。

1. 縮小予定のサイズで新しいディスクを作る

qemu-img create -f qcow2 -o preallocation=falloc atarasi-vol.qcow2 20G

2. 作業用のVMに、元のボリュームと 1. で作ったボリュームの両方をアタッチする

  • vda: 作業用のOS
  • vdb: 元のボリューム
  • vdc: 1で作ったボリューム
<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2'/>
  <source file='/var/lib/libvirt/images/furui-vol.qcow2'/>
  <target dev='vdb' bus='virtio'/>
</disk>
<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2'/>
  <source file='/var/lib/libvirt/images/atarasi-vol.qcow2'/>
  <target dev='vdc' bus='virtio'/>
</disk>

3. 作業用のVMを起動

virsh start work-os

4. vdb(元のボリューム)のファイルシステムを縮小する

e2fsck -f /dev/vdb1   (事前にやらないといけない)
resize2fs /dev/vdb1 15G (少し余裕を持たせるために、20GBより小さくする)

5. コピー先にパーティションを作る

fdisk /dev/vdc1

パーティションの開始セクタなどはコピー元とあわせる

6. コピーする

dd if=/dev/vdb1 of=/dev/vdc1 bs=1M

7. MBRをコピーする

dd if=/dev/vdb of=/dev/vdc bs=446 count=1

8. grubを再インストールする

mount /dev/vdc1 /mnt
cd /mnt

mount --bind /dev dev
mount --bind /proc proc
mount --bind /sys sys

chroot .

grub2-install /dev/vdc

9. 元のVMに新しいボリュームをアタッチして起動

<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2'/>
  <source file='/var/lib/libvirt/images/atarasi-vol.qcow2'/>
  <target dev='vda' bus='virtio'/>
</disk>

ここで起動しなかったら、何かコピーで失敗している

10. ログインして、ファイルシステムを最大まで拡張する

resize2fs /dev/vda1

総括

もっといい手順があれば知りたい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0