Difference between revisions of "LVM"

From Briki
Jump to: navigation, search
(Creating a new volume group & logical volume)
 
(28 intermediate revisions by 9 users not shown)
Line 3: Line 3:
 
* One or more Physical Volumes (eg. /dev/sda2) are joined together in a Volume Group (eg. /dev/vg1)
 
* One or more Physical Volumes (eg. /dev/sda2) are joined together in a Volume Group (eg. /dev/vg1)
 
* A Volume Group is split into one or more Logical Volumes (eg. /dev/vg1/media)
 
* A Volume Group is split into one or more Logical Volumes (eg. /dev/vg1/media)
 +
* Instead of dealing directly in KB/MB/GB, LVM expresses sizes as ''physical extents''. The size of a single physical extent for a volume group can be seen by looking at the '''PE Size''' shown in '''vgdisplay'''.
 +
* If you don't have a physical volume created yet, simply use fdisk
 +
 +
==== Required Packages (ubuntu) ====
 +
 +
* lvm2
 +
 +
== Creating a partition > 2TB ==
 +
* Run parted on the relevant disk:
 +
<pre>
 +
sudo parted /dev/sde
 +
</pre>
 +
 +
* In parted, set the partition type to GPT, change the units to TB (needed to avoid warnings about misaligned partitions), and create a partition using all the space on the disk:
 +
<pre>
 +
(parted) mklabel gpt
 +
Warning: The existing disk label on /dev/sde will be destroyed and all data on this disk will be lost. Do you want to continue?
 +
Yes/No? y
 +
(parted) unit TB
 +
(parted) mkpart primary 0 -1
 +
(parted) quit
 +
</pre>
 +
 +
Run 'print' at any time to check the current partition table.
  
 
== Creating a new volume group & logical volume ==
 
== Creating a new volume group & logical volume ==
Line 11: Line 35:
 
sudo pvcreate /dev/sda2
 
sudo pvcreate /dev/sda2
 
</pre>
 
</pre>
 +
NB. Although you can setup an entire disk as a physical volume (eg. via '''sudo pvcreate /dev/sda''') this is not recommended due to possible confusion with other OSs which won't recognise the disk as being used.
  
 
* Create a volume group consisting of only this physical volume:
 
* Create a volume group consisting of only this physical volume:
Line 21: Line 46:
 
vgdisplay
 
vgdisplay
 
</pre>
 
</pre>
This should show 1 volume group consisting of 0 LVs (see '''Cur LV''' and 1 PV (see '''Cur PV''').
+
This should show 1 volume group consisting of 0 LVs (see '''Cur LV''') and 1 PV (see '''Cur PV'''). Note the number of free physical extents (PE) in the volume group (see '''Free PE''').
  
* Create a logical volume:
+
* Create a logical volume with this number of free extents (if lvcreate complains about kernel modules not being loaded, run '''sudo modprobe dm-mod'''):
 
<pre>
 
<pre>
sudo lvcreate --size 30g --name media vg1
+
sudo lvcreate -l <num-extents> --name media vg1
 
</pre>
 
</pre>
  
* Format the logical volume as ext3:
+
* Format the logical volume as ext4:
 
<pre>
 
<pre>
mkfs.ext3 /dev/vg1/media
+
mkfs.ext4 /dev/vg1/media
 
</pre>
 
</pre>
  
Line 36: Line 61:
 
<pre>
 
<pre>
 
/dev/vg1/media  /var/media  ext3  defaults    0  2
 
/dev/vg1/media  /var/media  ext3  defaults    0  2
 +
</pre>
 +
<pre>
 +
sudo mount /dev/vg1/media
 +
</pre>
 +
 +
== Adding a physical volume to a volume group and logical volume ==
 +
For example, to add /dev/sdb2 to /dev/vg1/media
 +
 +
* Setup partition as a physical volume:
 +
<pre>
 +
sudo pvcreate /dev/sdb2
 +
</pre>
 +
 +
* Extend the volume group to cover the new physical volume:
 +
<pre>
 +
sudo vgextend vg1 /dev/sdb2
 +
</pre>
 +
 +
* Check volume group has been changed successfully, and note the number of new free extents (see '''Free PE'''):
 +
<pre>
 +
vgdisplay
 +
</pre>
 +
 +
* Unmount the logical volume:
 +
<pre>
 +
sudo umount /dev/vg1/media
 +
</pre>
 +
 +
* Extend the logical volume:
 +
<pre>
 +
sudo lvextend -l +<num-extents> /dev/vg1/media
 +
</pre>
 +
 +
* Check that the volume sizes look correct
 +
<pre>
 +
sudo vgdisplay
 +
</pre>
 +
 +
* Resize the ext3 filesystem on /dev/vg1/media
 +
<pre>
 +
sudo resize2fs /dev/vg1/media
 
</pre>
 
</pre>
  
 +
* Mount the logical volume:
 +
<pre>
 +
sudo mount /dev/vg1/media
 +
</pre>
  
 
== Resizing a physical volume ==
 
== Resizing a physical volume ==
For example, extending /dev/sda2. Note here that we are extending a '''physical volume''', not just a logical one.
+
For example, deleting /dev/sda3 and extending /dev/sda2 to use the free space. Note here that we are extending a '''physical''' volume, not just a logical one.
  
 
* Unmount any logical volumes which use /dev/sda2
 
* Unmount any logical volumes which use /dev/sda2
 +
<pre>
 +
sudo umount /dev/vg1/media
 +
</pre>
 +
 +
* Use '''fdisk''' to delete /dev/sda3, delete old /dev/sda2 and recreate as a larger partition (this will not actually delete any data from /dev/sda2)
 +
<pre>
 +
$ sudo fdisk /dev/sda
 +
 +
The number of cylinders for this disk is set to 38913.
 +
There is nothing wrong with that, but this is larger than 1024,
 +
and could in certain setups cause problems with:
 +
1) software that runs at boot time (e.g., old versions of LILO)
 +
2) booting and partitioning software from other OSs
 +
  (e.g., DOS FDISK, OS/2 FDISK)
 +
 +
Command (m for help): p
 +
 +
Disk /dev/sda: 320.0 GB, 320072933376 bytes
 +
255 heads, 63 sectors/track, 38913 cylinders
 +
Units = cylinders of 16065 * 512 = 8225280 bytes
 +
Disk identifier: 0x08270827
 +
 +
  Device Boot      Start        End      Blocks  Id  System
 +
/dev/sda1  *          1      16708  134206976    7  HPFS/NTFS
 +
/dev/sda2          16708      20532    30716282+  83  Linux
 +
/dev/sda3          20533      38913  147645380    7  HPFS/NTFS
 +
 +
Command (m for help): d
 +
Partition number (1-4): 3
 +
 +
Command (m for help): p
 +
 +
Disk /dev/sda: 320.0 GB, 320072933376 bytes
 +
255 heads, 63 sectors/track, 38913 cylinders
 +
Units = cylinders of 16065 * 512 = 8225280 bytes
 +
Disk identifier: 0x08270827
 +
 +
  Device Boot      Start        End      Blocks  Id  System
 +
/dev/sda1  *          1      16708  134206976    7  HPFS/NTFS
 +
/dev/sda2          16708      20532    30716282+  83  Linux
 +
 +
Command (m for help): d
 +
Partition number (1-4): 2
 +
 +
Command (m for help): n
 +
Command action
 +
  e  extended
 +
  p  primary partition (1-4)
 +
p
 +
Partition number (1-4): 2
 +
First sector (268414015-625142447, default 268414015):
 +
Using default value 268414015
 +
Last sector or +size or +sizeM or +sizeK (268414015-625142447, default 625142447):
 +
Using default value 625142447
 +
 +
Command (m for help): p
 +
 +
Disk /dev/sda: 320.0 GB, 320072933376 bytes
 +
255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors
 +
Units = sectors of 1 * 512 = 512 bytes
 +
Disk identifier: 0x08270827
 +
 +
  Device Boot      Start        End      Blocks  Id  System
 +
/dev/sda1  *          63  268414014  134206976    7  HPFS/NTFS
 +
/dev/sda2      268414015  625142447  178364216+  83  Linux
 +
 +
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.
 +
Syncing disks.
 +
</pre>
 +
 +
* If fdisk warned you that the kernel still uses the old table, you will need to reboot now. Remember to unmount relevant logical volumes again after rebooting.
 +
 +
* Resize the LVM physical volume to match the new partition size
 +
<pre>
 +
sudo pvresize /dev/sda2
 +
</pre>
 +
 +
* Check how many free extents exist in the volume group - you should see extents corresponding to the increase in size of your physical partition
 +
<pre>
 +
sudo vgdisplay
 +
</pre>
 +
 +
* Add this many extents to /dev/vg1/media
 +
<pre>
 +
sudo lvextend -l +<num-extents> /dev/vg1/media
 +
</pre>
 +
 +
* Check that the volume sizes look correct
 +
<pre>
 +
sudo vgdisplay
 +
</pre>
 +
 +
* Resize the ext3 filesystem on /dev/vg1/media
 +
<pre>
 +
sudo resize2fs /dev/vg1/media
 +
</pre>
 +
 +
* Remount affected filesystems
 +
<pre>
 +
sudo mount /dev/vg1/media
 +
</pre>
 +
 +
* Check that everything looks ok :)
 +
 +
== Shrink a logical volume ==
 +
 +
* Reduce the size of the logical volume to the intended size (either by specifying new size in kB/MB/GB or number of extents, or by specifying the amount to remove by prefixing with '-') :
 +
<pre>
 +
sudo lvreduce --resizefs -l -384000 /dev/maine/media
 +
fsck from util-linux 2.31.1
 +
<snip/>
 +
resize2fs 1.44.1 (24-Mar-2018)
 +
Resizing the filesystem on /dev/mapper/maine-media to 3220777984 (4k) blocks.
 +
The filesystem on /dev/mapper/maine-media is now 3220777984 (4k) blocks long.
 +
 +
  Size of logical volume maine/media changed from 13.46 TiB (3529291 extents) to <12.00 TiB (3145291 extents).
 +
  Logical volume maine/media successfully resized.
 +
</pre>
 +
 +
=== Old Method ===
 +
* First, reduce the filesystem size to slightly smaller than you want the final size to be:
 +
<pre>
 +
sudo resize2fs /dev/mapper/vg1-backup 390G
 +
resize2fs 1.40.8 (13-Mar-2008)
 +
Resizing the filesystem on /dev/mapper/vg1-backup to 102236160 (4k) blocks.
 +
The filesystem on /dev/mapper/vg1-backup is now 102236160 blocks long.
 +
</pre>
 +
 +
* Reduce the size of the logical volume to the intended size:
 +
<pre>
 +
sudo lvreduce -L 400G /dev/vg1/backup
 +
  WARNING: Reducing active logical volume to 400.00 GB
 +
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
 +
Do you really want to reduce backup? [y/n]: y
 +
  Reducing logical volume backup to 400.00 GB
 +
  Logical volume backup successfully resized
 +
</pre>
 +
 +
* Resize the filesystem to match the new logical volume size (you may be asked to run e2fsck first):
 +
<pre>
 +
sudo resize2fs /dev/mapper/vg1-backup
 +
resize2fs 1.40.8 (13-Mar-2008)
 +
Resizing the filesystem on /dev/mapper/vg1-backup to 104857600 (4k) blocks.
 +
The filesystem on /dev/mapper/vg1-backup is now 104857600 blocks long.
 +
</pre>
  
* Use '''fdisk''' to delete old /dev/sda2 and recreate as a larger partition (this will not actually delete any data). If fdisk warns you that the partition table will not be written until the next reboot, you will need to reboot now. Remember to unmount relevant logical volumes again after rebooting.
+
== Reactivating a volume group ==
 +
If things have gone a bit wrong, and you've had to manually fix stuff (with fdisk, and the like), then to reactivate the volume group, and get it to recognise the fixed LVs, simply run
 +
<pre>
 +
sudo vgchange -a y /dev/vg1
 +
</pre>
  
* sudo pvresize /dev/sda2
+
== Sample vgdisplay Output ==
 +
<pre>
 +
  --- Volume group ---
 +
  VG Name              vg1
 +
  System ID
 +
  Format                lvm2
 +
  Metadata Areas        1
 +
  Metadata Sequence No  5
 +
  VG Access            read/write
 +
  VG Status            resizable
 +
  MAX LV                0
 +
  Cur LV                1
 +
  Open LV              1
 +
  Max PV                0
 +
  Cur PV                1
 +
  Act PV                1
 +
  VG Size              170.10 GB
 +
  PE Size              4.00 MB
 +
  Total PE              43545
 +
  Alloc PE / Size      43545 / 170.10 GB
 +
  Free  PE / Size      0 / 0
 +
  VG UUID              TACrPR-xO6J-mHz0-hiMn-ax3G-R3a4-dv2ivp
 +
</pre>
  
* Check how many free extents exist using ''sudo vgdisplay''
 
  
* Add this many extents to /dev/vg1/media using ''sudo lvextend -l +<num-extents> /dev/vg1/media''
+
== Useful Commands ==
  
* Resize the filesystem on /dev/vg1/media using ''sudo resize2fs /dev/vg1/media''
+
; fuser -vm <mountpoint> : Display processes using mountpoint
 +
; vgdisplay : Display volume group information
 +
; pvdisplay : Display physical volume information
 +
; lvdisplay : Display logical volume information
 +
; pvdisplay -m : Show PV -> LV mapping
 +
; lvdisplay -m : Show LV -> PV mapping
 +
; lvs -o +devices : Show concise LV -> PV mapping

Latest revision as of 12:00, 11 November 2021

Overview

  • One or more Physical Volumes (eg. /dev/sda2) are joined together in a Volume Group (eg. /dev/vg1)
  • A Volume Group is split into one or more Logical Volumes (eg. /dev/vg1/media)
  • Instead of dealing directly in KB/MB/GB, LVM expresses sizes as physical extents. The size of a single physical extent for a volume group can be seen by looking at the PE Size shown in vgdisplay.
  • If you don't have a physical volume created yet, simply use fdisk

Required Packages (ubuntu)

  • lvm2

Creating a partition > 2TB

  • Run parted on the relevant disk:
sudo parted /dev/sde
  • In parted, set the partition type to GPT, change the units to TB (needed to avoid warnings about misaligned partitions), and create a partition using all the space on the disk:
(parted) mklabel gpt
Warning: The existing disk label on /dev/sde will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? y
(parted) unit TB
(parted) mkpart primary 0 -1
(parted) quit

Run 'print' at any time to check the current partition table.

Creating a new volume group & logical volume

To create a volume group (/dev/vg1) consisting of 1 physical volume (/dev/sda2), and split into only 1 logical volume (/dev/vg1/media):

  • Setup partition as a physical volume:
sudo pvcreate /dev/sda2

NB. Although you can setup an entire disk as a physical volume (eg. via sudo pvcreate /dev/sda) this is not recommended due to possible confusion with other OSs which won't recognise the disk as being used.

  • Create a volume group consisting of only this physical volume:
sudo vgcreate vg1 /dev/sda2
  • Check volume group has been created successfully:
vgdisplay

This should show 1 volume group consisting of 0 LVs (see Cur LV) and 1 PV (see Cur PV). Note the number of free physical extents (PE) in the volume group (see Free PE).

  • Create a logical volume with this number of free extents (if lvcreate complains about kernel modules not being loaded, run sudo modprobe dm-mod):
sudo lvcreate -l <num-extents> --name media vg1
  • Format the logical volume as ext4:
mkfs.ext4 /dev/vg1/media
  • Mount the volume by adding the following line to /etc/fstab:
/dev/vg1/media   /var/media   ext3   defaults    0  2
sudo mount /dev/vg1/media

Adding a physical volume to a volume group and logical volume

For example, to add /dev/sdb2 to /dev/vg1/media

  • Setup partition as a physical volume:
sudo pvcreate /dev/sdb2
  • Extend the volume group to cover the new physical volume:
sudo vgextend vg1 /dev/sdb2
  • Check volume group has been changed successfully, and note the number of new free extents (see Free PE):
vgdisplay
  • Unmount the logical volume:
sudo umount /dev/vg1/media
  • Extend the logical volume:
sudo lvextend -l +<num-extents> /dev/vg1/media
  • Check that the volume sizes look correct
sudo vgdisplay
  • Resize the ext3 filesystem on /dev/vg1/media
sudo resize2fs /dev/vg1/media
  • Mount the logical volume:
sudo mount /dev/vg1/media

Resizing a physical volume

For example, deleting /dev/sda3 and extending /dev/sda2 to use the free space. Note here that we are extending a physical volume, not just a logical one.

  • Unmount any logical volumes which use /dev/sda2
sudo umount /dev/vg1/media
  • Use fdisk to delete /dev/sda3, delete old /dev/sda2 and recreate as a larger partition (this will not actually delete any data from /dev/sda2)
$ sudo fdisk /dev/sda

The number of cylinders for this disk is set to 38913.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x08270827

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       16708   134206976    7  HPFS/NTFS
/dev/sda2           16708       20532    30716282+  83  Linux
/dev/sda3           20533       38913   147645380    7  HPFS/NTFS

Command (m for help): d
Partition number (1-4): 3

Command (m for help): p

Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x08270827

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       16708   134206976    7  HPFS/NTFS
/dev/sda2           16708       20532    30716282+  83  Linux

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

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First sector (268414015-625142447, default 268414015):
Using default value 268414015
Last sector or +size or +sizeM or +sizeK (268414015-625142447, default 625142447):
Using default value 625142447

Command (m for help): p

Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x08270827

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *          63   268414014   134206976    7  HPFS/NTFS
/dev/sda2       268414015   625142447   178364216+  83  Linux

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.
Syncing disks.
  • If fdisk warned you that the kernel still uses the old table, you will need to reboot now. Remember to unmount relevant logical volumes again after rebooting.
  • Resize the LVM physical volume to match the new partition size
sudo pvresize /dev/sda2
  • Check how many free extents exist in the volume group - you should see extents corresponding to the increase in size of your physical partition
sudo vgdisplay
  • Add this many extents to /dev/vg1/media
sudo lvextend -l +<num-extents> /dev/vg1/media
  • Check that the volume sizes look correct
sudo vgdisplay
  • Resize the ext3 filesystem on /dev/vg1/media
sudo resize2fs /dev/vg1/media
  • Remount affected filesystems
sudo mount /dev/vg1/media
  • Check that everything looks ok :)

Shrink a logical volume

  • Reduce the size of the logical volume to the intended size (either by specifying new size in kB/MB/GB or number of extents, or by specifying the amount to remove by prefixing with '-') :
sudo lvreduce --resizefs -l -384000 /dev/maine/media
fsck from util-linux 2.31.1
<snip/>
resize2fs 1.44.1 (24-Mar-2018)
Resizing the filesystem on /dev/mapper/maine-media to 3220777984 (4k) blocks.
The filesystem on /dev/mapper/maine-media is now 3220777984 (4k) blocks long.

  Size of logical volume maine/media changed from 13.46 TiB (3529291 extents) to <12.00 TiB (3145291 extents).
  Logical volume maine/media successfully resized.

Old Method

  • First, reduce the filesystem size to slightly smaller than you want the final size to be:
sudo resize2fs /dev/mapper/vg1-backup 390G
resize2fs 1.40.8 (13-Mar-2008)
Resizing the filesystem on /dev/mapper/vg1-backup to 102236160 (4k) blocks.
The filesystem on /dev/mapper/vg1-backup is now 102236160 blocks long.
  • Reduce the size of the logical volume to the intended size:
sudo lvreduce -L 400G /dev/vg1/backup
  WARNING: Reducing active logical volume to 400.00 GB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce backup? [y/n]: y
  Reducing logical volume backup to 400.00 GB
  Logical volume backup successfully resized
  • Resize the filesystem to match the new logical volume size (you may be asked to run e2fsck first):
sudo resize2fs /dev/mapper/vg1-backup
resize2fs 1.40.8 (13-Mar-2008)
Resizing the filesystem on /dev/mapper/vg1-backup to 104857600 (4k) blocks.
The filesystem on /dev/mapper/vg1-backup is now 104857600 blocks long.

Reactivating a volume group

If things have gone a bit wrong, and you've had to manually fix stuff (with fdisk, and the like), then to reactivate the volume group, and get it to recognise the fixed LVs, simply run

sudo vgchange -a y /dev/vg1

Sample vgdisplay Output

  --- Volume group ---
  VG Name               vg1
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               170.10 GB
  PE Size               4.00 MB
  Total PE              43545
  Alloc PE / Size       43545 / 170.10 GB
  Free  PE / Size       0 / 0
  VG UUID               TACrPR-xO6J-mHz0-hiMn-ax3G-R3a4-dv2ivp


Useful Commands

fuser -vm <mountpoint> 
Display processes using mountpoint
vgdisplay 
Display volume group information
pvdisplay 
Display physical volume information
lvdisplay 
Display logical volume information
pvdisplay -m 
Show PV -> LV mapping
lvdisplay -m 
Show LV -> PV mapping
lvs -o +devices 
Show concise LV -> PV mapping