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.
Two versions of Linux LVM are available:
LVM1:
LVM2:
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.
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 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.
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,
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.
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:- […]
- Command (m for help): t
- Selected partition 1
- Hex code (type L to list codes): 8e
- Changed system type of partition 1 to 8e (Linux LVM)
- Command (m for help): p
- Disk /dev/sdb: 5368 MB, 5368709120 bytes
- 255 heads, 63 sectors/track, 652 cylinders
- Units = cylinders of 16065 * 512 = 8225280 bytes
- Sector size (logical/physical): 512 bytes / 512 bytes
- I/O size (minimum/optimal): 512 bytes / 512 bytes
- Disk identifier: 0xa8661341
- Device Boot Start End Blocks Id System
- /dev/sdb1 1 262 2104483+ 8e Linux LVM
- Command (m for help): w
- The partition table has been altered!
- Calling ioctl() to re-read partition table.
- Syncing disks.
- $
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:
- $ sudo pvcreate /dev/sdb1
- dev_is_mpath: failed to get device for 8:17
- Physical volume “/dev/sdb1” successfully created
- $
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:
- $ sudo pvdisplay /dev/sdb1
- “/dev/sdb1” is a new physical volume of “2.01 GiB”
- – NEW Physical volume –
- PV Name /dev/sdb1
- VG Name
- PV Size 2.01 GiB
- Allocatable NO
- PE Size 0
- Total PE 0
- Free PE 0
- Allocated PE 0
- PV UUID 0FIuq2-LBod-IOWt-8VeN-tglm-Q2ik-rGU2w7
- $
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:
- $ sudo vgdisplay Vol1
- – Volume group –
- VG Name Vol1
- System ID
- Format lvm2
- Metadata Areas 1
- Metadata Sequence No 1
- VG Access read/write
- VG Status resizable
- MAX LV 0
- Cur LV 0
- Open LV 0
- Max PV 0
- Cur PV 1
- Act PV 1
- VG Size 2.00 GiB
- PE Size 4.00 MiB
- Total PE 513
- Alloc PE / Size 0 / 0
- Free PE / Size 513 / 2.00 GiB
- VG UUID oe4I7e-5RA9-G9ti-ANoI-QKLz-qkX4-58Wj6e
- $
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:
- $ sudo mkfs.ext4 /dev/Vol1/lvtest
- mke2fs 1.41.12 (17-May-2010)
- Filesystem label=
- OS type: Linux
- Block size=4096 (log=2)
- Fragment size=4096 (log=2)
- Stride=0 blocks, Stripe width=0 blocks
- 131376 inodes, 525312 blocks
- 26265 blocks (5.00%) reserved for the super user
- First data block=0
- Maximum filesystem blocks=541065216
- 17 block groups
- 32768 blocks per group, 32768 fragments per group
- 7728 inodes per group
- Superblock backups stored on blocks:
- 32768, 98304, 163840, 229376, 294912
- Writing inode tables: done
- Creating journal (16384 blocks): done
- Writing superblocks and filesystem accounting information: done
- This filesystem will be automatically checked every 28 mounts or
- 180 days, whichever comes first.Use tune2fs -c or -i to override.
- $
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:
- $ sudo mount /dev/Vol1/lvtest /mnt/my_partition
- $$
- mount
- /dev/mapper/vg_server01-lv_root on / type ext4 (rw)
- […]
- /dev/mapper/Vol1-lvtest on /mnt/my_partition type ext4 (rw)
- $$
- cd /mnt/my_partition
- $$
- ls -al
- total 24
- drwxr-xr-x. 3 root root 4096 Jun 12 10:22 .
- drwxr-xr-x. 3 root root 4096 Jun 11 09:58 ..
- drwx––. 2 root root 16384 Jun 12 10:22 lost+found
- $
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 |
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