Differences
This shows you the differences between two versions of the page.
linux_wiki:logical_volume_management_lvm [2015/11/05 09:59] billdozor [Example 1: Logical Volume Extending] |
linux_wiki:logical_volume_management_lvm [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Logical Volume Management (LVM) ====== | ||
- | **General Information** | ||
- | |||
- | LVM provides a way to create virtual disks and containers in order to provide flexibility with parition sizes and extending/ | ||
- | |||
- | **Checklist** | ||
- | * The hard disk being formatted is not in use/blank. These steps will erase all data on the target disk. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Format the disk ===== | ||
- | |||
- | /dev/sdb is used in this example. | ||
- | |||
- | Use gdisk in order for GPT instead of MBR. (seriously, don't use fdisk...let MBR die already) | ||
- | <code bash> | ||
- | gdisk /dev/sdb | ||
- | </ | ||
- | |||
- | ==== Create a New Empty GUID Partition Table ==== | ||
- | |||
- | Once in gdisk: | ||
- | * Use option " | ||
- | * " | ||
- | |||
- | ==== Create the LVM Partition ==== | ||
- | |||
- | * Go back into gdisk. (gdisk /dev/sdb) | ||
- | * " | ||
- | * Choose the defaults for partition number, start, and end blocks to fill the entire disk | ||
- | * Use code " | ||
- | * " | ||
- | |||
- | ---- | ||
- | |||
- | ===== Create a Physical Volume ===== | ||
- | |||
- | About: Think of physical volumes as your " | ||
- | |||
- | Create a physical volume using the parition that the LVM is on. | ||
- | |||
- | pvcreate < | ||
- | |||
- | Example: | ||
- | <code bash> | ||
- | pvcreate /dev/sdb1 | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Create a Volume Group ===== | ||
- | |||
- | About: Volume groups aggregate physical volumes into a usable pool of disk space. | ||
- | |||
- | vgcreate < | ||
- | |||
- | Example: | ||
- | <code bash> | ||
- | vgcreate vgstorage /dev/sdb1 | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Create a Logical Volume ===== | ||
- | |||
- | About: Logical volumes use the disk space made available via a volume group. | ||
- | |||
- | lvcreate --size < | ||
- | |||
- | Example: Create a 100G logical volume | ||
- | <code bash> | ||
- | lvcreate --size 100G --name lvbackups vgstorage | ||
- | </ | ||
- | |||
- | Example: Create a logical volume that uses 100% of the volume group free extents | ||
- | <code bash> | ||
- | lvcreate --extents 100%FREE --name lvbackups vgstorage | ||
- | </ | ||
- | ---- | ||
- | |||
- | ===== Create the file system on the logical volume ===== | ||
- | |||
- | About: So far, the only things being created were pretty much containers. Create the actual file system below. | ||
- | |||
- | mkfs -t < | ||
- | |||
- | Example: | ||
- | <code bash> | ||
- | mkfs -t ext4 -L Backups / | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Mount the logical volume by uuid ===== | ||
- | |||
- | ==== Find the UUID with blkid ==== | ||
- | <code bash> | ||
- | blkid | ||
- | / | ||
- | </ | ||
- | |||
- | ==== Add entry to /etc/fstab by UUID ==== | ||
- | <code bash> | ||
- | vim /etc/fstab | ||
- | UUID=d89744ad-133c-452b-98d8-9480ef18fe77 / | ||
- | </ | ||
- | |||
- | ==== Mount the fstab entries and check to see if it mounted with df ==== | ||
- | |||
- | <code bash> | ||
- | mount -a | ||
- | df -h | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Real World Examples ===== | ||
- | |||
- | It is sometimes difficult to predict how much space a particular partition will need. With LVM, your paritions can grow with your needs. | ||
- | |||
- | ==== Example 1: Logical Volume Extending ==== | ||
- | |||
- | **Situation**: | ||
- | |||
- | A /boot partition is the only partition that can currently not be LVM. | ||
- | |||
- | === How the disk is formatted === | ||
- | |||
- | The disk (/dev/sda) was formatted with a GUID Paritition Table (GPT). | ||
- | |||
- | It has two paritions, a Linux Filesystem (to be used for /boot) and a Linux LVM. | ||
- | |||
- | <code bash> | ||
- | gdisk -l /dev/sda | ||
- | |||
- | Partition table scan: | ||
- | MBR: protective | ||
- | BSD: not present | ||
- | APM: not present | ||
- | GPT: present | ||
- | |||
- | Number | ||
- | | ||
- | | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | === The LVM Structure === | ||
- | |||
- | The LVM structure was setup to allow for a good amount of room in each parition, but enough free disk space left for any of them to grow. | ||
- | |||
- | |||
- | == Physical Volume == | ||
- | |||
- | One physical volume was created from /dev/sdb2 (the Linux LVM formatted partition from above). | ||
- | |||
- | <code bash> | ||
- | pvs | ||
- | PV | ||
- | / | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | == Volume Group == | ||
- | |||
- | One volume group was created and the physical disk added to it. | ||
- | |||
- | <code bash> | ||
- | vgs | ||
- | VG #PV #LV #SN Attr | ||
- | vgroot | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | == Logical Volumes == | ||
- | |||
- | The following partitions each got their own logical volume: | ||
- | * /home | ||
- | * / | ||
- | * swap | ||
- | * /tmp | ||
- | * /var | ||
- | |||
- | The sizes for each can be seen below: | ||
- | <code bash> | ||
- | lvs | ||
- | LV | ||
- | lvhome vgroot -wi-ao--- 50.00g | ||
- | lvroot vgroot -wi-ao--- 30.00g | ||
- | lvswap vgroot -wi-ao--- | ||
- | lvtmp vgroot -wi-ao--- | ||
- | lvvar vgroot -wi-ao--- | ||
- | </ | ||
- | |||
- | As you can see, the partitions are using up only 100 GB of the 297.84 GB total available for the volume group. | ||
- | |||
- | This leaves 197.84 GB of space that can be given to any of the partions. | ||
- | |||
- | This information can been seen by going back to the volume group show command: | ||
- | <code bash> | ||
- | vgs | ||
- | VG #PV #LV #SN Attr | ||
- | vgroot | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | == Partition Status == | ||
- | |||
- | Using df -h, we can see how each LVM partition is doing space wise: | ||
- | <code bash> | ||
- | df -h | ||
- | Filesystem | ||
- | / | ||
- | / | ||
- | / | ||
- | / | ||
- | / | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | === Extending Logical Volumes === | ||
- | |||
- | Say that I have run out of space in my /home partition. Good thing I'm using LVM; I can easily make /home larger. | ||
- | |||
- | 1) Extend the logical volume container. You can either give an exact size or an "add this much" size. | ||
- | |||
- | Example: Extend lvhome by 10 GB | ||
- | <code bash> | ||
- | lvextend --size +10G / | ||
- | </ | ||
- | |||
- | 2) Grow the file system to fill the new size of the logical volume. (If the size parameter is not specified, it will default to the size of the logical volume.) | ||
- | <code bash> | ||
- | resize2fs / | ||
- | </ | ||
- | |||
- | 3) Check the volume status | ||
- | |||
- | Check lvs and df: | ||
- | <code bash> | ||
- | lvs | ||
- | LV | ||
- | lvhome vgroot -wi-ao--- 60.00g | ||
- | lvroot vgroot -wi-ao--- 30.00g | ||
- | lvswap vgroot -wi-ao--- | ||
- | lvtmp vgroot -wi-ao--- | ||
- | lvvar vgroot -wi-ao--- | ||
- | |||
- | df -h | ||
- | Filesystem | ||
- | / | ||
- | / | ||
- | / | ||
- | / | ||
- | / | ||
- | </ | ||
- | |||
- | **Note: **If you do not resize your file system and only extend the logical volume, you will see the logical partition from the " | ||
- | |||
- | ---- | ||
- | |||
- | ==== Example 2: Move Data To A New Disk ==== | ||
- | **Situation**: | ||
- | |||
- | * Add the new disk to the VM. | ||
- | * Format it and create a LVM partition. | ||
- | * Use pvcreate to make it a physical volume. | ||
- | * Use vgextend to add the new physical volume to the same volume group that contains the disk you want to replace. | ||
- | * Move the data from the old disk, to the new: | ||
- | |||
- | ---- | ||
- | |||
- | === Move Logical Volume Only === | ||
- | If you only need to move 1 logical volume to the new disk and not everything, follow these steps. | ||
- | |||
- | pvmove -n lvname olddisk newdisk | ||
- | <code bash> | ||
- | pvmove -n lvhome /dev/sdb1 /dev/sdc1 | ||
- | </ | ||
- | |||
- | Extend the logical volume that was moved, only using the new disk's free space | ||
- | <code bash> | ||
- | lvextend / | ||
- | </ | ||
- | |||
- | Resize the file system (ext2,3,4 only. See other filesystem' | ||
- | <code bash> | ||
- | resize2fs / | ||
- | </ | ||
- | |||
- | **=>Stop Here if only moving a logical volume** | ||
- | |||
- | ---- | ||
- | |||
- | === Move All === | ||
- | |||
- | If moving all data from an old physical volume to new, follow these steps instead. | ||
- | |||
- | pvmove olddisk newdisk | ||
- | <code bash> | ||
- | pvmove /dev/sdb1 /dev/sdc1 | ||
- | </ | ||
- | |||
- | * Ensure old physical volume has no space in use | ||
- | <code bash> | ||
- | pvs -o +pv_used | ||
- | </ | ||
- | |||
- | * Remove old physical volume from volume group | ||
- | vgreduce vgname olddisk | ||
- | <code bash> | ||
- | vgreduce vgroot /dev/sdb1 | ||
- | </ | ||
- | |||
- | * Remove old disk from being a physical volume | ||
- | pvremove olddisk | ||
- | <code bash> | ||
- | pvremove /dev/sdb1 | ||
- | </ | ||
- | |||
- | * Extend logical volume | ||
- | <code bash> | ||
- | lvextend --extents +100%FREE / | ||
- | </ | ||
- | |||
- | * Resize the file system (ext2,3,4 only. See other filesystem' | ||
- | <code bash> | ||
- | resize2fs / | ||
- | </ | ||
- | |||
- | * Remove disk from VM using the vendor' | ||
- | |||
- | ----- | ||
- | |||
- | ===== LVM Renaming ===== | ||
- | |||
- | We don't always get a name right the first time. Luckily, LVM allows for easy renaming of logical volumes and volume groups. | ||
- | |||
- | Rename a Volume Group (vgrename vg-oldname vg-newname) | ||
- | <code bash> | ||
- | vgrename vgbackupmirror vgredusb | ||
- | </ | ||
- | |||
- | Rename a Logical Volume (lvrename vgname lv-oldname lv-newname) | ||
- | <code bash> | ||
- | lvrename vgredusb lvbackupmirror lvredbackups | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== LVM Snapshots ===== | ||
- | |||
- | LVM allows you to " | ||
- | |||
- | |||
- | {TODO} |