1
0
Fork 0
digitalstudium.com/content/en/linux-lifehacks/how-to-extend-lvm-volume-on...

126 lines
3.1 KiB
Markdown
Raw Normal View History

2023-03-08 15:12:15 +00:00
---
title: "Linux: How to extend LVM volume"
2023-04-23 14:56:27 +00:00
date: 2022-05-15
2023-03-08 15:12:15 +00:00
---
2023-05-16 10:49:25 +00:00
This article describes how to expand an LVM group and volume on a Linux operating system.
<!--more-->
2023-03-08 15:12:15 +00:00
### Situation 1: new disk
#### First step: creating a physical volume
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
After you have attached the disk to a physical server or virtual machine, you need to type command:
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
```bash
sudo fdisk -l
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
This is to make sure the drive is recognized by the operating system, and to identify the drive name. Output of the command will be something like this:
2023-04-23 21:06:03 +00:00
2023-07-23 12:55:12 +00:00
```
2023-03-08 15:12:15 +00:00
Disk /dev/vdc: 5 GiB, 5368709120 bytes, 10485760 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
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
Once you have identified the drive name (in our case it is `/dev/vdc`), you can create physical volume using the command:
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
```bash
sudo pvcreate /dev/vdc
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
You will see output like this:
2023-04-23 21:06:03 +00:00
2023-07-23 12:55:12 +00:00
```
2023-03-08 15:12:15 +00:00
kostya@ubuntu-21-04:~$ sudo pvcreate /dev/vdc
Physical volume "/dev/vdc" successfully created.
kostya@ubuntu-21-04:~$
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
#### Step two: extend the volume group
2023-04-23 21:06:03 +00:00
To get a list of available volume groups run this command:
2023-07-23 12:55:12 +00:00
```bash
2023-04-23 21:06:03 +00:00
vgdisplay
```
2023-03-08 15:12:15 +00:00
You can now extend the volume group. This is done by the following command:
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
```bash
sudo vgextend {vg-name} {pv-name}
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
In our case, it will be:
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
```bash
sudo vgextend vg-example /dev/vdc
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
You will see output like this:
2023-04-23 21:06:03 +00:00
2023-07-23 12:55:12 +00:00
```
2023-03-08 15:12:15 +00:00
kostya@ubuntu-21-04:~$ sudo vgextend vg-example /dev/vdc
Physical volume "/dev/vdc" successfully created.
Volume group "vg-example" successfully extended
kostya@ubuntu-21-04:~$
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
#### Step three: extending the logical volume
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
Extending a logical volume can be done with the following command:
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
```bash
sudo lvextend --size + {size} {vg-name/lv-name}
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
In our case, it will be:
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
```bash
sudo lvextend --size +2G vg-example/lv-example
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
You will see output like this:
2023-04-23 21:06:03 +00:00
2023-07-23 12:55:12 +00:00
```
2023-03-08 15:12:15 +00:00
kostya@ubuntu-21-04:~$ sudo lvextend --size +2G vg-example/lv-example
Size of logical volume vg-example/lv-example changed from 5.00 GiB (1280 extents) to 7.00 GiB (1792 extents).
Logical volume vg-example/lv-example successfully resized.
kostya@ubuntu-21-04:~$
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
If you want the logical volume to use all the free space in the volume group, then type
command:
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
```bash
sudo lvextend --extents +100%FREE vg-example/lv-example
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
#### Fourth step: extending the file system
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
If you have xfs file system, then the extending can be done with the following command:
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
```bash
sudo xfs_growfs /dev/{vg-name}/{lv-name}
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
In our case, it will be:
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
```bash
sudo xfs_growfs /dev/vg-example/lv-example
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
For ext4 filesystem, replace `xfs_growfs` with `resize2fs`
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
### Situation 2: if the size of the existing disk has changed
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
Sometimes the size of an existing disk can change, for example, in case of a virtual machine. In this case, the first step will be different, the second step will not be performed, and the rest of the steps will be pretty the same as in the situation with a new disc described above. The first step is not to create a physical volume, but to resize the existing one. It can be done with the command:
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
```bash
sudo pvresize /dev/DISKNAME
```
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
For example,
2023-04-23 21:06:03 +00:00
2023-03-08 15:12:15 +00:00
```bash
sudo pvresize /dev/vdc
```