Difference between revisions of "Logical Volume Management"

From CDOT Wiki
Jump to: navigation, search
(Filesystems on LVs)
 
(20 intermediate revisions by 6 users not shown)
Line 30: Line 30:
 
** <code>vgs</code> - display compact information about all volume groups
 
** <code>vgs</code> - display compact information about all volume groups
 
** <code>vgdisplay</code> - display verbose information about all volume groups
 
** <code>vgdisplay</code> - display verbose information about all volume groups
** <code>vgcreate</code> - creates a new volume group. Usually, only one volume group is used per system, but additional ones can be created in special circumstances. You must specify at least one PV to create a LV.
+
** <code>vgcreate</code> - creates a new volume group. Usually, only one volume group is used per system, but additional ones can be created in special circumstances. You must specify at least one PV to create a VG.
** <code>vgextend</code> - adds additional PVs to an LV.
+
** <code>vgextend</code> - adds additional PVs to a VG.
** <code>vgreduce</code> - removes PVs from an LV.
+
** <code>vgreduce</code> - removes PVs from a VG.
** <code>vgremove</code> - destroys an LV.
+
** <code>vgremove</code> - destroys a VG.
 
* LV management
 
* LV management
 
** <code>lvs</code> - display compact information about all logical volumes
 
** <code>lvs</code> - display compact information about all logical volumes
Line 44: Line 44:
 
For the full syntax and details of these commands, see the appropriate manpage.
 
For the full syntax and details of these commands, see the appropriate manpage.
  
{{Admon/note|Using LVM in Fedora's Rescue Mode|The Fedora installation DVD has a "rescue mode" which may be used to fix problems on a previously-installed system -- useful if (for example) you have deleted or messed up a file that is necessary to successfully boot the system. In rescue mode, only a single LVM command is available: <code>lvm</code. To use any of the lvm commands, you can type <code>lvm</code> followed by the name of the command you wish to execute. Alternately, typing <code>lvm</code> by itself will start the LVM shell, and you can then type any of the lvm commands directly. To use non-LVM commands, exit from the LVM shell.}}
+
{{Admon/note|Using LVM in Fedora's Rescue Mode|The Fedora installation DVD has a "rescue mode" which may be used to fix problems on a previously-installed system -- useful if (for example) you have deleted or messed up a file that is necessary to successfully boot the system. In rescue mode, only a single LVM command is available: <code>lvm</code>. To use any of the lvm commands, you can type <code>lvm</code> followed by the name of the command you wish to execute. Alternately, typing <code>lvm</code> by itself will start the LVM shell, and you can then type any of the lvm commands directly. To use non-LVM commands, exit from the LVM shell.}}
  
== LVM Device Names ==[[Category:Linux]][[Category:Fedora]][[Category:OPS235]]
+
== LVM Device Names ==
 +
[[Category:Linux]][[Category:Fedora]][[Category:OPS235]][[Category:OPS335]]
  
 
PVs are referenced by their normal block device name, such as /dev/sda1. VGs and LVs are referred to by names which are assigned when they are created.
 
PVs are referenced by their normal block device name, such as /dev/sda1. VGs and LVs are referred to by names which are assigned when they are created.
Line 52: Line 53:
 
Fedora's installation system, ''Anaconda'', will set up LVM by default. Anaconda will name the main volume group <code>lv_''hostname''</code>, where ''hostname'' is the host name assigned to the system during installation; LVs will be named <code>lv_''desc''</code> where ''desc'' is a description of the mountpoint (such as "root" for the LV containing the filesystem mounted as <code>/</code>, and "home" for the LV containing the filesystem mounted as <code>/home</code>). However, you can choose to use any names you want; it's a good idea to avoid names that could be confused with other devices (such as <code>sdb1</code> or <code>mouse0</code>).
 
Fedora's installation system, ''Anaconda'', will set up LVM by default. Anaconda will name the main volume group <code>lv_''hostname''</code>, where ''hostname'' is the host name assigned to the system during installation; LVs will be named <code>lv_''desc''</code> where ''desc'' is a description of the mountpoint (such as "root" for the LV containing the filesystem mounted as <code>/</code>, and "home" for the LV containing the filesystem mounted as <code>/home</code>). However, you can choose to use any names you want; it's a good idea to avoid names that could be confused with other devices (such as <code>sdb1</code> or <code>mouse0</code>).
  
LVs are found at two points in the /dev/directory:
+
LVs are found at three points in the /dev/directory:
 
# <code>/dev/mapper/''vgname''-''lvname''</code>
 
# <code>/dev/mapper/''vgname''-''lvname''</code>
# <code>/dev/''vgname''/''lvname''</code> - this is a symbolic link to the directory above
+
# <code>/dev/''vgname''/''lvname''</code>
 +
# <code>/dev/dm-''N'' - the names above are actually links to this device node. ''dm'' refers to the ''device mapper'', which is the underlying technology that enables LVM.
  
 
{{Admon/note|LV names in Rescue Mode|Note that the LV symbolic links are not available in Fedora's rescue mode -- you must use the entry in <code>/dev/mapper</code>.}}
 
{{Admon/note|LV names in Rescue Mode|Note that the LV symbolic links are not available in Fedora's rescue mode -- you must use the entry in <code>/dev/mapper</code>.}}
Line 63: Line 65:
 
* as a virtual machine's storage device
 
* as a virtual machine's storage device
  
Many LVM commands directly accept the LV name (<code>vg_muskoka</code>) or the VG name and LV name separated by a slash (<code>vg_muskoka/lv_home</code>), but commands such as <code>mkfs</code> which are used with block devices require the full device name (<code>/dev/vg_muskoka/lv_home</code>)
+
Many LVM commands directly accept the LV name </code>(<code>vg_muskoka</code>) or the VG name and LV name separated by a slash (<code>vg_muskoka/lv_home</code>), but commands such as <code>mkfs</code> which are used with block devices require the full device name (<code>/dev/vg_muskoka/lv_home</code>)
  
 
== LVM and Booting ==
 
== LVM and Booting ==
Line 91: Line 93:
 
  mount /dev/vg_muskoka/lv_archive /archive
 
  mount /dev/vg_muskoka/lv_archive /archive
 
   
 
   
  # Edit /etc/fstab to have the filesystem mounted automatically at boot time
+
  # Edit /etc/fstab to have the file-system mounted automatically at boot time
 +
# (refer to  [https://wiki.archlinux.org/index.php/fstab https://wiki.archlinux.org/index.php/fstab] for additional information)
 +
# Here is an example of issuing a command to add the mount point to the bottom of the /etc/fstab file:<br /><br />echo "/dev/vg_muskoka/lv_archive /archive ext3 defaults 1 2" >>/etc/fstab
  
Notice that the LV serves as a container for the filesystem, but otherwise the two are independent. If you increase the size of the logical volume, you must then increase the size of the filesystem so that it fills the logical volume:
+
Notice that the LV serves as a container for the file-system, but otherwise the two are independent. If you increase the size of the logical volume, you must then increase the size of the file-system so that it fills the logical volume:
  
 
  # Add 5G to the logical volume lv_home:
 
  # Add 5G to the logical volume lv_home:
  lvextend vg_muskoka/lv_home --size +5G
+
  lvextend vg_muskoka/lv_home -r --size +5G
 
   
 
   
# Resize the filesystem to fill the LV --
 
# note that the filesystem can be mounted
 
# while this is performed:
 
resize2fs /dev/vg_muskoka/lv_main
 
  
 
When reducing the size of an LV-based filesystem, it's critical that you shrink the filesystem (the contents) before you shrink the LV (the container) -- otherwise you may lose data, and the filesystem will be damaged. Here is an example:
 
When reducing the size of an LV-based filesystem, it's critical that you shrink the filesystem (the contents) before you shrink the LV (the container) -- otherwise you may lose data, and the filesystem will be damaged. Here is an example:
  
 
  # Reduce the filesystem in lv_home by 5G
 
  # Reduce the filesystem in lv_home by 5G
  resize2fs /dev/vg_muskoka/lv_home -5G
+
  resize2fs /dev/vg_muskoka/lv_home 50G
 
   
 
   
 
  # Shrink the LV:
 
  # Shrink the LV:
  lvreduce vg_muskoka/lv_hone --size -5G
+
  lvreduce vg_muskoka/lv_home --size -5G
 +
# or specify an absolute size of 50G<br /><br />Note: If you are experiencing problems mounting the reduced LV, it may be due to the difference in extent size<br /> between the LINV and the filesystem layer. To fix this problem,<br /> remove the LV, create, format the LV, and then resize only using the lvreduce command with the -r option.<br /> This automatically reduces the filesystem , and the LV.<br /> Refer to this example below"<br /><br />lvreduce -r vg_muskoka/lv_home --size 50G
  
{{Admon/important|Check your arguments!|lvreduce, lvextend, and resize2fs can all take absolute or relative size arguments. For example, <code>-5G</code> means "reduce the size by five gigabytes", but <code>5G</code> means "make the size five gigabytes". If you have a 100G LV, the command <code>lvresize vg_muskoka/lv_home --size -5G</code> would shrink it to 95G, but accidentally typing <code>lvresize vg_muskoka/lv_home --size 5G</code> would shrink it to 5G. This would cause data loss if you have previously resized the filesystem to 95G. Remember to check your commands carefully.}}
+
{{Admon/important|Check your arguments!|lvreduce and lvextend can both take absolute or relative size arguments. For example, <code>-5G</code> means "reduce the size by five gigabytes", but <code>5G</code> means "make the size five gigabytes". If you have a 100G LV, the command <code>lvresize vg_muskoka/lv_home --size -5G</code> would shrink it to 95G, but accidentally typing <code>lvresize vg_muskoka/lv_home --size 5G</code> would shrink it to 5G. This would cause data loss if you have previously resized the filesystem to 95G. Remember to check your commands carefully.}}
 +
 
 +
{{Admon/important|Non-integer sizes|The LVM commands accept fractional sizes such as 1.5G, but resize2fs does not -- so you must use the next-smallest unit. For example, 1.5G corresponds to 1536M (1.5 * 1024M).}}
  
 
Remember the order in which the filesystem and LV size must be adjusted:
 
Remember the order in which the filesystem and LV size must be adjusted:
Line 122: Line 125:
  
 
  # Create a logical volume:
 
  # Create a logical volume:
  lvcreate vg_muskoka --name vg_swap1 --size 2G
+
  lvcreate vg_muskoka --name lv_swap1 --size 2G
 
   
 
   
 
  # Write the swap space metadata (format it)
 
  # Write the swap space metadata (format it)
Line 155: Line 158:
  
 
If a system is installed with all of the VG space in use, it will be necessary to add a PV or reduce the size of an LV in order to add any more LVs or to grow one of the existing LVs. '''It's therefore a much better idea to create small LVs initially, leaving some unused space in the VG which can be assigned as needed'''.
 
If a system is installed with all of the VG space in use, it will be necessary to add a PV or reduce the size of an LV in order to add any more LVs or to grow one of the existing LVs. '''It's therefore a much better idea to create small LVs initially, leaving some unused space in the VG which can be assigned as needed'''.
 +
 +
=== Adding a Physical Volume ===
 +
 +
To add a physical volume to the system:
 +
# Add the disk (if not already attached) -- for this example, let's assume the disk is <code>/dev/sdb</code>
 +
# Create a partition with <code>fdisk</code> - let's assume the partition is <code>/dev/sdb1</code>
 +
# Mark the partition as usable by LVM: <code>pvcreate /dev/''sdb1''</code>
 +
# Add the partition to your volume group: <code>vgextend vg_main /dev/''sdb1''</code>

Latest revision as of 12:41, 13 February 2014

Logical Volume Management

Partitioning enables a disk or other block storage device, such as /dev/sda, to be divided into multiple virtual block devices, such as /dev/sda1, /dev/sda2, and /dev/sda3. These are very simple virtual block devices which are directly tied to a specific physical device.

Partitions are not particularly flexible -- they cannot:

  • be easily resized
  • span across multiple disks
  • be easily moved from one disk to another

Logical volume managment is a more flexible way of managing storage: the physical block devices are separated from the logical (virtual) block devices, so that they can be independently manipulated.

When using LVM, a block devices providing storage space is called a physical volume (PV). One or more physical volumes provide storage capacity for a storage pool, called a volume group. Space in a volume group is allocated to virtual block devices called logical volumes.

Using LVM, it is possible to add and remove physical volumes at any time, providing that sufficient space is left in the volume group to hold all of the logical volumes. Likewise, it is possible to extend or reduce the size of logical volumes, to delete logical volumes, and to create new logical volumes, providing that there is enough space in the volume group to hold them.

This enables you to do things such as add a disk drive, replace a failing disk, and re-allocate space between filesystems, usually without stopping the system.

LVM Commands

These are the basic LVM commands:

  • PV management
    • pvs - display compact information about all physical volumes
    • pvdisplay - display verbose information about all physical volumes
    • pvcreate - sets up a block device for use as a physical volume by writing some metadata to the device. After running this command, vgcreate or vgextend is used to add the new physical volume to a volume group
    • pvmove - moves data between PVs. Usually used to move data off a PV before running vgreduce to remove it from the volume group
    • pvremove - erases the PV metadata on the device, ensuring that LVM will not attempt to use it.
  • VG management
    • vgs - display compact information about all volume groups
    • vgdisplay - display verbose information about all volume groups
    • vgcreate - creates a new volume group. Usually, only one volume group is used per system, but additional ones can be created in special circumstances. You must specify at least one PV to create a VG.
    • vgextend - adds additional PVs to a VG.
    • vgreduce - removes PVs from a VG.
    • vgremove - destroys a VG.
  • LV management
    • lvs - display compact information about all logical volumes
    • lvdisplay - display verbose information about all logical volumes
    • lvcreate - creates an LV using space in a VG
    • lvextend - adds more space to an LV
    • lvreduce - removes space from an LV
    • lvremove - removes a LV

For the full syntax and details of these commands, see the appropriate manpage.

Note.png
Using LVM in Fedora's Rescue Mode
The Fedora installation DVD has a "rescue mode" which may be used to fix problems on a previously-installed system -- useful if (for example) you have deleted or messed up a file that is necessary to successfully boot the system. In rescue mode, only a single LVM command is available: lvm. To use any of the lvm commands, you can type lvm followed by the name of the command you wish to execute. Alternately, typing lvm by itself will start the LVM shell, and you can then type any of the lvm commands directly. To use non-LVM commands, exit from the LVM shell.

LVM Device Names

PVs are referenced by their normal block device name, such as /dev/sda1. VGs and LVs are referred to by names which are assigned when they are created.

Fedora's installation system, Anaconda, will set up LVM by default. Anaconda will name the main volume group lv_hostname, where hostname is the host name assigned to the system during installation; LVs will be named lv_desc where desc is a description of the mountpoint (such as "root" for the LV containing the filesystem mounted as /, and "home" for the LV containing the filesystem mounted as /home). However, you can choose to use any names you want; it's a good idea to avoid names that could be confused with other devices (such as sdb1 or mouse0).

LVs are found at three points in the /dev/directory:

  1. /dev/mapper/vgname-lvname
  2. /dev/vgname/lvname
  3. /dev/dm-N - the names above are actually links to this device node. dm refers to the device mapper, which is the underlying technology that enables LVM.
Note.png
LV names in Rescue Mode
Note that the LV symbolic links are not available in Fedora's rescue mode -- you must use the entry in <code>/dev/mapper</code>.

A logical volume may be used like any other block device:

  • to hold a filesystem
  • for swap space
  • as a virtual machine's storage device

Many LVM commands directly accept the LV name (vg_muskoka) or the VG name and LV name separated by a slash (vg_muskoka/lv_home), but commands such as mkfs which are used with block devices require the full device name (/dev/vg_muskoka/lv_home)

LVM and Booting

System firmware, whether BIOS or EFI, does not know about LVM and therefore it is not possible to boot from a logical volume. As a consequence, Linux systems which use LVM must have a separate /boot partition containing the kernel, initrc, grub configuration, and any other files necessary for booting.

A GUI for LVM administration: system-config-lvm

Fedora provides the system-config-lvm tool for LVM management. This tool is a Python script that provides easy access to the most common LVM operations. However, when something goes wrong with an LVM-based system, the GUI tools may not be available (either because of booting into runlevel s or 1, using rescue mode, or because the LV containing the GUI tools is not accessible). Therefore it's essential that any system administrator using LVM know how to manage it from the command line.

Using LVM

Filesystems on LVs

Creating a filesystem on a logical volume is straightforward. Here is an example:

# Create a 50G logical volume named lv_archive in the volume group vg_muskoka:
lvcreate vg_muskoka --name lv_archive --size 50G

# Create an ext4 filesystem inside the new LV:
mkfs -t ext4 /dev/vg_muskoka/lv_archive

# Create a new mountpoint:
mkdir /archive

# Mount the filesystem in the LV:
mount /dev/vg_muskoka/lv_archive /archive

# Edit /etc/fstab to have the file-system mounted automatically at boot time
# (refer to  https://wiki.archlinux.org/index.php/fstab for additional information)
# Here is an example of issuing a command to add the mount point to the bottom of the /etc/fstab file:

echo "/dev/vg_muskoka/lv_archive /archive ext3 defaults 1 2" >>/etc/fstab

Notice that the LV serves as a container for the file-system, but otherwise the two are independent. If you increase the size of the logical volume, you must then increase the size of the file-system so that it fills the logical volume:

# Add 5G to the logical volume lv_home:
lvextend vg_muskoka/lv_home -r --size +5G

When reducing the size of an LV-based filesystem, it's critical that you shrink the filesystem (the contents) before you shrink the LV (the container) -- otherwise you may lose data, and the filesystem will be damaged. Here is an example:

# Reduce the filesystem in lv_home by 5G
resize2fs /dev/vg_muskoka/lv_home 50G

# Shrink the LV:
lvreduce vg_muskoka/lv_home --size -5G
# or specify an absolute size of 50G

Note: If you are experiencing problems mounting the reduced LV, it may be due to the difference in extent size
between the LINV and the filesystem layer. To fix this problem,
remove the LV, create, format the LV, and then resize only using the lvreduce command with the -r option.
This automatically reduces the filesystem , and the LV.
Refer to this example below"

lvreduce -r vg_muskoka/lv_home --size 50G
Important.png
Check your arguments!
lvreduce and lvextend can both take absolute or relative size arguments. For example, -5G means "reduce the size by five gigabytes", but 5G means "make the size five gigabytes". If you have a 100G LV, the command lvresize vg_muskoka/lv_home --size -5G would shrink it to 95G, but accidentally typing lvresize vg_muskoka/lv_home --size 5G would shrink it to 5G. This would cause data loss if you have previously resized the filesystem to 95G. Remember to check your commands carefully.
Important.png
Non-integer sizes
The LVM commands accept fractional sizes such as 1.5G, but resize2fs does not -- so you must use the next-smallest unit. For example, 1.5G corresponds to 1536M (1.5 * 1024M).

Remember the order in which the filesystem and LV size must be adjusted:

  • Growing - increase the LV before the filesystem
  • Shrinking - decrease the filesystem before the LV

Swap on LVs

Creating swap space on an LV is straightforward:

# Create a logical volume:
lvcreate vg_muskoka --name lv_swap1 --size 2G

# Write the swap space metadata (format it)
mkswap /dev/vg_muskoka/lv_swap1

# Activate the swap space
swapon /dev/vg_muskoka/lv_swap1
# Add to /etc/fstab have the swap space activated at boot

You can grow swap space easily:

# Deactivate the swap space
swapoff /dev/vg_muskoka/lv_swap1

# Grow (or shrink) the swap space
lvextend vg_mukoka/lv_swap1 --size +1G

# Format the LV for swap, so the new size takes effect
mkswap /dev/vg_muskoka/lv_swap1

# Activate the swap space
swapon /dev/vg_muskoka/lv_swap1

However, this can only be done when the swap space is not in use (i.e., you're not using enough memory to need swap). If you need to add swap space while the existing swap is in use, you can create additional swap LVs and activate them.

Reducing filesystem size

The size of a logical volume may be altered while it is in use. However, most current filesystems may only be grown while mounted -- to shrink a filesystem, it must be unmounted.

It's quite difficult to unmount /home -- you must be logged in non-graphically as root. It's even harder to umount / -- you have to boot from another disk, such as a live disc or the rescue mode of the installation disc.

If a system is installed with all of the VG space in use, it will be necessary to add a PV or reduce the size of an LV in order to add any more LVs or to grow one of the existing LVs. It's therefore a much better idea to create small LVs initially, leaving some unused space in the VG which can be assigned as needed.

Adding a Physical Volume

To add a physical volume to the system:

  1. Add the disk (if not already attached) -- for this example, let's assume the disk is /dev/sdb
  2. Create a partition with fdisk - let's assume the partition is /dev/sdb1
  3. Mark the partition as usable by LVM: pvcreate /dev/sdb1
  4. Add the partition to your volume group: vgextend vg_main /dev/sdb1