linux_wiki:logical_volume_management_lvm

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
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] (current)
Line 6: Line 6:
  
 **Checklist** **Checklist**
-  * The hard disk being formatted is not in use/blank. These steps will erase all data on the target disk.+  * Distro(s): Any 
 +  * Other: The hard disk being formatted is not in use/blank. These steps will erase all data on the target disk
 + 
 +---- 
 + 
 +====== Expanding or New ====== 
 + 
 +Whether expanding or creating new storage, the summarized steps are: 
 +  * Add virtual disk to VM 
 +  * Scan SCSI Bus (if needed) 
 +  * Format the Disk with a partition 
 +  * Create physical volume using the new disk's partition 
 + 
 +---- 
 + 
 +===== Add/Expand Disk ===== 
 + 
 +Add or expand the disk to the system either physically or virtually if a VM. 
 + 
 +---- 
 + 
 +==== Add Disk: Scan SCSI Bus ==== 
 + 
 +If adding a disk to a virtual machine and the new disk does not show up: 
 + 
 +\\ 
 +**One Liner Method**<code bash>echo "- - -" > /sys/class/scsi_host/$(grep mpt /sys/class/scsi_host/host?/proc_name | grep -o -E '(host[0-9])')/scan</code> 
 + 
 +\\ 
 +**Manual Method** 
 +  * Find the host bus number<code bash>grep mpt /sys/class/scsi_host/host?/proc_name</code> 
 +    * Example return value:<code bash>/sys/class/scsi_host/host2/proc_name:mptspi</code> 
 +    * "host2" is the important value 
 +  * Used the returned host# to scan the SCSI bus<code bash>echo "- - -" > /sys/class/scsi_host/host#/scan</code> 
 +    * Example from returned value<code bash>echo "- - -" > /sys/class/scsi_host/host2/scan</code> 
 +  * The hyphens represent controller,channel,lun, so – - – indicates all controllers, all channels, and all luns should be scanned. 
 + 
 +==== Expand Disk: Re-Scan SCSI Bus ==== 
 + 
 +If you have expanded an existing disk, rescan the scsi bus<code bash>echo 1 > /sys/class/scsi_device/<device>/rescan</code> 
 + 
 +Where "<device>" is your disk such as "sda".
  
 ---- ----
Line 50: Line 91:
 ---- ----
  
-===== Create a Volume Group =====+**Continue on to either Expanding Storage or Creating New Storage.**
  
-About: Volume groups aggregate physical volumes into a usable pool of disk space.+----
  
-vgcreate <vgname> <physical-device>+====== Expanding Storage ======
  
-Example: +Expanding storage steps.
-<code bash> +
-vgcreate vgstorage /dev/sdb1 +
-</code>+
  
-----+===== Expand: Summarized Steps =====
  
-===== Create Logical Volume =====+To expand storage for logical volume, the summarized steps are:
  
-AboutLogical volumes use the disk space made available via a volume group.+Performed above in "Expanding or New": 
 +  * Add virtual disk to VM 
 +  * Re-Scan SCSI Bus (if needed) 
 +  * Format the Disk with partition 
 +  * Create physical volume using the new disk's partition
  
-lvcreate --size <size> --name <lv-name> <vg-name>+Remaining Steps: 
 +  * Extend the volume group using the new physical volume 
 +  * Extend the logical volume 
 +  * Verify new LVM layout 
 +  * Grow the file system 
 +  * Cleanup (if a pvmove of the logical volume was done to a new disk) 
 +    * Verify old physical volume is not used 
 +    * Reduce the volume group by removing the physical volume 
 +    * Remove the physical volume LVM label 
 +    * Delete virtual disk from virtual management UI
  
-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 =====+===== Extend the Volume Group =====
  
-About: So far, the only things being created were pretty much containers. Create the actual file system below. +  - Add the new physical disk to the existing volume group<code bash>vgextend vglocal /dev/sdb1</code>
- +
-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 =====+===== Extend Logical Volume =====
  
-==== Find the UUID with blkid ==== +  - Extend the logical volume that needs the space (/home will be used in this example) 
-<code bash> +    - **Option 1(Preferred)**: Move the physical extents of the logical volume to the new disk, so all of it resides on the same VMDK, and then use all of that new disk's space. 
-blkid +      - There may be a performance hit if a logical volume is spanned across multiple VMDKs, so this method is preferred.<code bash>pvmove --name lvhome /dev/sda2 /dev/sdb1 
-/dev/mapper/vgstorage-lvbackupsUUID="d89744ad-133c-452b-98d8-9480ef18fe77" TYPE="ext4" +lvextend --resizefs /dev/vglocal/lvhome /dev/sdb1</code> 
-</code>+    - Option 2: Give the logical volume all of the space from the newly added physical volume<code bash>lvextend --resizefs --extents +100%PVS /dev/vglocal/lvhome /dev/sdb1</code> 
 +    Option 3Give the logical volume a specific amount of additional free space from a specific physical volume<code bash>lvextend --resizefs --size +10G /dev/vglocal/lvhome /dev/sdb1</code> 
 +    - Option 4: Give the logical volume all of the free space available to the volume group (potentially across multiple physical volumes)<code bash>lvextend --resizefs --extents +100%FREE /dev/vglocal/lvhome</code>
  
-==== Add entry to /etc/fstab by UUID ==== +==== Extending a Logical Swap Volume ====
-<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 ====+Extending a swap volume is slightly different, as the "--resizefs" flag will not work.
  
-<code bash> +  - Example: Specify the total size of the logical swap volume<code bash>lvextend --size 8G /dev/vglocal/lvswap /dev/sdb1</code
-mount -a +  - Disable all swap<code bash>swapoff -a</code> 
-df -h +  Create a new swap area, overwriting the original<code bash>mkswap /dev/vglocal/lvswap</code> 
-</code>+  - Enable swap<code bash>swapon -a</code> 
 +  - Verify<code bash>swapon -s</code>
  
 ---- ----
  
-===== Real World Examples =====+===== Verify New LVM Layout =====
  
-It is sometimes difficult to predict how much space a particular partition will need. With LVM, your paritions can grow with your needs.+  - Verify LVM Allocation 
 +    - Logical Volume: <code bash>lvs</code> 
 +    - Volume Group: <code bash>vgs</code> 
 +    - Physical Volumes: <code bash>pvs</code>
  
-==== Example 1: Logical Volume Extending ==== 
  
-**Situation**: I have a laptop with a 300GB hard drive and installed Linux.+**Logical Volume Location(s) on Physical Volumes**
  
-A /boot partition is the only partition that can currently not be LVM.+If you would like to see what logical volumes reside where, a nice command to view physical extent to logical volume mappings is:<code bash>pvdisplay -m</code>
  
-=== How the disk is formatted ===+----
  
-The disk (/dev/sda) was formatted with a GUID Paritition Table (GPT).+===== Grow File System =====
  
-It has two paritions, a Linux Filesystem (to be used for /boot) and a Linux LVM.+**Skip if you used the "--resizefs" flag to lvextend above.**
  
-<code bash> +  - Grow the file system 
-gdisk -/dev/sda +    - Ext2/3/4<code bash>resize2fs /dev/vglocal/lvhome</code
- +    XFS<code bash>xfs_growfs /dev/vglocal/lvhome</code> 
-Partition table scan: +  - Verify filesystem space<code bash>df -h</code>
-  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 ===+===== Cleanup =====
  
-The LVM structure was setup to allow for good amount of room in each paritionbut enough free disk space left for any of them to grow.+If you have just moved all of the data of logical volume off of an old physical volumethe old physical volume can go away if it is no longer in use.
  
 +Examples below: You have just moved everything off of /dev/sdc1 to a new disk and can now remove the old disk (/dev/sdc).
  
-== Physical Volume == +==== Verify Old Physical Volume Not Used ====
- +
-One physical volume was created from /dev/sdb2 (the Linux LVM formatted partition from above).+
  
 <code bash> <code bash>
-pvs +pvs -o +pv_used
-  PV         VG     Fmt  Attr PSize   PFree   +
-  /dev/sda2  vgroot lvm2 a--  297.84g 197.84g+
 </code> </code>
 +==== Reduce Volume Group ====
  
----- +vgreduce vgname olddisk
- +
-== Volume Group == +
- +
-One volume group was created and the physical disk added to it. +
 <code bash> <code bash>
-vgs +vgreduce vglocal /dev/sdc1
-  VG     #PV #LV #SN Attr   VSize   VFree   +
-  vgroot       0 wz--n- 297.84g 197.84g+
 </code> </code>
 +==== Remove Physical Volume ====
  
----- +pvremove olddisk
- +
-== 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> <code bash>
-lvs +pvremove /dev/sdc1
-  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> </code>
 +==== Delete Virtual Disk ====
  
-As you can see, the partitions are using up only 100 GB of the 297.84 GB total available for the volume group.+Login to the virtualization user interface and delete the old disk from inventory.
  
-This leaves 197.84 GB of space that can be given to any of the partions. +\\ 
- +**Errors During LVM Commands** 
-This information can been seen by going back to the volume group show command: +  * If you see errors during LVM commands like this after a device delete from vmware<code bash>root@llsrlscd01 home $ pvs 
-<code bash> +  /dev/sdc: read failed after 0 of 4096 at 0: Input/output error 
-vgs +  /dev/sdc: read failed after 0 of 4096 at 17179803648: Input/output error 
-  VG     #PV #LV #SN Attr   VSize   VFree   +  /dev/sdc: read failed after 0 of 4096 at 17179860992: Input/output error 
-  vgroot   1     0 wz--n- 297.84g 197.84g +  /dev/sdc: read failed after 0 of 4096 at 4096: Input/output error 
-</code>+  /dev/sdc1: read failed after 0 of 4096 at 17178755072: Input/output error 
 +  /dev/sdc1: read failed after 0 of 4096 at 17178812416: Input/output error 
 +  /dev/sdc1: read failed after 0 of 4096 at 0: Input/output error 
 +  /dev/sdc1: read failed after 0 of 4096 at 4096: Input/output error 
 +  PV         VG      Fmt  Attr PSize   PFree 
 +  /dev/sda2  vglocal lvm2 a--   19.50g 3.50g 
 +  /dev/sdd1  vglocal lvm2 a--  250.00g    0 
 +  /dev/sde1  vglocal lvm2 a--   40.00g    0</code> 
 +    * Fix by rebooting the system or<code bash>echo 1 > /sys/block/sdX/device/delete</code> 
 +      * Where "sdX" is the device, such as 'sdc' in the above example.
  
 ---- ----
  
-== Partition Status ==+====== Creating New Storage ======
  
-Using df -h, we can see how each LVM partition is doing space wise: +===== Create NewSummarized Steps =====
-<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>+
  
-----+To create new storage for a logical volume, the summarized steps are:
  
-=== Extending Logical Volumes ===+Performed above in "Expanding or New": 
 +  * Add virtual disk to VM 
 +  * Re-Scan SCSI Bus (if needed) 
 +  * Format the Disk with a partition 
 +  * Create physical volume using the new disk's partition
  
-Say that I have run out of space in my /home partition. Good thing I'm using LVM; I can easily make /home larger.+Remaining Steps: 
 +  * Create a volume group 
 +  * Create a logical volume 
 +  * Create a file system on the logical volume 
 +  * Mount the logical volume
  
-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 +===== Create a Volume Group =====
-<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.) +About: Volume groups aggregate physical volumes into a usable pool of disk space.
-<code bash> +
-resize2fs /dev/mapper/vgroot-lvhome +
-</code>+
  
-3) Check the volume status+vgcreate <vgname> <physical-device>
  
-Check lvs and df:+Example:
 <code bash> <code bash>
-lvs +vgcreate vgstorage /dev/sdb1
-  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> </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 ==== +===== Create a Logical Volume =====
-**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. +About: Logical volumes use the disk space made available via a volume group.
-  * Format it and create 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:+
  
-----+lvcreate --size <size> --name <lv-name> <vg-name>
  
-=== Move Logical Volume Only === +Example: Create a 100G logical volume
-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> <code bash>
-pvmove -n lvhome /dev/sdb1 /dev/sdc1+lvcreate --size 100G --name lvbackups vgstorage
 </code> </code>
  
-Extend the logical volume that was moved, only using the new disk'free space+Example: Create a logical volume that uses 100% of the volume group free extents
 <code bash> <code bash>
-lvextend /dev/mapper/vgroot-lvhome /dev/sdc1+lvcreate --extents 100%FREE --name lvbackups vgstorage
 </code> </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 ===+===== Create the file system on the logical volume =====
  
-If moving all data from an old physical volume to newfollow these steps instead.+About: So farthe only things being created were pretty much containers. Create the actual file system below.
  
-pvmove olddisk newdisk +mkfs -t <fstype> -L <label> /dev/mapper/<vgname>-<lvname>
-<code bash> +
-pvmove /dev/sdb1 /dev/sdc1 +
-</code>+
  
-  * Ensure old physical volume has no space in use+Example:
 <code bash> <code bash>
-pvs -o +pv_used+mkfs -t ext4 -L Backups /dev/mapper/vgstorage-lvbackups
 </code> </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 +===== Mount the logical volume by uuid ===== 
-pvremove olddisk+ 
 +==== Find the UUID with blkid ====
 <code bash> <code bash>
-pvremove /dev/sdb1+blkid 
 +/dev/mapper/vgstorage-lvbackups: UUID="d89744ad-133c-452b-98d8-9480ef18fe77" TYPE="ext4"
 </code> </code>
  
-  * Extend logical volume+==== Add entry to /etc/fstab by UUID ====
 <code bash> <code bash>
-lvextend --extents +100%FREE /dev/mapper/vgroot-lvhome+vim /etc/fstab 
 +UUID=d89744ad-133c-452b-98d8-9480ef18fe77 /opt/backups ext4 defaults 0 2
 </code> </code>
  
-  * Resize the file system (ext2,3,4 only. See other filesystem's man pages for syntax)+==== Mount the fstab entries and check to see if it mounted with df ==== 
 <code bash> <code bash>
-resize2fs /dev/mapper/vgroot-lvhome+mount -
 +df -h
 </code> </code>
- 
-  * Remove disk from VM using the vendor's virtual machine manager. 
  
 ----- -----
  
-===== LVM Renaming =====+====== 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. We don't always get a name right the first time. Luckily, LVM allows for easy renaming of logical volumes and volume groups.
Line 355: Line 324:
 ---- ----
  
-===== LVM Snapshots =====+====== 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. 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.
  • linux_wiki/logical_volume_management_lvm.1446735551.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)