-->

Using the LVM in Linux

The Linux LVM was developed by Heinz Mauelshagen and released to the Linux community in 1998. It allows you to manage a complete logical volume management environment in Linux using simple command line commands.

Using the LVM in Linux
Using the LVM in Linux


Two versions of Linux LVM are available:

LVM1:
The original LVM package released in 1998 and available in only the 2.4 Linux kernels. It provides only basic logical volume management features.

LVM2:
An updated version of the LVM, available in the 2.6 Linux kernels. It provides additional features over the standard LVM1 features.

Most modern Linux distributions using the 2.6 kernel version or above provide support for LVM2. Besides the standard logical volume management features, LVM2 provides a few other nice things for you to use in your Linux system.

Taking a Snapshot

The original Linux LVM allows you to copy an existing logical volume to another device while the logical volume is active. This feature is called a snapshot. Snapshots are great for backing up important data that can’t be locked due to high availability requirements.

Traditional backup methods usually lock files as they’re being copied to the backup media. The snapshot allows you to continue running mission critical web or database servers while performing the copy. Unfortunately, LVM1 allows you to create only a readonly snapshot. After you create the snapshot, you can’t write to it.

LVM2 allows you to create a read-write snapshot of an active logical volume. With the read-write copy, you can remove the original logical volume and mount the snapshot as a replacement. This feature is great for fast fail-overs or for experimenting with applications that modify data that may need to be restored if something fails.

Striping

Another interesting feature that LVM2 provides is striping. With striping, a logical volume is created across multiple physical hard drives. When the Linux LVM writes a file to the logical volume, the data blocks in the file are spread across the multiple hard drives. Each successive block of data is written to the next hard drive.

Striping helps improve disk performance, because Linux can write the multiple data blocks for a file to the multiple hard drives simultaneously, rather than having to wait for a single hard drive to move the read/write head to different locations. This improvement also applies to reading sequentially accessed files, because the LVM can read data from the multiple hard drives simultaneously.

Note:LVM striping is not the same as RAID striping. LVM striping doesn’t provide a parity entry, which creates the fault-tolerant environment. In fact, LVM striping may increase the chance of a file being lost due to a hard drive failure. A single disk failure can result in multiple logical volumes being inaccessible.

Mirroring

Just because you install a filesystem using LVM doesn’t mean that things can’t still go wrong in the filesystem. Just as in a physical partition, LVM logical volumes are susceptible to power outages and disk crashes. After a filesystem becomes corrupt, there’s always a possibility that you won’t be able to recover it.

The LVM snapshot process provides some comfort knowing that you can create a backup copy of a logical volume at any time, but for some environments that may not be enough.

Systems that have lots of data changes, such as database servers, may store hundreds or thousands of records since the last snapshot.

A solution to this problem is the LVM mirror. A mirror is a complete copy of a logical volume that’s updated in real time. When you create the mirror logical volume, LVM synchronizes the original logical volume to the mirror copy. Depending on the size of the original logical volume, this may take some time to complete.

After the original synchronization is complete, LVM performs two writes for each write process in the filesystem — one to the main logical volume and one to the mirrored copy. As you can guess, this process does slow down write performance on the system. However, if the original logical volume should become corrupt for some reason,

you have a complete up-to-date copy at your fingertips!

Using the Linux LVM

Now that you’ve seen what the Linux LVM can do, this section discusses how to implement it to help organize the disk space on your system. The Linux LVM package only provides command line programs for creating and managing all the components in the logical volume management system. Some Linux distributions include graphical frontends to the command line commands, but for complete control of your LVM environment, it’s best to get comfortable working directly with the commands.

Defining Physical Volumes

The first step in the process is to convert the physical partitions on the hard drive into physical volume extents used by the Linux LVM. Our friend the fdisk command helps us here. After creating the basic Linux partition, you need to change the partition type using the t command:

  1. […]
  2. Command (m for help): t
  3. Selected partition 1
  4. Hex code (type L to list codes): 8e
  5. Changed system type of partition 1 to 8e (Linux LVM)
  6. Command (m for help): p
  7. Disk /dev/sdb: 5368 MB, 5368709120 bytes
  8. 255 heads, 63 sectors/track, 652 cylinders
  9. Units = cylinders of 16065 * 512 = 8225280 bytes
  10. Sector size (logical/physical): 512 bytes / 512 bytes
  11. I/O size (minimum/optimal): 512 bytes / 512 bytes
  12. Disk identifier: 0xa8661341
  13. Device Boot Start End Blocks Id System
  14. /dev/sdb1 1 262 2104483+ 8e Linux LVM
  15. Command (m for help): w
  16. The partition table has been altered!
  17. Calling ioctl() to re-read partition table.
  18. Syncing disks.
  19. $



The 8e partition type denotes that the partition will be used as part of a Linux LVM system and not as a direct filesystem, as you saw with the 83 partition type earlier.

Note
If the pvcreate command in the next step does not work for you, it’s most likely due to the LVM2 package not being installed by default. To install the package, use the package name lvm2 and see Chapter 9 for how to install software packages.

The next step is to use the partition to create the actual physical volume. That’s done using the pvcreate command. The pvcreate command defines the physical partition to use for the PV. It simply tags the partition as a physical volume in the Linux LVM system:

  1. $ sudo pvcreate /dev/sdb1
  2.  dev_is_mpath: failed to get device for 8:17
  3.  Physical volume “/dev/sdb1” successfully created
  4. $


Note
Don’t let the daunting message dev_is_mpath: failed to get device for 8:17 or similar messages frighten you. As long as you receive the successfully created message, all is well. The pvcreate command checks to see whether the partition is a multi-path (mpath) device. If it is not, it issues the daunting message.

You can use the pvdisplay command to display a list of physical volumes you’ve created if you’d like to see your progress along the way:

  1. $ sudo pvdisplay /dev/sdb1
  2.     “/dev/sdb1” is a new physical volume of “2.01 GiB”
  3.     – NEW Physical volume –
  4.     PV Name /dev/sdb1
  5.     VG Name
  6.     PV Size 2.01 GiB
  7.     Allocatable NO
  8.     PE Size 0
  9.     Total PE 0
  10.     Free PE 0
  11.     Allocated PE 0
  12.     PV UUID 0FIuq2-LBod-IOWt-8VeN-tglm-Q2ik-rGU2w7
  13. $


The pvdisplay command shows that /dev/sdb1 is now tagged as a PV. Notice, however, that in the output, the VG Name is blank. The PV does not yet belong to a volume group.

Creating Volume Groups

The next step in the process is to create one or more volume groups from the physical volumes. There are no set rules for how many volume groups you need to create for your system — you can add all the available physical volumes to a single volume group, or you can create multiple volume groups by combining different physical volumes.

To create the volume group from the command line, you need to use the vgcreate command. The vgcreate command requires a few command line parameters to define the volume group name, as well as the name of the physical volumes you’re using to create the volume group:

$ sudo vgcreate Vol1 /dev/sdb1
Volume group “Vol1” successfully created
$

That’s not all too exciting for output! If you’d like to see some details about the newly created volume group, use the vgdisplay command:

  1. $ sudo vgdisplay Vol1
  2.     – Volume group –
  3.     VG Name Vol1
  4.     System ID
  5.     Format lvm2
  6.     Metadata Areas 1
  7.     Metadata Sequence No 1
  8.     VG Access read/write
  9.     VG Status resizable
  10.     MAX LV 0
  11.     Cur LV 0
  12.     Open LV 0
  13.     Max PV 0
  14.     Cur PV 1
  15.     Act PV 1
  16.     VG Size 2.00 GiB
  17.     PE Size 4.00 MiB
  18.     Total PE 513
  19.     Alloc PE / Size 0 / 0
  20.     Free PE / Size 513 / 2.00 GiB
  21.     VG UUID oe4I7e-5RA9-G9ti-ANoI-QKLz-qkX4-58Wj6e
  22. $



This example creates a volume group named Vol1, using the physical volume created on the /dev/sdb1 partition.

Now that you have one or more volume groups created, you’re ready to create the logical volume.


Creating the Filesystem

After you run the lvcreate command, the logical volume exists but doesn’t have a filesystem. To do that, you need to use the appropriate command line program for the filesystem you want to create:

  1. $ sudo mkfs.ext4 /dev/Vol1/lvtest
  2. mke2fs 1.41.12 (17-May-2010)
  3. Filesystem label=
  4. OS type: Linux
  5. Block size=4096 (log=2)
  6. Fragment size=4096 (log=2)
  7. Stride=0 blocks, Stripe width=0 blocks
  8. 131376 inodes, 525312 blocks
  9. 26265 blocks (5.00%) reserved for the super user
  10. First data block=0
  11. Maximum filesystem blocks=541065216
  12. 17 block groups
  13. 32768 blocks per group, 32768 fragments per group
  14. 7728 inodes per group
  15. Superblock backups stored on blocks:
  16.         32768, 98304, 163840, 229376, 294912
  17. Writing inode tables: done
  18. Creating journal (16384 blocks): done
  19. Writing superblocks and filesystem accounting information: done
  20. This filesystem will be automatically checked every 28 mounts or
  21. 180 days, whichever comes first.Use tune2fs -c or -i to override.
  22. $


After you’ve created the new filesystem, you can mount the volume in the virtual directory using the standard Linux mount command, just as if it were a physical partition. The only difference is that you use a special path that identifies the logical volume:

  1. $ sudo mount /dev/Vol1/lvtest /mnt/my_partition
  2. $$
  3. mount
  4. /dev/mapper/vg_server01-lv_root on / type ext4 (rw)
  5. […]
  6. /dev/mapper/Vol1-lvtest on /mnt/my_partition type ext4 (rw)
  7. $$
  8. cd /mnt/my_partition
  9. $$
  10. ls -al
  11. total 24
  12. drwxr-xr-x. 3 root root 4096 Jun 12 10:22 .
  13. drwxr-xr-x. 3 root root 4096 Jun 11 09:58 ..
  14. drwx––. 2 root root 16384 Jun 12 10:22 lost+found
  15. $


Notice that the path used in both the mkfs.ext4 and mount commands is a little odd. Instead of a physical partition path, the path uses the volume group name, along with the logical volume name. After the filesystem is mounted, you can access the new area in the virtual directory.

Modifying the LVM

Because the benefit of using the Linux LVM is to dynamically modify filesystems, you’d expect that some tools would allow you to do that. Some tools are available in Linux that allow you to modify the existing logical volume management configuration.

If you don’t have access to a fancy graphical interface for managing your Linux LVM environment, all is not lost. You’ve already seen some of the Linux LVM command line programs in action in this chapter. You can use a host of other command line programs to manage the LVM setup after you’ve installed it. The Table below lists the common commands that are available in the Linux LVM package.

The Linux LVM Commands
The Linux LVM Commands

Using these command line programs, you have full control over your Linux LVM environment.

Tip
Be careful when manually increasing or decreasing the size of a logical volume. The filesystem stored in the logical volume must be manually fixed to handle the change in size. Most filesystems include command line programs for reformatting the filesystem, such as the resize2fs program for the ext2, ext3, and ext4 filesystems.

Working with storage devices in Linux requires that you know a little bit about filesystems. Knowing how to create and work with filesystems from the command line can come in handy as you work on Linux systems. This chapter discussed how to handle filesystems from the Linux command line.

The Linux system is different from Windows in that it supports lots of different methods for storing files and folders. Each filesystem method has different features that make it ideal for different situations. Also, each filesystem method uses different commands for interacting with the storage device.

Before you can install a filesystem on a storage device, you must first prepare the device. The fdisk command is used to partition storage devices to get them ready for the filesystem. When you partition the storage device, you must define what type of filesystem will be used on it.

After you partition a storage device, you can use one of several different filesystems for the partition. Popular Linux filesystems include ext4 and XFS. Both of these filesystems provide journaling filesystem features, making them less prone to errors and problems if the Linux system should crash.
One limiting factor to creating filesystems directly on a storage device partition is that you can’t easily change the size of the filesystem if you run out of disk space. However, Linux supports logical volume management, a method of creating virtual partitions across multiple storage devices. This method allows you to easily expand an existing filesystem without having to completely rebuild it. The Linux LVM package provides command line commands to create logical volumes across multiple storage devices on which to build filesystems.

Now that you’ve seen the core Linux command line commands, it’s close to the time to start creating some shell script programs. However, before you start coding, we need to discuss another element: installing software. If you plan to write shell scripts, you need an environment in which to create your masterpieces. The next chapter discusses how to install and manage software packages from the command line in different Linux environments.

0 Response to "Using the LVM in Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel