28

I have a virtual machine that recently had its disk image increased from 20GB to 50GB, and fdisk -l verifies that the VM can see this new size. Now I need to resize my root LVM partition to fill the extra 30GB.

I've found several articles about resizing LVM, but the few that cover resizing the root partition all claim you need to boot from a LiveCD. Is there any way to do this without taking down the server? The server is critical, so I'd like to minimize downtime.

Edit: Output of fdisk -l:

[root@fedora-host ~]# sudo fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders, total 104857600 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
Disk identifier: 0x00097c90

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    83886079    41430016   8e  Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 36.1 GB, 36104568832 bytes
255 heads, 63 sectors/track, 4389 cylinders, total 70516736 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
Disk identifier: 0x00000000

Disk /dev/mapper/VolGroup-lv_root doesn't contain a valid partition table

Disk /dev/mapper/VolGroup-lv_swap: 6308 MB, 6308233216 bytes
255 heads, 63 sectors/track, 766 cylinders, total 12320768 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
Disk identifier: 0x00000000

Disk /dev/mapper/VolGroup-lv_swap doesn't contain a valid partition table

Edit: How do I resize the physical partition? fdisk can see the free space, but I don't know how to resize primary LVM partition to use it. I tried booting into a LiveCD and using parted'd resize command, but all it gives me is the error "Unable to detect file system". I found this guide, which says I need to delete the partition and create a new one with the correct size, but that sounds very dangerous.

Final Edit: Parted's resize command is oddly unable to resize LVM partitions. Go figure. Instead, I simply deleted the old partition and created a new one with the new range, as outlined in the link above, and that correctly resized the LVM partition. I then followed the advice below to resize the volumes and filesystems inside the LVM partition.

Cerin
  • 3,497
  • 17
  • 57
  • 72

7 Answers7

35

You can grow a logical volume online. You'd have to unmount it to shrink it (which requires a LiveCD / Rescue Mode.)

  1. pvresize /dev/sda2 (assuming your LVM partition is sda2. Replace as required.)
  2. lvextend /dev/mapper/root -l+100%FREE (or, whatever your root logical volume is called.)
  3. resize2fs /dev/mapper/root (assuming ext2/3/4)
Aaron Copley
  • 12,345
  • 5
  • 46
  • 67
  • 1
    I personally prefer `lvextend -L [size][unit] /dev/group/volume`, if only because I can extend a volume by 10 gigabytes by specifying `-L +10G` – Hubert Kario Sep 05 '12 at 20:20
  • 1
    Yea, but the OP asked; `Now I need to resize my root LVM partition to fill the extra 30GB.` (Which is to grow the logical volume by the available free space.) – Aaron Copley Sep 05 '12 at 21:17
  • "New size (1076 extents) matches existing size (1076 extents)" – Cerin Sep 05 '12 at 23:41
  • See Zoredache's answer. It doesn't look like the partition was resized. From your original post it sounded like it had been. – Aaron Copley Sep 06 '12 at 12:42
  • 10
    I thought this trick was neat: If you add the `-r` option to `lvextend`, it will hook `fsadm` to resize the FS. This would make the 3rd step redundant. – Jodie C Sep 07 '12 at 02:34
  • If you don't know the path to your logical and physical volumes (like me) use @HubertKario's tip: `pvs` and `lvs` to list them and make sure you're resizing the right thing. – hobs Feb 20 '15 at 22:37
  • 3
    Note this answer assumes the partition has already been extended to fill the free space and just the logical volumes need to be updated. The `pvresize /dev/sda2` command does nothing until the /dev/sda2 partition has been resized with parted or fdisk. I had to resize mine with fdisk as parted on my system would not show or does not have a resize command. Fdisk resizing involves deleting and recreating the partition. – Jason Mar 15 '18 at 22:24
  • Right, that assumption was made based on the question and the information given in the question. I [commented on this](https://serverfault.com/questions/424678/how-to-resize-root-lvm-partition-in-fedora-without-livecd-or-rebooting/424682#comment454039_424682) a few lines up from here. No harm in bringing extra attention to it, though. – Aaron Copley Mar 17 '18 at 17:24
  • For Fedora, replace 3. `resize2fs /dev/mapper/root` with `fsadm resize /dev/mapper/root` as indicated in [this answer](https://stackoverflow.com/a/60938514/988248). – Dave Snyder May 18 '20 at 22:00
3

Install gparted. Run as root (use ssh -X or x2go if remote). Select the partition. It should show free space past the end (to the right). If it doesn't, you need to reboot for your virtual machine to see the new size of the virtual device. That should be minimal downtime with systemd.

Right click and select resize. Drag the end to the end of the disk. Make sure you do not touch the beginning. gparted is able to move a partition during resize, but that will, of course destroy a running system. But moving the end is fine. Review the pending operation gparted will perform to make sure you are extending and not moving the partition. Click apply. gparted will change the partition on disk, then issue the kernel ioctl to reread the partition table, changing the size of the live block device as well. Then, since it sees it is an LVM PV, it runs pvresize for you. Exit gparted, and vgs now shows your new space.

I've heard rumors that parted can do all that without the overhead of a GUI, but I've never been able to figure out the voodoo. Parted has to be one of the worst CLI interfaces I've ever tried to use.

Don't use fdisk, you have to delete and reallocate the partition to resize, and by default it will wipe the LVM2 signature on write. I think I saw an "advanced" option to turn off that destructive behavior, but I've never tested it.

You could also try cfdisk (curses fdisk). It might have a usable partition resize. It is easy enough to run pvresize manually, and there is a command to tell the kernel to reread the partition table. But I'd have to test to make sure it doesn't wipe the LVM signature like fdisk.

2

Did your system really change from 20 to 50 GB? That seems really odd since your sda2 device is ~40GB, lv_root shows 36GB, and swap shows 6GB.

Your output of fdisk -l shows the total sectors of /dev/sda is 104857600. The last sector used by /dev/sda2 is 83886079. This tells me that you have not adjusted the partition size to use all the available space on the drive. The pvresize command doesn't change the partition table. You need to change the partition table first.

Zoredache
  • 128,755
  • 40
  • 271
  • 413
0

If the LVM physical volume is on a partition then, no you can't resize it without rebooting.

You can edit partition table on drive, but Linux will refuse to use it until you reboot.

If you're using XFS, switch resize2fs to xfs_growfs.

You can display the PVs you use and LVs in volume group using pvs and lvs.

PV on raw disk

pvresize /dev/sda
lvextend /dev/<volume-group>/<lv-name> -L +<size>G
resize2fs /dev/<volume-group>/<lv-name>

PV on a partition

First you have to resize the partition, use parted, fdisk or similar tool for that. Remember that those tools edit only the description where partitions are, they don't move data around. In other words, easily you can extend only the last partition on the disk. Then reboot your machine and after that you should be able to inform LVM that it has additional space available:

pvresize /dev/sda2
lvextend /dev/<volume-group>/<lv-name> -L +<size>G
resize2fs /dev/<volume-group>/<lv-name>
Hubert Kario
  • 6,351
  • 6
  • 33
  • 65
  • Using a Gparted Live CD, `sudo pvresize /dev/sda2` reports "1 physical volume(s) resized", but `sudo lvextend /dev/mapper/VolGroup-lv_root -L +10G` fails with the error "insufficient free space: 320 extents needed, but only 0 available". `sudo fdisk -l` reports that I have 54G available, while `sudo pvdisplay` reports only 40G are in use. Why is lvextend failing? – Cerin Sep 05 '12 at 21:10
  • 1
    @Cerin, you missed the Huberts first statement for a PV on a partition. **First you have to resize the partition, use parted, fdisk or similar tool for that**. – Zoredache Sep 06 '12 at 01:24
  • @Zoredache, Ok, well, that's basically my entire question. I don't know how to do that. I just booted into a Gparted LiveCD and tried to use parted's `resize` command to increase the primary partition size, but it gave me the error "Could not detect filesystem". How do you resize the partition using parted or fdisk? – Cerin Sep 06 '12 at 18:39
  • @Cerin: search SF or ask another question – Hubert Kario Sep 06 '12 at 21:29
0

I'd consider checking out ssm (system-storage-manager) if it's available on your distro.

Check out this for a little more info: https://unix.stackexchange.com/a/293173

ssm appears to make it easier to resize logical volumes without having to do everything yourself.

I was able to use to extend my home partition with it after rebooting my OS and logging in first as root. (Had to reboot twice in total, but it worked in the end and i now have an extra 50G available in my /home parition.)

0

After combining 25 howto's, I finally have this solution. It requires no live cd, it's completely on the fly.

Step 1. format the partition with parted (no gui). In this example the max size of the disk increased from 8BG to 32 GB in virtual box.

    # parted
       ....
       Number   Begin   End     Size     Type     FileSystem       Flags
         1      1049kB  1075MB  1074MB   primary  ext4             boot
         2      1075MB  8590MB  7515MB   primary                   lvm
       ....
    (parted) mkpart primary ext2 8590 100%
       ....
       Number   Begin   End     Size     Type     FileSystem       Flags
         1      1049kB  1075MB  1074MB   primary  ext4             boot
         2      1075MB  8590MB  7515MB   primary                   lvm
         3      8590MB  34,4GB  25,8GB   primary  ext2             lba
       ....
    (parted) set 3 lvm on
    (parted) set 3 lba off
    (parted) print
       ....
         3      8590MB  34,4GB  25,8GB   primary  ext2             lvm
       ....
    (parted) quit

Step 2. Create a Physical Volume based on the new partition

# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.

Step 3. Add the physical volume to the Volume Group. In a plain fedora 28 server install the volume group is called fedora.

# vgextend fedora /dev/sda3
  Volume group "fedora" successfully extended

Step 4. Add the available space in the volume group to the logical volume. In a plain fedora 28 server install the logical volume is called root.

# lvextend -l+100%FREE /dev/fedora/root
  Size of logical volume fedora/root changed from <6,20 GiB (1586 extents) to 30,19 GiB (7729 extents).
  Logical volume fedora/root successfully resized.

Step 5. Tell the filesystem in the logical volume to use the extra space. In fedora the filesystem is xfs.

Attention! xfs_growfs uses the mount point to identify the filesystem, not the name of the filesystem you find in df output!!!!!!!

The command 'xfs_growfs /dev/mapper/fedora-root' will tell you /dev/mapper/fedora-root is not a mounted XFS filesystem. If you are using a different filesystem refer to https://www.tldp.org/HOWTO/LVM-HOWTO/extendlv.html

# xfs_growfs /
    data blocks changed from 1624064 to 7914496

Step 6. Enjoy

# df
FileSystem               1K-blocks      Used   Available used% Mounted on
....
/dev/mapper/fedora-root   31647744   6492840    25154904  21% /
/dev/sda1                   999320    125576      804932  14% /boot
....
-1

Try testdisk

sudo dnf install testdisk

sudo testdisk

Just set create a log file Then it will list the partitions, - select the media/partition - then type - and analyse - quick search - select and write