Tuesday, May 26, 2009

Linux Logical Volume Management

Linux Logical Volume Management

September 16th, 2008 · by Xander Tan

Logical Volume Management (LVM)

Logical Volume Management is a method of managing disk drives and similar mass-storage devices. In Linux, a logical volume manager perform this task for the Linux kernel. The manager is capable to allocate space on the mass-storage devices, dynamically move or resize the space while it is being used. The manager controls physical volumes, physical extends, logical extends, volume groups and logical volumes in order to perform this flexible space allocation.

A physical volume (PV) may be a hard disk partition, a RAID device or a SAN. A PV is split into chunks called physical extends (PEs). PEs are mapped one-to-one to logical extents (LEs) or are mirrored onto one LE. A volume group (VG) is a pool of LEs. LEs in a volume group can be concatenated into Logical Volumes (LVs). And finally these LVs are useable as a raw block devices just as disk partitions are; file systems can be created on an LV.

A VG can grow its storage pool by adding in new PVs or shrink by reducing PVs. An LV can grow by adding in more LEs or shrink by returning LEs to the pool.

Linux LVM

In Linux operating systems, a logical volume manager can:

  • Resize VGs online by adding in or reducing PVs.
  • Resize LVs online by adding in or reducing LEs.
  • Stripe whole or parts of LV across multiple PVs, in a fashion similar to RAID0.
  • Mirror whole or parts of LV, in a fashion similar to RAID1.
  • Move online logical volumes between PVs.

In the 2.6 Linux kernels, the LVM is implemented in terms of the device mapper, creating virtual block devices and mapping their space onto other block devices. While LVM user-space tools manipulate these mappings and reconstruct their state from on-disk metadata upon each invocation.

Implementation

Redhat Enterprise Linux (RHEL) 5 provides an LVM user-space tool to implement logical volumes. The good news is that the tool runs on Terminal; thus, the implementation logical volumes in RHEL 5 can be performed via SSL connection remotely.

# lvm
lvm>

There are a number of LVM commands available in this tool.

  Available lvm commands:
Use 'lvm help ' for more information

dumpconfig Dump active configuration
formats List available metadata formats
help Display help for commands
lvchange Change the attributes of logical volume(s)
lvconvert Change logical volume layout
lvcreate Create a logical volume
lvdisplay Display information about a logical volume
lvextend Add space to a logical volume
lvmchange With the device mapper, this is obsolete and does nothing.
lvmdiskscan List devices that may be used as physical volumes
lvmsadc Collect activity data
lvmsar Create activity report
lvreduce Reduce the size of a logical volume
lvremove Remove logical volume(s) from the system
lvrename Rename a logical volume
lvresize Resize a logical volume
lvs Display information about logical volumes
lvscan List all logical volumes in all volume groups
pvchange Change attributes of physical volume(s)
pvresize Resize physical volume(s)
pvck Check the consistency of physical volume(s)
pvcreate Initialize physical volume(s) for use by LVM
pvdata Display the on-disk metadata for physical volume(s)
pvdisplay Display various attributes of physical volume(s)
pvmove Move extents from one physical volume to another
pvremove Remove LVM label(s) from physical volume(s)
pvs Display information about physical volumes
pvscan List all physical volumes
segtypes List available segment types
vgcfgbackup Backup volume group configuration(s)
vgcfgrestore Restore volume group configuration
vgchange Change volume group attributes
vgck Check the consistency of volume group(s)
vgconvert Change volume group metadata format
vgcreate Create a volume group
vgdisplay Display volume group information
vgexport Unregister volume group(s) from the system
vgextend Add physical volumes to a volume group
vgimport Register exported volume group with system
vgmerge Merge volume groups
vgmknodes Create the special files for volume group devices in /dev
vgreduce Remove physical volume(s) from a volume group
vgremove Remove volume group(s)
vgrename Rename a volume group
vgs Display information about volume groups
vgscan Search for all volume groups
vgsplit Move physical volumes into a new volume group
version Display software and driver version information

Example

The example was performed using HP Integrity Server with RHEL 5.1 (Tikanga). The target hard disk was /dev/cciss/c0d1 that had one partition /dev/cciss/c0d1p1. The purpose was to create a logical volume on the partition. Notice that a hard disk partition like /dev/hdb1 or /dev/sdb1 may also be a target hard disk to implement logical volume.

  • Add a new physical volume

    Show the existing physical volumes.
    lvm> pvs
    PV VG Fmt Attr PSize PFree
    /dev/cciss/c0d0p2 VolGroup00 lvm2 a- 68.22G 0

    Add a new physical volume.

    lvm> pvcreate /dev/cciss/c0d1p1
    Physical volume "/dev/cciss/c0d1p1" successfully created

    Show the current physical volumes.

    lvm> pvs
    PV VG Fmt Attr PSize PFree
    /dev/cciss/c0d0p2 VolGroup00 lvm2 a- 68.22G 0
    /dev/cciss/c0d1p1 lvm2 -- 67.77G 67.77G
  • Create a new volume group

    Show the existing volume groups.
    lvm> vgs
    VG #PV #LV #SN Attr VSize VFree
    VolGroup00 1 2 0 wz--n- 68.22G 0

    Create a new volume group.

    lvm> vgcreate VolGroup01 /dev/cciss/c0d1p1
    Volume group "VolGroup01" successfully created

    Show the existing volume groups and physical volumes.

    lvm> vgs
    VG #PV #LV #SN Attr VSize VFree
    VolGroup00 1 2 0 wz--n- 68.22G 0
    VolGroup01 1 0 0 wz--n- 67.77G 67.77G
    lvm> pvs
    PV VG Fmt Attr PSize PFree
    /dev/cciss/c0d0p2 VolGroup00 lvm2 a- 68.22G 0
    /dev/cciss/c0d1p1 VolGroup01 lvm2 a- 67.77G 67.77G
  • Create a new logical volume

    Show the existing logical volumes.
    lvm> lvs
    LV VG Attr LSize Origin Snap% Move Log Copy%
    LogVol00 VolGroup00 -wi-ao 66.28G
    LogVol01 VolGroup00 -wi-ao 1.94G

    Create a new logical volume.

    lvm> lvcreate --size 30000 --name LogVol02 VolGroup01
    Rounding up size to full physical extent 29.31 GB
    Logical volume "LogVol02" created

    Show the current logical volumes and physical volumes.

    lvm> lvs
    LV VG Attr LSize Origin Snap% Move Log Copy%
    LogVol00 VolGroup00 -wi-ao 66.28G
    LogVol01 VolGroup00 -wi-ao 1.94G
    LogVol02 VolGroup01 -wi-a- 29.31G
    lvm> pvs
    PV VG Fmt Attr PSize PFree
    /dev/cciss/c0d0p2 VolGroup00 lvm2 a- 68.22G 0
    /dev/cciss/c0d1p1 VolGroup01 lvm2 a- 67.75G 38.44G
  • Use the new logical volume

    Format the new logical volume.
    # mkfs -t ext3 /dev/VolGroup01/LogVol02
    mke2fs 1.39 (29-May-2006)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    3842720 inodes, 7684096 blocks
    384204 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=4294967296
    235 block groups
    32768 blocks per group, 32768 fragments per group
    16352 inodes per group
    Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000

    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done

    This filesystem will be automatically checked every 38 mounts or
    180 days, whichever comes first. Use tune2fs -c or -i to override.

    Mount the new logical volume.

    # mount /dev/VolGroup01/LogVol02 /mnt/storage01
    # df
    Filesystem 1K-blocks Used Available Use% Mounted on
    /dev/mapper/VolGroup00-LogVol00
    67323516 3138844 60709628 5% /
    /dev/cciss/c0d0p1 102182 9230 92952 10% /boot/efi
    tmpfs 1998224 0 1998224 0% /dev/shm
    /dev/mapper/VolGroup01-LogVol02
    30254032 176200 28541016 1% /mnt/storage01

Posted in Linux | No Comments »


Memory and Linux Swap Space

August 5th, 2008 · by Xander Tan

Linux Virtual Memory

Operating systems divide memory (physical) into chunks of smaller memory called pages. Due to the limited size of memory, operating systems have to store these pages in memory and a secondary storage, i.e. hard disk. Swapping is the activity of transferring these pages in between memory and hard disk space. In Linux, this pre-configured hard disk space is called Linux swap space and the combined size of the memory and the swap space is the total amount of virtual memory.

When Linux runs multiple programs, of which total memory footprint is larger than the available memory, it uses this swapping mechanism. It temporarily stores chunks of memory into its swap space while loads the others into the memory and continuously swaps the chunks in the swap space with the ones in the memory. This means that all programs are loaded into the virtual memory and properly executed.

Linux Swap Space

Linux possesses two types of swap space, i.e. a swap partition and a swap file. A swap partition is an independent section of the hard disk used solely for swapping; no other files can reside there. While a swap file is a file that resides amongst system and data files. With the 2.6 Linux kernel, swap files are just as fast as swap partitions.

Linux virtually supports unlimited number of swap spaces, each of which can be assigned a priority. When it needs to swap pages out of memory, it uses the highest-priority free space; when it finds multiple spaces with the same priority, it uses them in a similar fashion as RAID0 (access them in parallel). In order to gain higher performance, priorities must be assigned effectively. For example, the fastest spaces possess the highest priority and swaps located on the same physical disk should not be used in parallel.

Advantages and Disadvantages

Advantages of swapping:

  1. It allows systems to execute multiple processes in virtual memory larger than available memory. Linux swaps pages in between memory and swap space. This virtually gives the operating systems a larger memory footprint than what it physically has.
  2. It increases the efficiency of memory utilization. Linux swaps out less used pages (in the memory) and allocates the freed memory to processes that need memory immediately. For example, Linux can swap out the pages (in the memory), which are used by a process during its initialization and then never used again, and allocate the freed memory for other processes.

Disadvantages of swapping:

  1. It requires a long time to perform swapping. Hard disk is much slower than memory; memory and hard disk speed is measured in nanoseconds and milliseconds respectively. This means accessing a hard disk is tens of thousands times slower than accessing physical memory; the more swapping occurs, the slower the system will be. In the case of trashing (excessive swapping), the system is struggling to free memory, only adding in more memory will help.

Memory and Swap Space Size

To see the amount of memory (physical) installed:

# grep MemTotal /proc/meminfo

The output will look something like this:

MemTotal:      1035776 kB

To see the amount of swap space configured:

# grep SwapTotal /proc/meminfo

The output will look something like this:

SwapTotal:      393552 kB

To see the utilization and availability of both memory and swap space:

# free

The output will look something like:

             total       used       free     shared    buffers     cached
Mem: 1035776 970104 65672 0 226544 618284
-/+ buffers/cache: 125276 910500
Swap: 393552 0 393552

To see more details about swap space configuration:

# swapon -s

The output will look something like this:

Filename                                Type            Size    Used    Priority
/dev/hda5 partition 393552 0 -1

The ‘Type’ field indicates that this is a partition swap space rather than a file. The ‘Filename’ shows that the configured partition is /dev/hda5. The ‘Size’ tells us the size of the partition in kilobytes. The ‘Used’ field shows that the swap space is currently unused. ‘Priority’ tells Linux which swap space to use first, of course, it doesn’t really matter since it has only one swap space.

Swap Space Optimization

Based on the discussion above regarding zero swap space utilization, Linux is able to run without swap space. Linux will keep going even though it runs out virtual memory, normally, its kernel OutOfMemory (OOM) will kick in and try to kill the “right process” and thereby freeing up memory. There are many guidelines recommending the size of swap space to be allocated for a certain memory size. For example:

RAM Swap Space
Between 1024 MB and 2048 MB 1.5 times the size of RAM
Between 2049 MB and 8192 MB Equal to the size of RAM
More than 8192 MB 0.75 times the size of RAM

And let us see the setting of my Linux:

             total       used       free     shared    buffers     cached
Mem: 1035776 970104 65672 0 226544 618284
-/+ buffers/cache: 125276 910500
Swap: 393552 0 393552

Memory (physical) is 1,035,776 kB (~1GB) and Swap Space is 393,552 kB (~400MB). In conclusion: hell yeah! The table can be a guide but it doesn’t have to be followed. Predict the Linux utilization and set the swap accordingly.

No comments:

Post a Comment