How to Resize Ubuntu Root Partition
I used to be able to do these tasks with my eyes closed with AIX and other version of Linux and Unix. Today my brain blue screened on me while trying to expand my Ubuntu machine’s root volume group, logical volume, and filesystem. Wow it’s been awhile.
So instead of racking my brain in the future I will jot down the process here for anyone else stuck in the same situation.
How to expand Ubuntu root filesystem
So a quick recap for those that don’t remember. We have to expand components of the filesystem in the following order. Physical Volume -> Volume Group -> Logical Volume -> Filesystem
In my scenario I increased the size of my Virtual Machine disk in VMware from 18GB to 28 GB.
- Step 1 Before we get started run pvdisplay in order to see the current state of the physical volume
root@Docker01:~# pvdisplay --- Physical volume --- PV Name /dev/sda1 VG Name ubuvg PV Size 16.00 GiB / not usable 2.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 4095 Free PE 678 Allocated PE 3417 PV UUID HiNAY2-sfqz-7fwT-tLvy-XKfT-3QR6-BXash5
root@Docker01:~# fdisk /dev/sdb n (create new partition, select start and end cylinders, all free space is selected by default) Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): Using default response p Partition number (1-4, default 2): Using default value 2 First sector (33552384-33554431, default 33552384): Using default value 33552384 Last sector, +sectors or +size (33552384-33554431, default 33554431): Using default value 33554431 w (save partition table and exit)
Accept the default values that are provide that the «n» option returns.
- Step 3 Reboot your Ubuntu machine in order for Ubuntu to recognize the change.
root@Docker01:~# reboot - Step 4 Update the partition table tool using cfdisk.
root@Docker01:~# cfdisk
Select [NEW] partition and [PRIMARY] for partition type. Select how big you want the partition and enter.
Once finished select [WRITE] to save your changes to your partition then [QUIT].
You’ve now created a new partition most likely /dev/sda1 - Step 5 Intialize the newly created partition with pvcreate
root@Docker01:~# pvcreate /dev/sda3 Physical volume "/dev/sda3" successfully created
- Step 6 Add the newly created partition to the root volume group. First rerun pvdisplay to show the new physical volume details. You should now see the new partition
"/dev/sda3" is a new physical volume of "9.00 GiB" --- NEW Physical volume --- PV Name /dev/sda3 VG Name PV Size 9.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID OEADew-tamR-EHil-FU4p-kIuW-F5rT-V0CTcY
Run the command vgs in order to get the name of your volume group
root@Docker01:~# vgs VG #PV #LV #SN Attr VSize VFree ubuvg 1 5 0 wz--n- 16.00g 2.65g
Expand the volume group with vgextend then verify your volume group again once complete with vgs
root@Docker01:~# vgextend ubuvg /dev/sda3 Volume group "ubuvg" successfully extended root@Docker01:~# vgs VG #PV #LV #SN Attr VSize VFree ubuvg 2 5 0 wz--n- 24.99g 11.64g root@Docker01:~#
root@Docker01:~# df -k Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/ubuvg-ubulv 8726456 7739868 520260 94% / root@Docker01:~# lvextend -L +10G /dev/mapper/ubuvg-ubulv Extending logical volume ubulv to 18.58 GiB Logical volume ubulv successfully resized
root@Docker01:~# resize2fs /dev/mapper/ubuvg-ubulv resize2fs 1.42.9 (4-Feb-2014) Filesystem at /dev/mapper/ubuvg-ubulv is mounted on /; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/mapper/ubuvg-ubulv is now 4871168 blocks long. root@Docker01:~# df -k Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/ubuvg-ubulv 19048316 7744220 10420152 43% /
Ubuntu Root Resize in Review
Hopefully this tutorial will save you time and give you a little more background on the flow of a resize. Important to remember is the path of the resize Physical Volume -> Volume Group -> Logical Volume -> Filesystem. We must follow this flow or the resize will either fail or encounter problems. If you use the Ubuntu GUI some tools contain the entire process in a single tool.
I’m a CLI junkie so no plans of me finding out how a GUI works anytime soon.
Follow me
If you liked this article be sure to Follow Me on Twitter to stay updated!
4’000 Stars and counting, a trip down memory lane
A trip down memory lane & the history of Docker, Compose and monitoring w/ Docker & Prometheus. Along the way, I discovered an amazing community.
2022 Retrospective 2022 and Goals/Predictions for 2023
There is no beating around the bush about 2022; it was damn hard! 2022 challenged me professionally, physically, and organizationally. But, at the same time, we had some great highlights to celebrate the small wins. 2022 will go down in history as unique that we finally came out of the
Apple Freeform vs Miro
A review of the collaboration canvas tools Apple Freeform and Miro. Both tools allow for brainstorming with virtual post-it notes with others to structure ideas visually. This article compares Apple Freeform with Miro.
How to extend my root (/) partition?
Till today I was using ubuntu 13.04 in my windows7/ubuntu dual boot. Just now I have upgraded to ubuntu 14.04. My software updater showed an updates available message. I proceeded with Install Now . Soon I got the following window. Then I tried sudo apt-get clean as recommended. Nothing happened! Not even a window opened. Teach me deleting the temporary packages. Deleting temporary files may not be highly helpful in case of further updates and installs for I don’t have too much space left in my /. I wanna extend my / partition with the help of the unallocated space in my HD. The following is my Gparted window. I got an answer here. But it wasn’t useful for me. I had two choices. But the answer wasn’t working for both my chioces. Choice 1: I may use the 4.25 GiB unallocated space to extend my /dev/sda10. To do so, I must need to move my linux-swap partition. But my Resize/Move option is disabled for the linux-swap partition. Choice 1 failed! Choice 2: I may use the 14.35 GiB unallocated space instead. In this case I must move more than one partition. /dev/sda4 partition is the first on the list. Even that partition has the same problem. Resize/Move option is disabled. Choice 2 failed! I am out of choices now. What shall I do? Help me to extend my / with Gparted.
Why does it says 1.96 GiB unused in your root? You actually have space, no? What’s the output of df ?
apt-get clean doesn’t pop-up a window or even echo to the command line what it did. It just does it, but the now freed 1.96 gb space shows it did something.
3 Answers 3
You actually can use the 4.25 GiB at the end without any reboot, because Linux supports on-line partition resizing since kernel 2.6 (meaning you can resize partitions while they’re mounted, even the root partition, without any reboot).
I’ll show you how to remove the swap partition and extend the root partition with all the free space at the end. Then I’ll use the unallocated space in the beginning (14.35 GiB) for a new swap partition. Of course 14.35 GiB is a bit much so you can also choose to use some to extend your NTFS partition.
- Open GParted
- Right click on /dev/sda11 and select Swapoff
- Right click on /dev/sda11 and select Delete
- Click on Apply All Operations
- Open a terminal
- Extend the root partition:
Initially I wrote this using the command line only, but then I decided to try to use GParted as much as possible as this may be easier for you to see what’s going on. You may even extend the root partition using GParted. But you need to unmount your root partition before you work on it. I mean you should work offline (using a liveUSB). In some cases your grub may get damaged. So do reinstall your grub before you leave out from your offline working.
I believe GParted will automatically enable the new swap partition but I’m not sure. You can always check by right clicking on it and then selecting Swapon . If this option is not available the swap partitions is probably already enabled.
Also the Refresh Devices step is probably unnecessary, but you should follow it just to be sure.
We cannot resize the extended partition ( sda4 , in this case) as it has a mounted logical partition within it. So we need to boot into the live mode of an Ubuntu USB/CD drive. From there, start GParted, turn off swap if necessary, right click on the extended partition and resize it to suit your needs. Then, follow the steps mentioned as needed to resize root and add back swap. In the event that your root partition UUID gets changed, you can update Grub by executing these steps in the live mode:
sudo mount /dev/sda10 /mnt sudo mount -o bind /dev/ /mnt/dev sudo grub-install --root-directory=/mnt /dev/sda sudo chroot /mnt /bin/bash # Now within the chroot sudo update-grub exit # Now outside chroot
The first couple of commands mount the root partition ( sda10 , in this case) and set up /dev/ for use within a chroot environment. (Other partitions that can be similarly mounted for a more functional chroot are /proc , /sys and /dev/pts .) Then we reinstall grub (not really necessary). Then, we enter the chroot and update grub. This causes grub to use the new UUIDs where necessary. After exiting the chroot, reboot and we should get the grub menu back.