Resize partitions linux lvm

How to Resize LVM Partition Inside an Extended Partition

Resizing a logical volume in Linux is not very difficult but it can be tricky if the root is under an extended partition.

Resizing a logical volume in Linux is not very difficult and can be achieved through very straightforward approach. Here are the usual steps:

  1. Create a new partition on hard disk.
  2. Add the partition you just created as a physical volume.
  3. Add the new physical volume to the volume group.
  4. Assign space from the volume group to the logical volume.
  5. Resize the filesystem.

But in this scenario, you have the root filesystem (as an LVM partition) mounted under an extended partition, not within a primary partition. You just have one primary partition which is mounted on /boot and rest all space is part of that extended partition.

Sounds troublesome? Let me show you how to resize LVM inside extended partition.

Resizing LVM partition inside extended partition

I am using a Linux installed in virtual machine in this tutorial.

Step 1: Shut down your VM and increase the disk size

First, shut down your VM and increase the disk size. Here, I have increased the disk /dev/sda size by 20 GB to around 40 GB. Then start your VM and go to the console.

Have a look at our disk partitions.

[email protected]:~# fdisk -l Disk /dev/sda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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: 0x000a975f Device Boot Start End Blocks Id System /dev/sda1 * 2048 499711 248832 83 Linux /dev/sda2 501758 41940991 20719617 5 Extended /dev/sda5 501760 41940991 20719616 8e Linux LVM

If you analyze the disk space with df command, here’s what it shows for me:

[email protected]:~# df -hT Filesystem Type Size Used Avail Use% Mounted on udev devtmpfs 989M 4.0K 989M 1% /dev tmpfs tmpfs 201M 716K 200M 1% /run /dev/dm-0 ext4 19G 1.5G 16G 9% / none tmpfs 4.0K 0 4.0K 0% /sys/fs/cgroup none tmpfs 5.0M 0 5.0M 0% /run/lock none tmpfs 1001M 0 1001M 0% /run/shm none tmpfs 100M 0 100M 0% /run/user /dev/sda1 ext2 236M 40M 184M 18% /boot 

Here, the object is to increase the size of the partition /dev/dm-0 which is mounted on /dev/sda5.

Let me also show the current status of physical volumes, volume groups and logical volumes:

[email protected]:~# lvs LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert root ubuntu14-vg -wi-ao--- 18.74g swap_1 ubuntu14-vg -wi-ao--- 1020.00m [email protected]:~# pvs PV VG Fmt Attr PSize PFree /dev/sda5 ubuntu14-vg lvm2 a-- 19.76g 20.00m [email protected]:~# vgs VG #PV #LV #SN Attr VSize VFree ubuntu14-vg 1 2 0 wz--n- 19.76g 20.00m 

They all have around 20 GB of storage space assigned to them.

Читайте также:  Search inside files in linux

Step 2: Begin the LVM resizing process

Here are the steps for resizing the LVM partition:

Open fdisk utility and look at the partitions:

[email protected]:~# fdisk /dev/sda Command (m for help): p Disk /dev/sda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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: 0x000a975f Device Boot Start End Blocks Id System /dev/sda1 * 2048 499711 248832 83 Linux /dev/sda2 501758 41940991 20719617 5 Extended /dev/sda5 501760 41940991 20719616 8e Linux LVM

Delete the extended partition (/dev/sda2) using command d , which will automatically delete the underlying LVM partition which is /dev/sda5.

Command (m for help): d Partition number (1-5): 2

Create a new partition again as extended using command n with default start and end cylinder values.

Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): e Partition number (1-4, default 2): Using default value 2 First sector (499712-83886079, default 499712): Using default value 499712 Last sector, +sectors or +size (499712-83886079, default 83886079): Using default value 83886079 Command (m for help): p Disk /dev/sda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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: 0x000a975f Device Boot Start End Blocks Id System /dev/sda1 * 2048 499711 248832 83 Linux /dev/sda2 499712 83886079 41693184 5 Extended 

Create a logical partition (dev/sda5) using the default start and end cylinder values.

Command (m for help): n Partition type: p primary (1 primary, 1 extended, 2 free) l logical (numbered from 5) Select (default p): l Adding logical partition 5 First sector (501760-83886079, default 501760): Using default value 501760 Last sector, +sectors or +size (501760-83886079, default 83886079): Using default value 83886079

Switch to expert mode by pressing x .

Run expert command b to adjust the beginning of the partition (this changes the partition size, not where it ends). Enter the start value as it was earlier before deleting the partitions. Here it is 501760.

Expert command (m for help): b Partition number (1-5): 5 New beginning of data (499713-83886079, default 501760): 501760

Then run r to return to the main menu.

Expert command (m for help): r

Check the partition number just to make sure.

Command (m for help): p Disk /dev/sda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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: 0x000a975f Device Boot Start End Blocks Id System /dev/sda1 * 2048 499711 248832 83 Linux /dev/sda2 499712 83886079 41693184 5 Extended /dev/sda5 501760 83886079 41692160 83 Linux

Now change the partition type to LVM by pressing t command and chose type 8e .

Command (m for help): t Partition number (1-5): 5 Hex code (type L to list codes): 8e Changed system type of partition 5 to 8e (Linux LVM)

Press w to write all the changes to the disk.

Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.

Step 3: Make manual changes to physical and logical volume

Run partprobe command to inform OS about partition table changes:

Читайте также:  Zabbix template os linux by zabbix agent

Run lsblk command to see that /dev/sda5 is now around 40 GB in size (for me).

[email protected]:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 40G 0 disk ├─sda1 8:1 0 243M 0 part /boot ├─sda2 8:2 0 1K 0 part └─sda5 8:5 0 39.8G 0 part ├─ubuntu14--vg-root (dm-0) 252:0 0 18.8G 0 lvm / └─ubuntu14--vg-swap_1 (dm-1) 252:1 0 1020M 0 lvm [SWAP] sr0 11:0 1 1024M 0 rom 

Run the df command and you’ll notice that /dev/dm-0 still shows the old size details:

[email protected]:~# df -hT Filesystem Type Size Used Avail Use% Mounted on udev devtmpfs 989M 4.0K 989M 1% /dev tmpfs tmpfs 201M 716K 200M 1% /run /dev/dm-0 ext4 19G 1.5G 16G 9% / none tmpfs 4.0K 0 4.0K 0% /sys/fs/cgroup none tmpfs 5.0M 0 5.0M 0% /run/lock none tmpfs 1001M 0 1001M 0% /run/shm none tmpfs 100M 0 100M 0% /run/user /dev/sda1 ext2 236M 40M 184M 18% /boot 

Same is the case with Physical volumes, Volume groups and Logical volumes:

[email protected]:~# lvs LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert root ubuntu14-vg -wi-ao--- 18.74g swap_1 ubuntu14-vg -wi-ao--- 1020.00m [email protected]:~# pvs PV VG Fmt Attr PSize PFree /dev/sda5 ubuntu14-vg lvm2 a-- 19.76g 20.00m [email protected]:~# vgs VG #PV #LV #SN Attr VSize VFree ubuntu14-vg 1 2 0 wz--n- 19.76g 20.00m 

You’l have to do some manual effort here.

Resize the Physical volume:

[email protected]:~# pvresize /dev/sda5 Physical volume "/dev/sda5" changed 1 physical volume(s) resized / 0 physical volume(s) not resized 

Now check Physical Volume and Volume group status and see that it is properly showing the new size:

[email protected]:~# pvs PV VG Fmt Attr PSize PFree /dev/sda5 ubuntu14-vg lvm2 a-- 39.76g 20.02g [email protected]:~# vgs VG #PV #LV #SN Attr VSize VFree ubuntu14-vg 1 2 0 wz--n- 39.76g 20.02g 

Similarly, resize the logical volume:

[email protected]:~# lvextend -l +100%FREE /dev/ubuntu14-vg/root Extending logical volume root to 38.76 GiB Logical volume root successfully resized 

Lastly, resize the filesystem:

[email protected]:~# resize2fs /dev/ubuntu14-vg/root resize2fs 1.42.9 (4-Feb-2014) Filesystem at /dev/ubuntu14-vg/root is mounted on /; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 3 The filesystem on /dev/ubuntu14-vg/root is now 10161152 blocks long. 

Verify the disk status and see that LVM is now resized properly:

[email protected]:~# df -hT Filesystem Type Size Used Avail Use% Mounted on udev devtmpfs 989M 4.0K 989M 1% /dev tmpfs tmpfs 201M 716K 200M 1% /run /dev/dm-0 ext4 39G 1.5G 35G 4% / none tmpfs 4.0K 0 4.0K 0% /sys/fs/cgroup none tmpfs 5.0M 0 5.0M 0% /run/lock none tmpfs 1001M 0 1001M 0% /run/shm none tmpfs 100M 0 100M 0% /run/user /dev/sda1 ext2 236M 40M 184M 18% /boot 

That’s all! You have successfully resized the LVM partition inside an extended partition.

Questions or suggestion? Leave a comment below.

Источник

How to resize a logical volume with 5 simple LVM commands

How to manage logical volumes with 5 simple LVM commands

Have you ever wondered how to extend your root or home directory filesystem partition using LVM? You might have low storage space and you need to increase the capacity of your partitions. This article looks at how to extend storage in Linux using Logical Volume Manager (LVM).

Читайте также:  Linux очистить файл подкачки

Process summary

The process is straightforward. Attach the new storage to the system. Next, create a new Physical Volume (PV) from that storage. Add the PV to the Volume Group (VG) and then extend the Logical Volume (LV).

Look at the picture below. The red line mark shows the original size of the root mount point. The xvdc disk is the new disk attached to it. Extend the root partition to make it 60G in size.

lsblk command displays volume sizes

[ Want to test your sysadmin skills? Take a skills assessment today. ]

Create a Physical Volume

pvcreate command to create a new physical volume

[root@redhat-sysadmin ~]# pvcreate /dev/xvdc Physical volume "/dev/xvdc" successfully created.

When you attach the new storage /dev/xvdc , you need to use the pvcreate command in order for the disk to be initialized and be seen by the Logical Volume Manager (LVM).

Identify the Volume Group

Next, you need to identify the Volume Group (VG) to which you are extending the new disk with the vgs command. Mine is called centos, and that’s the only VG available currently on my LVM.

vgs command displays volume group information

Extend the Volume Group

The vgextend command allows you to add one or more initialized Physical Volumes to an existing VG to extend its size.

As you can see, you want to extend the centos Volume Group.

vgextend command

After extending it, type the vgs or vgdisplay commands for a more detailed overview of the VG.

The vgs command shows only the VG in with a few lines.

vgs command displays volume group information

The vgdisplay shows all the VGs in the LVM and displays the complete information about them.

vgdisplay command displays volume group information

Cloud services

As you can see from the image above, marked with red, you have 10GB free. You can decide to extend all or some amount of storage size to it.

Identify the Logical Volume

The lvs or lvdisplay command shows the Logical Volume associated with a Volume Group. Use the lvs command, and the Logical Volume you’re trying to extend is the root, which belongs to the centos VG. As you can see above, you’ve already extended the VG. Next, extend the Logical Volume.

lvs command displays logical volume information

Extend the Logical Volume

Extend the LV with the lvextend command. The lvextend command allows you to extend the size of the Logical Volume from the Volume Group.

lvextend command displays logical volume information

[root@redhat-sysadmin ~]# lvextend -l +100%FREE /dev/centos/root.

Extend the filesystem

You need to confirm the filesystem type you’re using, Red Hat uses the XFS filesystem, but you can check the filesystem with lsblk -f or df -Th .

Resize the filesystem on the Logical Volume after it has been extended to show the changes. Resize the XFS filesystem by using the xfs_growfs command.

xfs_grow command extends the XFS filesystem

Finally, verify the size of your extended partition.

df -h command displays storage capacity

Wrap up

You can extend any other partition with the steps shown. You just have to ensure you’re using LVM and know the partition you’re extending. If you want to learn more about LVM, check out other articles from Enable Sysadmin by searching LVM.

Источник

Оцените статью
Adblock
detector