linux_wiki:logical_volume_management_lvm

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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/shrinking space without shutting a system down. 
- 
-**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 
-</code> 
- 
-==== Create a New Empty GUID Partition Table ==== 
- 
-Once in gdisk: 
-  * Use option "o" to create a new empty GUID Partition table. (GPT) 
-  * "w" to write table entries 
- 
-==== Create the LVM Partition ==== 
- 
-  * Go back into gdisk. (gdisk /dev/sdb) 
-  * "n" to create a new partition 
-    * Choose the defaults for partition number, start, and end blocks to fill the entire disk 
-    * Use code "8e00" for Linux LVM 
-  * "w" to write changes and exit 
- 
----- 
- 
-===== Create a Physical Volume ===== 
- 
-About: Think of physical volumes as your "virtual disks" to use in LVM. 
- 
-Create a physical volume using the parition that the LVM is on. 
- 
-pvcreate <device1-path> [device2-path] [deviceX-path] 
- 
-Example: 
-<code bash> 
-pvcreate /dev/sdb1 
-</code> 
- 
----- 
- 
-===== Create a Volume Group ===== 
- 
-About: Volume groups aggregate physical volumes into a usable pool of disk space. 
- 
-vgcreate <vgname> <physical-device> 
- 
-Example: 
-<code bash> 
-vgcreate vgstorage /dev/sdb1 
-</code> 
- 
----- 
- 
-===== Create a Logical Volume ===== 
- 
-About: Logical volumes use the disk space made available via a volume group. 
- 
-lvcreate --size <size> --name <lv-name> <vg-name> 
- 
-Example: Create a 100G logical volume 
-<code bash> 
-lvcreate --size 100G --name lvbackups vgstorage 
-</code> 
- 
-Example: Create a logical volume that uses 100% of the volume group free extents 
-<code bash> 
-lvcreate --extents 100%FREE --name lvbackups vgstorage 
-</code> 
----- 
- 
-===== 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 <fstype> -L <label> /dev/mapper/<vgname>-<lvname> 
- 
-Example: 
-<code bash> 
-mkfs -t ext4 -L Backups /dev/mapper/vgstorage-lvbackups 
-</code> 
- 
----- 
- 
-===== Mount the logical volume by uuid ===== 
- 
-==== Find the UUID with blkid ==== 
-<code bash> 
-blkid 
-/dev/mapper/vgstorage-lvbackups: UUID="d89744ad-133c-452b-98d8-9480ef18fe77" TYPE="ext4" 
-</code> 
- 
-==== Add entry to /etc/fstab by UUID ==== 
-<code bash> 
-vim /etc/fstab 
-UUID=d89744ad-133c-452b-98d8-9480ef18fe77 /opt/backups ext4 defaults 0 2 
-</code> 
- 
-==== Mount the fstab entries and check to see if it mounted with df ==== 
- 
-<code bash> 
-mount -a 
-df -h 
-</code> 
- 
----- 
- 
-===== 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**: I have a laptop with a 300GB hard drive and installed Linux. 
- 
-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  Start (sector)    End (sector)  Size       Code  Name 
-              2048          513766   249.9 MiB   8300  Linux filesystem 
-            514048       625142414   297.8 GiB   8E00  Linux LVM 
-</code> 
- 
----- 
- 
-=== 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         VG     Fmt  Attr PSize   PFree   
-  /dev/sda2  vgroot lvm2 a--  297.84g 197.84g 
-</code> 
- 
----- 
- 
-== Volume Group == 
- 
-One volume group was created and the physical disk added to it. 
- 
-<code bash> 
-vgs 
-  VG     #PV #LV #SN Attr   VSize   VFree   
-  vgroot       0 wz--n- 297.84g 197.84g 
-</code> 
- 
----- 
- 
-== 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     VG     Attr      LSize   
-  lvhome vgroot -wi-ao--- 50.00g                                            
-  lvroot vgroot -wi-ao--- 30.00g                                            
-  lvswap vgroot -wi-ao---  4.00g                                            
-  lvtmp  vgroot -wi-ao---  8.00g                                            
-  lvvar  vgroot -wi-ao---  8.00g   
-</code> 
- 
-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   VSize   VFree   
-  vgroot       0 wz--n- 297.84g 197.84g 
-</code> 
- 
----- 
- 
-== Partition Status == 
- 
-Using df -h, we can see how each LVM partition is doing space wise: 
-<code bash> 
-df -h 
-Filesystem                 Size  Used Avail Use% Mounted on 
-/dev/mapper/vgroot-lvroot   30G  2.7G   26G  10% / 
-/dev/sda1                  238M   95M  128M  43% /boot 
-/dev/mapper/vgroot-lvvar   7.8G  534M  6.9G   8% /var 
-/dev/mapper/vgroot-lvtmp   7.8G   19M  7.4G   1% /tmp 
-/dev/mapper/vgroot-lvhome   50G   16G   31G  34% /home 
-</code> 
- 
----- 
- 
-=== 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 /dev/mapper/vgroot-lvhome 
-</code> 
- 
-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 /dev/mapper/vgroot-lvhome 
-</code> 
- 
-3) Check the volume status 
- 
-Check lvs and df: 
-<code bash> 
-lvs 
-  LV     VG     Attr      LSize  Pool Origin Data%  Move Log Copy%  Convert 
-  lvhome vgroot -wi-ao--- 60.00g                                            
-  lvroot vgroot -wi-ao--- 30.00g                                            
-  lvswap vgroot -wi-ao---  4.00g                                            
-  lvtmp  vgroot -wi-ao---  8.00g                                            
-  lvvar  vgroot -wi-ao---  8.00g   
- 
-df -h 
-Filesystem                 Size  Used Avail Use% Mounted on 
-/dev/mapper/vgroot-lvroot   30G  2.7G   26G  10% / 
-/dev/sda1                  238M   95M  128M  43% /boot 
-/dev/mapper/vgroot-lvvar   7.8G  534M  6.9G   8% /var 
-/dev/mapper/vgroot-lvtmp   7.8G   19M  7.4G   1% /tmp 
-/dev/mapper/vgroot-lvhome   59G   16G   41G  29% /home 
-</code> 
- 
-**Note: **If you do not resize your file system and only extend the logical volume, you will see the logical partition from the "lvs" output as larger, but it will be the old size in df -h. 
- 
----- 
- 
-==== Example 2: Move Data To A New Disk ==== 
-**Situation**: You have a disk that has run out of space. You want to add a new, larger disk to a virtual machine, and delete the old disk to reclaim the resources. 
- 
-  * 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 
-</code> 
- 
-Extend the logical volume that was moved, only using the new disk's free space 
-<code bash> 
-lvextend /dev/mapper/vgroot-lvhome /dev/sdc1 
-</code> 
- 
-Resize the file system (ext2,3,4 only. See other filesystem's man pages for syntax) 
-<code bash> 
-resize2fs /dev/mapper/vgroot-lvhome 
-</code> 
- 
-**=>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 
-</code> 
- 
-  * Ensure old physical volume has no space in use 
-<code bash> 
-pvs -o +pv_used 
-</code> 
- 
-  * Remove old physical volume from volume group 
-vgreduce vgname olddisk 
-<code bash> 
-vgreduce vgroot /dev/sdb1 
-</code> 
- 
-  * Remove old disk from being a physical volume 
-pvremove olddisk 
-<code bash> 
-pvremove /dev/sdb1 
-</code> 
- 
-  * Extend logical volume 
-<code bash> 
-lvextend --extents +100%FREE /dev/mapper/vgroot-lvhome 
-</code> 
- 
-  * Resize the file system (ext2,3,4 only. See other filesystem's man pages for syntax) 
-<code bash> 
-resize2fs /dev/mapper/vgroot-lvhome 
-</code> 
- 
-  * Remove disk from VM using the vendor's virtual machine manager. 
- 
------ 
- 
-===== 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 
-</code> 
- 
-Rename a Logical Volume (lvrename vgname lv-oldname lv-newname) 
-<code bash> 
-lvrename vgredusb lvbackupmirror lvredbackups 
-</code> 
- 
----- 
- 
-===== LVM Snapshots ===== 
- 
-LVM allows you to "freeze" the metadata of files. This allows for things like rsync to complete successfully even if files it is trying to sync are modified during the operation. 
- 
- 
-{TODO} 
  • linux_wiki/logical_volume_management_lvm.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)