2

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

2

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:

  1. The disk itself: you've done that already.
  2. Partitions: to extend a partition, you can use a graphical tool such as gparted, or plain fdisk:

    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 for sda2 (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.

  3. 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
    
  4. File systems: this one's easy, the -r option to lvresize above took care of it, using a tool called fsadm.

  • 1
    +1 This one is indeed simpler, I am usually a bit nervous about dropping all partitions. As the OP stated it is vmware, I would suggest doing a snapshot of the VM a couple of hours before doing this procedure, lest something go wrong. – Rui F Ribeiro Mar 2 '17 at 9:33
  • Thanks! 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. – Stephen Kitt Mar 2 '17 at 9:37
  • I 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. – Rui F Ribeiro Mar 2 '17 at 10:56
  • @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 Mar 2 '17 at 10:58
  • But 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). – Stephen Kitt Mar 2 '17 at 11:00
1

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 $
  • It is not enough growing partitions, you have to deal with LVM, and the underlying file system. – Rui F Ribeiro Mar 2 '17 at 9:25
  • Sorry about that if I'm wrong – Cro Mar 2 '17 at 9:27
  • You are welcome. see this wiki.ubuntu.com/Lvm – Rui F Ribeiro Mar 2 '17 at 9:28
1

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:

Growing LVM root

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.