How do I increase the size of my active partition after resizing virtual disk?
Solution 1
The reason you're not seeing the extra disk space is that there are multiple levels involved between the disk (which you've grown) and the usable space in your operating system:
- the disk itself
- partitions on the disk (which allow a single disk to be used for different purposes)
- LVM (Logical Volume Management, which allows disk space to be managed more easily)
- file systems (which manage file storage in a partition or logical volume)
Each of these layers needs to be told about the increase in disk space.
Since you're using LVM, there are two approaches. You can add a new partition and use that, as described in Rui's answer (close enough — that describes adding a new disk, but you can add a new primary partition to use the added space on your existing disk in the same way). You can also, instead, extend your existing partition...
Let's go through the layers:
- The disk itself: you've done that already.
-
Partitions: to extend a partition, you can use a graphical tool such as
gparted
, or plainfdisk
:fdisk /dev/sda
Delete the existing partition, and its container, making a note of their start sectors:
p
(this prints the partition table and gives the information we'll need again later)
d 5 d 2
This won't delete any data, it just removes the markers (and in any case, as long as we don't tell
fdisk
to write, nothing changes on-disk). Create a new extended partition:n e
Accept the default partition number. For the first sector, make sure
fdisk
is using the same start sector as was used previously forsda2
(1001470 in your case); for the last sector,fdisk
will automatically pick the largest possible value (using all the disk space, which is what you want). Create a new logical partition inside:n l
Again, check the start sector (1001472 in your case), and use all the disk space.
w
will write the changes to disk. Because we've changed a partition while it was in use, you'll probably need to reboot at this point.
-
LVM: we need to tell the LV subsystem about the new disk space; we do this by first extending the physical volume which occupies the partition:
pvresize /dev/sda5
then the logical volume
lvresize -r -l 100%PVS /dev/mapper/webserver--vg-root
File systems: this one's easy, the
-r
option tolvresize
above took care of it, using a tool calledfsadm
.
Solution 2
Important note: I advise doing a snapshot of the VM a couple of hours before messing up with partitions. Often snapshots in VMWare go wrong when modifications are made immediately after doing them.
The easiest way to do this is creating a new disk, a new primary partition, and add that to your volume group.
You need to create a new partition with fdisk.
Then to create a new partition in the new disk (or /dev/sda, and partition 3, instead of partition 1 for the old disk - /dev/sda )
sudo fdisk /dev/sdb
n -- new
p -- primary
(accept all defaults with ENTER)
change the type of partition :
t
1
8E -- Linux LVM
to write it:
w
q
(I will assume /dev/sdb1
was created at this point. If other, substitute it in the vgextend
bellow)
Depending on the system it may ask you to reboot at this time. You either reboot, or run:
partprobe
If you do not have the command, install it with:
sudo apt-get install parted
Check what the volume group (VG Name) is with:
vgdisplay
From now on, I assume it is webserver--vg
The you add the partition to the volume group:
vgextend webserver--vg /dev/sdb1
You can then resize it with (real time, better than doing lvextend + resize as in the tutorial in the reference)
lvextend -r -l+100%FREE /dev/vg/webserver--vg-root
P.S. This is an alternative route to @Stephen Kitt, without deleting the partitions. It is flexible enough for growing your disk, the procedure is less complicated, the disadvantage is that over time, the file system management will be more complex when growing the file system. As it is, this procedure is often more oriented to physical machines e.g. growing LVM partitions on a second disk.
References:
Re-read The Partition Table Without Rebooting Linux System
How to Manage and Use LVM (Logical Volume Management)
Related question:
Solution 3
If your disk uses GPT, parted would be a great way to solve this.
Example:
sudo parted
(parted) select /dev/sda
Using /dev/sda
(parted) mkpart
Partition type?
primary File system type? [ext2]?
Start? 1
End? 10000
(parted) quit
cro@mac-mini-Ubuntu $
Related videos on Youtube
RapidScampi
Updated on September 18, 2022Comments
-
RapidScampi almost 2 years
I'm running a web server that has run out of space. It's running as a virtual machine on VMware ESX and I have doubled the size of its hard drive.
Disk /dev/sda: 96 GiB, 103079215104 bytes, 201326592 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: dos Disk identifier: 0x51f6b050 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 999423 997376 487M 83 Linux /dev/sda2 1001470 100661247 99659778 47.5G 5 Extended /dev/sda5 1001472 100661247 99659776 47.5G 8e Linux LVM
It shows that dev/sda is 96GB, which must refer to the physical volume, meaning that the table below has three partitions on it - sda1, sda2 and sda2.
However, if I run DF command, I get the following:
Filesystem 1K-blocks Used Available Use% Mounted on udev 4068132 0 4068132 0% /dev tmpfs 817508 9312 808196 2% /run /dev/mapper/webserver--vg-root 44762192 35857748 6607612 85% / tmpfs 4087524 0 4087524 0% /dev/shm tmpfs 5120 0 5120 0% /run/lock tmpfs 4087524 0 4087524 0% /sys/fs/cgroup /dev/sda1 482922 152390 305598 34% /boot tmpfs 817508 0 817508 0% /run/user/1000
What are these if they're not partitions? It looks as though the problem is /dev/mapper/webserver--vg-root (I just deleted some files so I could use the system). How do I increase its size based on the increase in disk space?
Thanks in advance
-
Rui F Ribeiro over 7 yearsIt is not enough growing partitions, you have to deal with LVM, and the underlying file system.
-
Cro over 7 yearsSorry about that if I'm wrong
-
Rui F Ribeiro over 7 yearsYou are welcome. see this wiki.ubuntu.com/Lvm
-
Stephen Kitt over 7 yearsThanks! It's always a good idea to have a backup anyway when messing around with partitions ;-). Even though this approach can seem risky, the partition table on disk always has the partitions, so it's safe enough.
-
Rui F Ribeiro over 7 yearsI also point out some best practices of vmware involve aligning/creating the first partition +4KB, so I would stress out that need of not accepting defaults, and being very attentive about the first sector of the 1st partition.
-
Stephen Kitt over 7 years@RuiFRibeiro no, because we're trying to enlarge an existing partition. The new partitions must use the same start sectors, otherwise the existing data will no longer be accessible.
-
Stephen Kitt over 7 yearsBut
fdisk
does the right thing on its own (in fact it aligns physical and logical partitions on 1MB boundaries, shifting extended partitions so their logical partitions are correctly aligned). -
Rui F Ribeiro over 7 yearshmmm it must be 4BM then. Will have to check it out....
-
Stephen Kitt over 7 years@RuiFRibeiro my initial comment still applies though — you can't adjust the alignment of existing partitions in this way, they need to be moved.
-
Rui F Ribeiro over 7 yearsYes, of course, I am not putting that in doubt.
-
Stephen Kitt over 7 yearsIndeed, I just wanted to emphasize the importance of this aspect so that the OP doesn't inadvertently lose data.
-
RapidScampi over 7 yearsthanks for the assistance. Following the above I am unable to set the start sector to 100472 for the logical partition contained within the extended partition: First sector (1003518-201326591, default 1003520): I assume for the file system residing on it, it will need to start from the exact same sector?
-
Stephen Kitt over 7 yearsYes, it needs to start on the exact same sector. Where does the extended partition start?
-
RapidScampi over 7 yearsIt won't let me start the sector before 1003518, default of 1003520.