---
# System prepended metadata

title: LVM Learnings

---

# LVM Learnings
This documents my learnings on using LVM. The notes stem primarily from a LinkedIn learning course that can be found [here](https://www.linkedin.com/learning/linux-foundation-cert-prep-storage-management-ubuntu/understanding-lvm?trk=learning-serp_learning_search-card&upsellOrderOrigin=homepage-learning_learning-search-bar_search-submit).

## Basics

### Key terms
* physical volumes
* logical volumes
* volume groups
* extent

### Command Paradigm
 The commands for each context are named:
1. `pv*`
2. `lv*`
3. `vg*`

corresponding to physical volumes (pv), logical volumes (lv), and virtual groups (vg) respectively.  

### Notes
LVM stands for `Logical Volume Management` and is an abstraction layer over physical volumes that allows for data to be stored in virtual partitions that can span multiple physical disks.  

A virtual disk can host multiple physical volumes. Physical volumes are grouped in a **volume group** or **VG**.  

Physical and logical volumes are divided into units called **extents**.  They are chunks of blocks that map between the logical and physical volumes.  

### What LVM allows you to do
1. You can dynamically shrink or grow logical volumes across physical volumes. 
2. Create snapshots of data

## Steps for adding Disks to a Machine
LVM can create a volume on a whole disk, but it is recommended that you first create a partition on the disk then add a logical volume on top of that. 

### Setting up the disk
Add a standard [GPT](https://en.wikipedia.org/wiki/GUID_Partition_Table) partition to the physical disk using`fdisk`.
1. `fdisk /dev/<disk>`. 
2. In the `fdisk` shell use the `g` command to create a new GPT partition.  
3. Run `n` to add the partition. Feel free to use the defaults on the partition. 
4. Finally commit the partition with `w` to write the change.

For information on the `/dev` directory see [here](https://www.linuxnix.com/linux-directory-structure-explained-dev-folder/).

### Creating a physical volume
To create the physical volume run:
1. `pvcreate /dev/<disk partition>`
    a. This will create the physical volume. It may prompt you to replace an existing partition type.
    b. Since you created the partition you will have a reference to the disk itself e.g. `/dev/sda`, and a reference to the partition e.g. `/dev/sda1`. When creating a physical volume reference the disk partition and not the disk itself.

The `extent` size is 4mb by default.  

To see the new physical volume you can use:
1. `pvdisplay`
2. `pvs`: for more of a compact view.

### Creating a volume group
To add a physical volume to a new volume group run:
`vcreate <group_name> <physical volume1> <physical volume2> ...`

### Creating a Logical Volume
Logical volumes are expressed in terms of `extents`.  
LVM manages the mapping from logical extends to physical extents. 

Options for creating a logical volume:
1. `lvcreate -L 100G`: used to create a logical volume of an explicit size in GT, TB, etc.
2. `lvcreate -l 100%VG`: use 100% of space in the VG
3. `lvcreate -l 100%FREE`: use 100% of the remaining space of the VG
4. `lvcreate -l <n>`: use n extents.  

The number of extents of a physical volume can be found with the `pvdisplay` command and looking at the `Total PE` (total physical extents).  

Example command:
`lvcreate -l 100%FREE -n mydata mygroup`
Where `-n` represents the name of the logical volume. And the `mygroup` is referencing the volume group the logical volume should be a part of.

The logical volumes are available in the `/dev/mapper/` folder.  Thee are also availabe in `/dev/<group>/<lv name>`.

Example:
```
$ ls /dev/mapper/
mygroup-mydata

$ ls /dev/mygroup/
mydata
```

### Simple Mounting for Data
Now that we have a logical volume we need to build a file system on the volume.  
To do so run 
```
mkfs.ext4 /dev/<group>/<volume>
```
For more information on `ext*` file system, go [here](https://www.tecmint.com/what-is-ext2-ext3-ext4-and-how-to-create-and-convert-linux-file-systems/).

Now we need to mount the volume.  
```
$ mkdir /mnt/<some name>`
$ mount /dev/<group>/<volume> /mnt/<some name>
```
Note the `/mnt` directory is for temporary mount points.  For more information see [here](http://www.linfo.org/mnt.html).

Now it's mounted and we can create files on the volume!

Test it out:
```
$ touch file1 /mnt/<some name>
$ ls /mnt/<some_name>
file1
```
To permanently mount the volume you will need to create a new directory outside of `/mnt` and setup your `/etc/fstab` to tell your system to mount the disk to that directory on boot.  For more on fstab see [here](https://www.howtogeek.com/howto/38125/htg-explains-what-is-the-linux-fstab-and-how-does-it-work/).

## Extending
Basic gist of what we are going to do: 
Given a disk and a physical volume that has no logical volumes attached to it yet, we are going to extend an existing volume group and logical volume to use the new physical volume.

### Extending a Volume Group
`vgextend <group> <physical volume>`

By looking at your volume group with `vgdisplay` you can see the number of current physical volumes found under `Cur PV` to have increased.  

### Extending a Logical Volume
Command Options:
* `lvextend -l +<number of extents> <LV to extend>`
* `lvextend -l +100%FREE <LV to extend>`
* `lvextend -L +2G <LV to extend>`

The lowerecase `-l` is for specifying extents.
The uppercase `-L` is for specifying general size.

Note: the `+` is important. Without it you are actually specifing the exact size of the logical volume you want, not to add the specified size to the existing LV.


To get the number of extents that are available on your system, use `vgdisplay` and look at `Free PE`. 

#### Extending the File System
This isn't LVM related but it's important for extending your file system

To extend the file system use:
`resize2fs <lv>`

Confirm by using `df -h` to see size of your filesystem before and after resizing.

Note: if the logical volume is not showing up with the `df` command, make sure the volume is mounted.

## Migrate and Replace a Physical Volume
Say we want to move our data from one physical volume to another. To do this we utilize the `pvmove` command.

Two options:
* `pvmove <old pv> <new pv>`
    * specifiying where to put the data explicitly
* `pvmove <old pv>`
    * lvm will figure out where to put it for you

`pvmove` is very slow and if it's interrupted can cause data loss. Be careful.

Once you have migrated the data off a disk you can remove the old physical volume from the volume group with `vgreduce <group> <old volume>`.

If you are repurposing the disk you can use `pvremove <physical volume>`. 

## Filesystem Security and Backups

### ACL's
 * `ACL`: access control list

An ACL is an extension of the existing owner group access system, that allows for more granular permissions. 

To give a specific user access to a file:
```
setfacl -m u:<username>:<permissions> <file>
```
Where permissions can be `rw` for read write.

To see the ACL's on a file you can run:
`getfacl <file>`

Also you can see that a file has an ACL applied to it when you run `ls -l` and see the `+` character appended to the permissions on the left most column.

To remove permissions
```
setfacl -x <user> <file>
```

### Quotas
Limiting space that an individual user can use.

Quota's are disabled by default.  So you will need to install the Quota package and set them up in your fstab file. 

Example of adding quota to a volume:
```
$ unmount /mnt/<volume>
$ mount -o quota /dev/<volume> /mnt/<volume>
$ quotacheck -c /mnt/<volume>
$ edquota <user> 
```

`quotacheck` will create the user quota file at the root of the volume.

`edquota` will edit the quotas for a user. If you use the `-G` option you can specify a group to apply the quota to. 

Soft limit: system will give the user a warning but will continue to let them create data

Hard limit: the user can no longer user more data.

You can set these limits by editing the file with `edquota`.  You can set the number of blocks or use gb like `1GB`.  

To check if the quota is on:
```
quotaon -pa
```

To enable the quota:
```
quotaon /mnt/<volume>
```

To see the quota you have as a user:
`quota`. To see with human readable `quota -s`. 

You can test this by running `fallocate -l 500mb <filename>`. This will create a file that is 500mb.

To see reports on quota from the admin side: 
`repquota -a -s`
where `-a` shows all filesystems with quotas, and `-s` shows data in human readable format. 


## Troubleshooting
1. check logs and review error messages
2. `getfacl` helps with showing permissions of a file
3. consider group membership and path permissions.
4. moving a disk between machines can result in strange looking permissions
5. moving a file retains permissions.
6. SELinux or AppArmor can have extra policies.

## Backups
There are multiple approaches to building backups.  

1. backup locally or remotely?
2. what files do we backup?



## Resources
* [Basic How To](https://tldp.org/HOWTO/LVM-HOWTO/)
* [Raid and LVM](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/logical_volume_manager_administration/raid_volumes)
* [Creating an Encrypted LVM file system](https://www.linux.com/training-tutorials/how-full-encrypt-your-linux-system-lvm-luks/) <- protection for physical attacks on a server