shrink qcow2 ext4 filesystem

I think these were the steps I took. This shit took forever

Guestfish the image

1
guestfish -rw -a myimg.qcow

start the guestfish vm an env

1
><fs> run

Inspect current partitions. These are listed in bytes

1
2
3
4
5
6
7
><fs> part-list /dev/sda
[0] = {
part_num: 1
part_start: 1048576
part_end: 13421510655
part_size: 13420462080
}

Del the partition you’re shrinking

1
><fs> part-del /dev/sda 1

Add it back at your preferred size. Note that part-add is in sectors

1
><fs> part-add /dev/sda p 2048 26213887

The partition will still be the same size in your list until you resize

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
><fs> part-list /dev/sda
[0] = {
part_num: 1
part_start: 1048576
part_end: 13421510655
part_size: 13420462080
}
><fs> resize2fs /dev/sda1
><fs> part-list /dev/sda
[0] = {
part_num: 1
part_start: 1048576
part_end: 13421510655
part_size: 13420462080
}
><fs> quit

Now convert to raw, create a new container which is larger than your new desired size

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
root()@8798.blake_test_playground [glance_test]# qemu-img info blake_test.full
image: blake_test.full
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 7.3G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
root(bcanderson)@8798.blake_test_playground [glance_test]# qemu-img convert -p -O raw blake_test.full blake_test.full.raw
(100.00/100%)
root()@8798.blake_test_playground [glance_test]# qemu-img create -f qcow2 shrunk.qcow2 13G
root(bcanderson)@8798.blake_test_playground [glance_test]# virt-resize --output-format qcow2 blake_test.full.raw shrunk.qcow2
root(bcanderson)@glance3 [glance_test]# qemu-img info shrunk.qcow2
image: shrunk.qcow2
file format: qcow2
virtual size: 13G (13958643712 bytes)
disk size: 7.7G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false