Logical Volume Manager in Linux ( Create, Displ...

What is LVM?

LVM is a tool for logical volume management which includes allocating disks, striping, mirroring and resizing logical volumes. With LVM, a hard drive or set of hard drives is allocated to one or more physical Volume Groupical volumes.LVM physical volumes can be placed on other block devices which might span two or more disks.

The physical volumes are combined into logical volumes, with the exception of the /boot/ partition. The /boot/ partition cannot be on a logical volume group because the boot loader cannot read it. If the root (/) partition is on a logical volume, create a separate /boot/ partition which is not a part of a volume group.

LVM Terms

  • Physical Volume: A physical volume (PV) is another name for a regular physical disk partition that is used or will be used by LVM.
  • Volume Group: Any number of physical volumes (PVs) on different disk drives can be added  together into a volume group (VG).
  • Logical Volumes: Volume groups must then be subdivided into logical volumes. Each logical volume can be individually formatted as if it were a regular Linux partition. A logical volume is, therefore, like a virtual partition on your virtual disk drive.

Steps of Creating LVM in Breif

Step 1 : Create Physical Volumes

pvcreate   /dev/hda#    /dev/hda#

Step 2 : Create Volume Group 
vgcreate   VG1   /dev/hda#    /dev/hda#

Step 3 : Change Volume  Group to ACTIVE(optional)
vgchange   -a   y    VG1

Step 4 : Create Logical Volume
lvcreate     -L   +600M    -n    LV1    VG1

Step 5 : Format the Logical Volume
mkfs -t ext3  /dev/VG1/LV1

Step 6: Create a directory
mkdir -p /mnt/data

Step 7: Mount in /etc/fstab 
/dev/VG1/LV1  /mnt/data                           ext3                       defaults                               0 0

Step 8: Activate the new volume
mount –a

Check the newly mounted Logical Volume
For Short details
#pvscan
#lvscan
#vgscan

For Long Full Details
#pvdisplay
#lvdisplay
#vgdisplay

RESIZING THE LVM : Note Only on RHEL 5.3 and above you can directly increase filesystem without unmount

Step 1: Resize the LVM

lvextend    -L    +200M      /dev/VG1/LV1

Step 2: Configuring the HDD for new extended space

resize2fs      /dev/VG1/LV1

Example of How to Create, Display and Resize PV , VG , LV

You have one or more physical volumes (/dev/sdb1 – /dev/sde1 in our example), and on these physical volumes you create one or more volume groups (e.g. fileserver), and in each volume group you can create one or more logical volumes. If you use multiple physical volumes, each logical volume can be bigger than one of the underlying physical volumes.

It is a good practice to not allocate the full space to logical volumes, but leave some space unused. That way you can enlarge one or more logical volumes later on if you feel the need for it.

Check the avaible disk on the server with following two commands

fdisk -l 

Let’s find out about our hard disks:

fdisk -lThe output looks like this:webmanual01:~# fdisk -l

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 18 144553+ 83 Linux
/dev/sda2 19 2450 19535040 83 Linux
/dev/sda4 2451 2610 1285200 82 Linux swap / Solaris

Disk /dev/sdb: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn’t contain a valid partition table

Disk /dev/sdc: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdc doesn’t contain a valid partition table

Disk /dev/sdd: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdd doesn’t contain a valid partition table

Disk /dev/sde: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sde doesn’t contain a valid partition table

Disk /dev/sdf: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdf doesn’t contain a valid partition table

There are no partitions yet on /dev/sdb – /dev/sdf

Create Partion

We will create the partitions /dev/sdb1, /dev/sdc1, /dev/sdd1, and /dev/sde1 with 25 GB  and leave /dev/sdf untouched for now. It will act our hard disks had only 25GB of space instead of 80GB for now, therefore we assign 25GB to /dev/sdb1, /dev/sdc1, /dev/sdd1, and /dev/sde1:

fdisk /dev/sdb

webmanual01:~# fdisk /dev/sdbThe number of cylinders for this disk is set to 10443.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)Command (m for help): <– m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)Command (m for help): <– n
Command action
e extended
p primary partition (1-4)
<– p
Partition number (1-4): <– 1
First cylinder (1-10443, default 1): <– < Press ENTER> 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-10443, default 10443): <– +25000M

Command (m for help): <– t
Selected partition 1
Hex code (type L to list codes): <– L

0 Empty 1e Hidden W95 FAT1 80 Old Minix be Solaris boot
1 FAT12 24 NEC DOS 81 Minix / old Lin bf Solaris
2 XENIX root 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
5 Extended 41 PPC PReP Boot 85 Linux extended c7 Syrinx
6 FAT16 42 SFS 86 NTFS volume set da Non-FS data
7 HPFS/NTFS 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
8 AIX 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
9 AIX bootable 4f QNX4.x 3rd part 8e Linux LVM df BootIt
a OS/2 Boot Manag 50 OnTrack DM 93 Amoeba e1 DOS access
b W95 FAT32 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
c W95 FAT32 (LBA) 52 CP/M 9f BSD/OS e4 SpeedStor
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
f W95 Ext’d (LBA) 54 OnTrackDM6 a5 FreeBSD ee EFI GPT
10 OPUS 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor a9 NetBSD f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fd Linux raid auto
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fe LANstep
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid ff BBT
1c Hidden W95 FAT3 75 PC/IX
Hex code (type L to list codes): <– 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): <– w
The partition table has been altered!

Now we do the same for the hard disks /dev/sdc – /dev/sde:

fdisk /dev/sdc
fdisk /dev/sdd
fdisk /dev/sde

Verify 

Run fdisk -l again. The output should look like this:

webmanual01:~# fdisk -lDisk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytesDevice Boot Start End Blocks Id System
/dev/sda1 * 1 18 144553+ 83 Linux
/dev/sda2 19 2450 19535040 83 Linux
/dev/sda4 2451 2610 1285200 82 Linux swap / SolarisDisk /dev/sdb: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 3040 24418768+ 8e Linux LVM

Disk /dev/sdc: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdc1 1 3040 24418768+ 8e Linux LVM

Disk /dev/sdd: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdd1 1 3040 24418768+ 8e Linux LVM

Disk /dev/sde: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sde1 1 3040 24418768+ 8e Linux LVM

Disk /dev/sdf: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdf doesn’t contain a valid partition table

Create PV

Create PV on available disks on the servers

webmanual01:~# pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
  Physical volume ”/dev/sdb1″ successfully created
  Physical volume ”/dev/sdc1″ successfully created
  Physical volume ”/dev/sdd1″ successfully created
  Physical volume ”/dev/sde1″ successfully

Create VG

vgcreate  vgtest /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1

Creating LV

Create a lvtesta of space 1 G on VG vgtesta

root@webmanual:/root> lvcreate -L 1024M /dev/vgtesta -n lvtesta
Incorrect metadata area header checksum
Logical volume “lvtesta” created

Checking LV properties

root@webmanual:/root> lvdisplay /dev/vgtesta/lvtestaIncorrect metadata area header checksum
— Logical volume —
LV Name /dev/vgtesta/lvtesta
VG Name vgtesta
LV UUID sXOjN8-cWnW-7DKf-Pm32-kuKU-z7ww-hkGGgw
LV Write Access read/write
LV Status available
# open 0
LV Size 1.00 GB
Current LE 1
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:58

Creating a directory to mount the LV created on previous step

root@webmanual:/root> mkdir /tmp/testinga
root@webmanual:/root> ls -l /tmp/testinga
total 0

Formationg in ext3 the LV lvtesta

root@webmanual:/root> mkfs -t ext3 /dev/vgtesta/lvtesta
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
131072 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

Mount the LV and Make a entry in /etc/fstab

root@webmanual:/root> mount /dev/vgtesta/lvtesta /tmp/testingaroot@webmanual:/root> vi /etc/fstabPress ESC+G then ESC + O/dev/vgtesta/lvtesta /tmp/testinga ext3    defaults        0 0 —> Make enrty like this

Verify the mounting

root@webmanual:/root> df -h /tmp/testinga
/dev/mapper/vgtesta-lvtesta 1008M   34M  924M   4% /tmp/testinga

Extending LV

root@webmanual:/root> lvextend -L+1G /dev/vgtesta/lvtesta
Incorrect metadata area header checksum
Extending logical volume lvtesta to 2.00 GB
Logical volume lvtesta successfully resized
root@webmanual:/root>

Resize the LV

root@webmanual:/root> resize2fs -p /dev/vgtesta/lvtesta 1G
resize2fs 1.39 (29-May-2006)
Please run ‘e2fsck -f /dev/vgtesta/lvtesta’ first.Resize failed and asked to run e2fsck . Run e2fsck as below 

root@webmanual:/root> e2fsck -f /dev/vgtesta/lvtesta

e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vgtesta/lvtesta: 11/262144 files (9.1% non-contiguous), 16822/524288 blocks

 

Resize will work nowroot@webmanual:/root> resize2fs -p /dev/vgtesta/lvtesta 1G
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vgtesta/lvtesta to 262144 (4k) blocks.
Begin pass 3 (max = 16)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/vgtesta/lvtesta is now 262144 blocks long.

Reducing LV

root@webmanual:/root> lvreduce -L -1G /dev/vgtesta/lvtesta
Incorrect metadata area header checksum
WARNING: Reducing active logical volume to 1.00 GB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvtesta? [y/n]: y
Reducing logical volume lvtesta to 1.00 GB
Logical volume lvtesta successfully
root@webmanual:/root> df -h /tmp/testinga
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vgtesta-lvtesta
1008M 34M 934M 4% /tmp/testinga
root@webmanual:/root>


你可能感兴趣的:(lvm)