Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
linux_wiki:logical_volume_management_lvm [2015/12/23 12:58] billdozor [Example 2: Move Data To A New Disk] |
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: | ||
---- | ---- | ||
- | ===== Add Disk ===== | ||
- | Add disk to the system either physically or virtually | + | ====== Expanding or New ====== |
+ | |||
+ | Whether expanding or creating new storage, the summarized steps are: | ||
+ | * Add virtual | ||
+ | * Scan SCSI Bus (if needed) | ||
+ | * Format the Disk with a partition | ||
+ | * Create physical volume using the new disk's partition | ||
---- | ---- | ||
- | ===== Re-Scan SCSI Bus ===== | + | ===== 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: | If adding a disk to a virtual machine and the new disk does not show up: | ||
+ | |||
+ | \\ | ||
+ | **One Liner Method**< | ||
+ | |||
+ | \\ | ||
+ | **Manual Method** | ||
* Find the host bus number< | * Find the host bus number< | ||
* Example return value:< | * Example return value:< | ||
Line 24: | Line 42: | ||
* Example from returned value< | * Example from returned value< | ||
* The hyphens represent controller, | * The hyphens represent controller, | ||
+ | |||
+ | ==== Expand Disk: Re-Scan SCSI Bus ==== | ||
+ | |||
+ | If you have expanded an existing disk, rescan the scsi bus<code bash> | ||
+ | |||
+ | Where "< | ||
---- | ---- | ||
Line 67: | 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 < | + | ====== Expanding Storage ====== |
- | Example: | + | Expanding storage steps. |
- | <code bash> | + | |
- | vgcreate vgstorage /dev/sdb1 | + | |
- | </ | + | |
- | ---- | + | ===== Expand: Summarized Steps ===== |
- | ===== Create | + | To expand storage for a logical volume, the summarized steps are: |
- | About: Logical volumes use the disk space made available via a volume | + | Performed above in " |
+ | * Add virtual | ||
+ | * Re-Scan SCSI Bus (if needed) | ||
+ | * Format the Disk with a partition | ||
+ | * Create physical | ||
- | lvcreate --size < | + | 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 | ||
- | </ | ||
- | |||
- | Example: Create a logical volume that uses 100% of the volume group free extents | ||
- | <code bash> | ||
- | lvcreate --extents 100%FREE --name lvbackups vgstorage | ||
- | </ | ||
---- | ---- | ||
- | ===== Create | + | ===== Extend |
- | About: So far, the only things being created were pretty much containers. Create | + | - Add the new physical disk to the existing volume group<code bash>vgextend vglocal |
- | + | ||
- | mkfs -t < | + | |
- | + | ||
- | Example: | + | |
- | <code bash> | + | |
- | mkfs -t ext4 -L Backups | + | |
- | </ | + | |
---- | ---- | ||
- | ===== Mount the logical volume by uuid ===== | + | ===== Extend Logical Volume |
- | ==== Find the UUID with blkid ==== | + | - Extend |
- | <code bash> | + | - **Option 1(Preferred)**: |
- | 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-lvbackups: UUID=" | + | lvextend --resizefs / |
- | </ | + | - Option 2: Give the logical volume all of the space from the newly added physical volume< |
+ | | ||
+ | - Option 4: Give the logical volume all of the free space available to the volume group (potentially across multiple physical volumes)< | ||
- | ==== Add entry to /etc/fstab by UUID ==== | + | ==== Extending a Logical Swap Volume |
- | <code bash> | + | |
- | vim / | + | |
- | UUID=d89744ad-133c-452b-98d8-9480ef18fe77 / | + | |
- | </ | + | |
- | ==== Mount the fstab entries and check to see if it mounted with df ==== | + | Extending a swap volume is slightly different, as the " |
- | <code bash> | + | - Example: Specify the total size of the logical swap volume<code bash> |
- | mount -a | + | - Disable all swap< |
- | df -h | + | - Create a new swap area, overwriting the original< |
- | </ | + | - Enable swap< |
+ | - Verify< | ||
---- | ---- | ||
- | ===== 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 |
+ | - Logical Volume: <code bash> | ||
+ | - Volume Group: <code bash> | ||
+ | - Physical Volumes: <code bash> | ||
- | ==== 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 | + | If you would like to see what logical volumes reside where, a nice command to view physical extent to logical volume mappings |
- | === 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 " |
- | <code bash> | + | - Grow the file system |
- | gdisk -l /dev/sda | + | - Ext2/3/4<code bash> |
- | + | - XFS<code bash> | |
- | Partition table scan: | + | |
- | | + | |
- | BSD: not present | + | |
- | APM: not present | + | |
- | GPT: present | + | |
- | + | ||
- | Number | + | |
- | | + | |
- | | + | |
- | </ | + | |
---- | ---- | ||
- | === The LVM Structure | + | ===== Cleanup ===== |
- | The LVM structure was setup to allow for a good amount | + | If you have just moved all of the data of a logical volume off of an old physical volume, the 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 |
- | + | ||
- | 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 | + | |
- | / | + | |
</ | </ | ||
+ | ==== 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 | + | |
- | vgroot | + | |
</ | </ | ||
+ | ==== 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 | + | |
- | lvhome vgroot -wi-ao--- 50.00g | + | |
- | lvroot vgroot -wi-ao--- 30.00g | + | |
- | lvswap vgroot -wi-ao--- | + | |
- | lvtmp vgroot -wi-ao--- | + | |
- | lvvar vgroot -wi-ao--- | + | |
</ | </ | ||
+ | ==== 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 |
- | 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> | + | / |
- | vgs | + | /dev/sdc: read failed after 0 of 4096 at 17179803648: |
- | | + | /dev/sdc: read failed after 0 of 4096 at 17179860992: |
- | | + | /dev/sdc: read failed after 0 of 4096 at 4096: Input/ |
- | </ | + | /dev/sdc1: read failed after 0 of 4096 at 17178755072: |
+ | /dev/sdc1: read failed after 0 of 4096 at 17178812416: | ||
+ | /dev/sdc1: read failed after 0 of 4096 at 0: Input/ | ||
+ | /dev/sdc1: read failed after 0 of 4096 at 4096: Input/ | ||
+ | PV VG Fmt | ||
+ | | ||
+ | / | ||
+ | / | ||
+ | * Fix by rebooting the system or<code bash> | ||
+ | * Where " | ||
---- | ---- | ||
- | == Partition Status | + | ====== Creating New Storage ====== |
- | Using df -h, we can see how each LVM partition is doing space wise: | + | ===== Create New: Summarized Steps ===== |
- | <code bash> | + | |
- | df -h | + | |
- | Filesystem | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | </ | + | |
- | ---- | + | To create new storage for a logical volume, the summarized steps are: |
- | === Extending Logical Volumes === | + | Performed above in " |
+ | * 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 / | + | |
- | </ | + | |
- | 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 / | + | |
- | </ | + | |
- | 3) Check the volume status | + | vgcreate < |
- | Check lvs and df: | + | Example: |
<code bash> | <code bash> | ||
- | lvs | + | vgcreate vgstorage |
- | 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 | + | |
- | /dev/mapper/ | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
</ | </ | ||
- | |||
- | **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 ==== | + | ===== Create a Logical Volume ===== |
- | **Situation**: | + | |
- | * Add the new disk to the VM. | + | About: Logical volumes use the disk space made available via a volume group. |
- | * Rescan the SCSI bus if disk does not show up. | + | |
- | * Format it and create | + | |
- | * 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 |
- | + | ||
- | === 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 | + | Example: Create a 100G logical volume |
<code bash> | <code bash> | ||
- | pvmove | + | lvcreate |
</ | </ | ||
- | Extend the logical volume that was moved, only using the new disk' | + | Example: Create a logical volume that uses 100% of the volume group free extents |
<code bash> | <code bash> | ||
- | lvextend / | + | lvcreate |
</ | </ | ||
- | |||
- | Resize the file system (ext2,3,4 only. See other filesystem' | ||
- | <code bash> | ||
- | resize2fs / | ||
- | </ | ||
- | |||
- | **=>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 new, follow these steps instead. | + | About: So far, the 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> |
- | <code bash> | + | |
- | pvmove | + | |
- | </code> | + | |
- | * Ensure old physical volume has no space in use | + | Example: |
<code bash> | <code bash> | ||
- | pvs -o +pv_used | + | mkfs -t ext4 -L Backups / |
</ | </ | ||
- | * Remove old physical volume from volume group | + | ---- |
- | vgreduce vgname olddisk | + | |
- | <code bash> | + | |
- | vgreduce vgroot /dev/sdb1 | + | |
- | </ | + | |
- | * Remove old disk from being a physical | + | ===== Mount the logical |
- | pvremove olddisk | + | |
+ | ==== Find the UUID with blkid ==== | ||
<code bash> | <code bash> | ||
- | pvremove | + | blkid |
+ | /dev/mapper/ | ||
</ | </ | ||
- | * Extend logical volume | + | ==== Add entry to /etc/fstab by UUID ==== |
<code bash> | <code bash> | ||
- | lvextend | + | vim / |
+ | UUID=d89744ad-133c-452b-98d8-9480ef18fe77 | ||
</ | </ | ||
- | * Resize | + | ==== Mount the fstab entries and check to see if it mounted with df ==== |
<code bash> | <code bash> | ||
- | resize2fs / | + | mount -a |
+ | df -h | ||
</ | </ | ||
- | |||
- | * Remove disk from VM using the vendor' | ||
----- | ----- | ||
- | ===== 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 373: | Line 324: | ||
---- | ---- | ||
- | ===== LVM Snapshots ===== | + | ====== LVM Snapshots |
LVM allows you to " | LVM allows you to " |