# Extend linux VM filesystem ###### tags: `linux`, `resize2fs` I have bunch of virtual machines nearly run out of root partition. I have to extend size of the root partition in order to install more packages. The partitions in the VM are slightly simple. ``` Device Start End Sectors Size Type /dev/vda1 2048 4095 2048 1M BIOS boot /dev/vda2 4096 33552383 33548288 16G Linux filesystem ``` This is example to double size of the /dev/vda2 partition. #### steps 1. shutdown VM if VM is running 2. resize virtual image file of VM ``` $ ls -lh node5.qcow2 -rw------- 1 root root 17G Mar 27 17:49 node5.qcow2 $ qemu-img info node5.qcow2 image: node5.qcow2 file format: qcow2 virtual size: 16 GiB (17179869184 bytes) disk size: 16 GiB cluster_size: 65536 Format specific information: compat: 1.1 compression type: zlib lazy refcounts: true refcount bits: 16 corrupt: false extended l2: false # add additional 16GB $ qemu-img resize node5.qcow2 +16G Image resized. $ qemu-img info node5.qcow2 image: node5.qcow2 file format: qcow2 virtual size: 32 GiB (34359738368 bytes) disk size: 16 GiB cluster_size: 65536 Format specific information: compat: 1.1 compression type: zlib lazy refcounts: true refcount bits: 16 corrupt: false extended l2: false ``` 4. poweron and login to the VM ``` # check disk size root@ubuntu:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 55.5M 1 loop /snap/core18/2284 loop1 7:1 0 55.5M 1 loop /snap/core18/2344 loop2 7:2 0 61.9M 1 loop /snap/core20/1328 loop3 7:3 0 67.2M 1 loop /snap/lxd/21835 loop4 7:4 0 61.9M 1 loop /snap/core20/1376 loop5 7:5 0 67.9M 1 loop /snap/lxd/22526 loop6 7:6 0 43.6M 1 loop /snap/snapd/15177 loop7 7:7 0 43.6M 1 loop /snap/snapd/14978 vda 252:0 0 32G 0 disk ├─vda1 252:1 0 1M 0 part └─vda2 252:2 0 16G 0 part / ``` 5. update partition ``` # enter fdisk tool prompt $ fdisk /dev/vda Welcome to fdisk (util-linux 2.34). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. GPT PMBR size mismatch (33554431 != 67108863) will be corrected by write. The backup GPT table is not on the end of the device. This problem will be corrected by write. ``` ``` # list partitions Command (m for help): p Disk /dev/vda: 32 GiB, 34359738368 bytes, 67108864 sectors 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: gpt Disk identifier: 029F65E0-4F19-4249-A6C1-3D3D3B82F0CF Device Start End Sectors Size Type /dev/vda1 2048 4095 2048 1M BIOS boot /dev/vda2 4096 33552383 33548288 16G Linux filesystem ``` ``` # delete the /dev/vda2 partition Command (m for help): d Partition number (1,2, default 2): 2 Partition 2 has been deleted. ``` ``` # create new partition which extend all avaialble sector Command (m for help): n Partition number (2-128, default 2): 2 First sector (4096-67108830, default 4096): Last sector, +/-sectors or +/-size{K,M,G,T,P} (4096-67108830, default 67108830): Created a new partition 2 of type 'Linux filesystem' and of size 32 GiB. Partition #2 contains a ext4 signature. Do you want to remove the signature? [Y]es/[N]o: N ``` ``` # write change to partition table Command (m for help): p Disk /dev/vda: 32 GiB, 34359738368 bytes, 67108864 sectors 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: gpt Disk identifier: 029F65E0-4F19-4249-A6C1-3D3D3B82F0CF Device Start End Sectors Size Type /dev/vda1 2048 4095 2048 1M BIOS boot /dev/vda2 4096 67108830 67104735 32G Linux filesystem Command (m for help): w The partition table has been altered. Syncing disks. ``` 7. resize the filesystem to fill the partition ``` $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 55.5M 1 loop /snap/core18/2284 loop1 7:1 0 55.5M 1 loop /snap/core18/2344 loop2 7:2 0 44.7M 1 loop /snap/snapd/15534 loop4 7:4 0 61.9M 1 loop /snap/core20/1376 loop5 7:5 0 67.9M 1 loop /snap/lxd/22526 loop6 7:6 0 43.6M 1 loop /snap/snapd/15177 loop7 7:7 0 67.8M 1 loop /snap/lxd/22753 loop8 7:8 0 61.9M 1 loop /snap/core20/1405 vda 252:0 0 32G 0 disk ├─vda1 252:1 0 1M 0 part └─vda2 252:2 0 32G 0 part / $ df -h / Filesystem Size Used Avail Use% Mounted on /dev/vda2 16G 6.0G 9.0G 40% / $ resize2fs /dev/vda2 resize2fs 1.45.5 (07-Jan-2020) Filesystem at /dev/vda2 is mounted on /; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 4 The filesystem on /dev/vda2 is now 8388091 (4k) blocks long. $ df -h / Filesystem Size Used Avail Use% Mounted on /dev/vda2 32G 6.0G 25G 20% / ``` #### Todo - use "virt-resize" #### use resize2fs For example, a data disk resized by "qemu-img resize" to 300GB from 200GB. Next, tell file system to use all space present in the disk ``` $ e2fsck -f /dev/vdb e2fsck 1.45.5 (07-Jan-2020) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/vdb: 94899/13107200 files (7.0% non-contiguous), 37776757/52428800 blocks ``` ``` $ resize2fs /dev/vdb resize2fs 1.45.5 (07-Jan-2020) Resizing the filesystem on /dev/vdb to 78643200 (4k) blocks. The filesystem on /dev/vdb is now 78643200 (4k) blocks long. ``` #### reference ``` https://blog.programster.org/expand-qcow2-root-filesystem https://maunium.net/blog/resizing-qcow2-images/ https://www.cyberithub.com/resize-qcow2-image-with-virt-resize-kvm-tools/#Step_5_Resize_Original_qcow2_using_virt-resize_command https://www.techgeekbuzz.com/blog/how-to-extend-linux-partitions-with-resize2fs/ ```