Common Configuration Commands
Creating a Physical Volume
- Create primary and logical partitions.
Run the fdisk –l command to scan for the mapped LUNs. Suppose that the identified LUN is displayed as disk sdb. Run the fdisk /dev/sdb command to partition sdb.
[root@root ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. The number of cylinders for this disk is set to 13054. 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) Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) Command (m for help):
Type n to create new partitions and type p to create the primary partition. Specify the primary partition number to 1. Keep the default value of first cylinder and specify a value to last cylinder.
Command (m for help): N/A Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-13054, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-13054, default 13054): 200
Type n to create new partitions and type e to create expansion partitions. Then type p to view partitions.
Command (m for help): N/A Command action e extended p primary partition (1-4) e Partition number (1-4): 4 First cylinder (201-13054, default 201): Using default value 201 Last cylinder or +size or +sizeM or +sizeK (201-13054, default 13054): 1000 Command (m for help): p Disk /dev/sdb: 107.3 GB, 107374182400 bytes 255 heads, 63 sectors/track, 13054 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 200 1606468+ 83 Linux /dev/sdb4 201 1000 6426000 5 Extended
Type n to create new partitions and type l to create logical partitions. Type p to view partitions and type w to save partitions and exit from partition creation.
Command (m for help): N/A Command action l logical (5 or over) p primary partition (1-4) l First cylinder (201-1000, default 201): Using default value 201 Last cylinder or +size or +sizeM or +sizeK (201-1000, default 1000): 400 Command (m for help): p Disk /dev/sdb: 107.3 GB, 107374182400 bytes 255 heads, 63 sectors/track, 13054 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 200 1606468+ 83 Linux /dev/sdb4 201 1000 6426000 5 Extended /dev/sdb5 201 400 1606468+ 83 Linux
- Create LVM partitions.
Perform the following operation to convert partitions 5 and 6 of sdb to LVM partitions.
Command (m for help): p Disk /dev/sdb: 107.3 GB, 107374182400 bytes 255 heads, 63 sectors/track, 13054 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 200 1606468+ 83 Linux /dev/sdb4 201 1000 6426000 5 Extended /dev/sdb5 201 400 1606468+ 8e Linux /dev/sdb6 401 600 1606468+ 83 Linux Command (m for help): t Partition number (1-6): 5 Hex code (type L to list codes): 8e Changed system type of partition 6 to 8e (Linux LVM) Command (m for help): t Partition number (1-6): 6 Hex code (type L to list codes): 8e Changed system type of partition 6 to 8e (Linux LVM) Command (m for help): p Disk /dev/sdb: 107.3 GB, 107374182400 bytes 255 heads, 63 sectors/track, 13054 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 200 1606468+ 83 Linux /dev/sdb4 201 1000 6426000 5 Extended /dev/sdb5 201 400 1606468+ 8e Linux LVM /dev/sdb6 401 600 1606468+ 8e Linux LVM
- Run the pvcreate command to create physical volumes.
[root@root ~]# pvcreate /dev/sdb5 Physical volume "/dev/sdb5" successfully created [root@root ~]# pvcreate /dev/sdb6 Physical volume "/dev/sdb6" successfully created
- Run the pvdisplay -v command to verify the physical volume creation.
[root@root ~]# pvdisplay -v Scanning for physical volume names Wiping cache of LVM-capable devices --- Physical volume --- PV Name /dev/sda2 VG Name VolGroup00 PV Size 557.65 GB / not usable 21.17 MB Allocatable yes (but full) PE Size (KByte) 32768 Total PE 17844 Free PE 0 Allocated PE 17844 PV UUID KyucjQ-9zte-1Zyr-0sZ0-Xxzt-HVjZ-2vQp8B "/dev/sdb5" is a new physical volume of "1.53 GB" --- NEW Physical volume --- PV Name /dev/sdb5 VG Name PV Size 1.53 GB Allocatable NO PE Size (KByte) 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID g60zN0-3sYn-qPbd-7y0M-dGfZ-hVs7-763Ywo "/dev/sdb6" is a new physical volume of "1.53 GB" --- NEW Physical volume --- PV Name /dev/sdb6 VG Name PV Size 1.53 GB Allocatable NO PE Size (KByte) 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 5UhmY2-fS4p-gdCo-OOgZ-nOa9-AV3H-LkvrNc
Changing the Size of a Physical Volume
Run the pvresize command to change the size of a physical volume. The command syntax is as follows:
pvresize –setphysicalvolumesize capacity size (unit: m or g) device name
In the following example, the size of a physical volume is changed from 1.53 GB to 300 MB.
[root@root ~]# pvscan
PV /dev/sda2 VG VolGroup00 lvm2 [557.62 GB / 0 free]
PV /dev/sdb5 lvm2 [1.53 GB]
PV /dev/sdb6 lvm2 [1.53 GB]
Total: 3 [560.69 GB] / in use: 1 [557.62 GB] / in no VG: 2 [3.06 GB]
[root@root ~]# pvresize --setphysicalvolumesize 300 /dev/sdb5
Physical volume "/dev/sdb5" changed
1 physical volume(s) resized / 0 physical volume(s) not resized
[root@root ~]# pvscan
PV /dev/sda2 VG VolGroup00 lvm2 [557.62 GB / 0 free]
PV /dev/sdb5 lvm2 [300.00 MB]
PV /dev/sdb6 lvm2 [1.53 GB]
Total: 3 [559.45 GB] / in use: 1 [557.62 GB] / in no VG: 2 [1.83 GB]
Creating a Volume Group
Run the vgcreate command to create a volume group.
[root@root ~]# vgcreate vg0 /dev/sdb5 /dev/sdb6
Volume group "vg0" successfully created
Expanding a Volume Group
Run the following command to expand a volume group:
vgextend vgname pvname
The following is an example:
[root@root ~]# vgdisplay -v /dev/vg0
Using volume group(s) on command line
Finding volume group "vg0"
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 1.82 GB
PE Size 4.00 MB
Total PE 466
Alloc PE / Size 0 / 0
Free PE / Size 466 / 1.82 GB
VG UUID ARkbdL-9ID6-5HCy-DSQG-Aj5z-dQap-9VkM5X
--- Physical volumes ---
PV Name /dev/sdb5
PV UUID g60zN0-3sYn-qPbd-7y0M-dGfZ-hVs7-763Ywo
PV Status allocatable
Total PE / Free PE 74 / 74
PV Name /dev/sdb6
PV UUID 5UhmY2-fS4p-gdCo-OOgZ-nOa9-AV3H-LkvrNc
PV Status allocatable
Total PE / Free PE 392 / 392
[root@root ~]# vgextend /dev/vg0 /dev/sdb7
Volume group "vg0" successfully extended
[root@root ~]# vgdisplay -v /dev/vg0
Using volume group(s) on command line
Finding volume group "vg0"
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size 3.35 GB
PE Size 4.00 MB
Total PE 858
Alloc PE / Size 0 / 0
Free PE / Size 858 / 3.35 GB
VG UUID ARkbdL-9ID6-5HCy-DSQG-Aj5z-dQap-9VkM5X
--- Physical volumes ---
PV Name /dev/sdb5
PV UUID g60zN0-3sYn-qPbd-7y0M-dGfZ-hVs7-763Ywo
PV Status allocatable
Total PE / Free PE 74 / 74
PV Name /dev/sdb6
PV UUID 5UhmY2-fS4p-gdCo-OOgZ-nOa9-AV3H-LkvrNc
PV Status allocatable
Total PE / Free PE 392 / 392
PV Name /dev/sdb7
PV UUID iF5Att-fVIj-9dOy-5055-rJlq-pOrS-aW8g2P
PV Status allocatable
Total PE / Free PE 392 / 392
In this example, volume group /dev/vg0 originally contains physical volumes /dev/sdb5 and /dev/sdb6. After the command is run, /dev/sdb7 is added to this volume group.
Creating a Logical Volume
- Run the lvcreate command to create a logical volume. The following is an example:
[root@root ~]# lvcreate -L 10m -n lv0 vg0 Rounding up size to full physical extent 12.00 MB Logical volume "lv0" created
The parameters in this output are described as follows:
- -L: indicates the size of a logical volume, expressed in MB.
- -n: indicates the name of a logical volume.
- View and confirm that the information about the newly created logical volume is correct.
[root@root ~]# vgdisplay -v vg0 Using volume group(s) on command line Finding volume group "vg0" --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 0 Max PV 0 Cur PV 3 Act PV 3 VG Size 3.35 GB PE Size 4.00 MB Total PE 858 Alloc PE / Size 3 / 12.00 MB Free PE / Size 855 / 3.34 GB VG UUID ARkbdL-9ID6-5HCy-DSQG-Aj5z-dQap-9VkM5X --- Logical volume --- LV Name /dev/vg0/lv0 VG Name vg0 LV UUID H6uskM-6clf-NVh2-KMiO-1Gk2-0iBz-nXOav2 LV Write Access read/write LV Status available # open 0 LV Size 12.00 MB Current LE 3 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 --- Physical volumes --- PV Name /dev/sdb5 PV UUID g60zN0-3sYn-qPbd-7y0M-dGfZ-hVs7-763Ywo PV Status allocatable Total PE / Free PE 74 / 74 PV Name /dev/sdb6 PV UUID 5UhmY2-fS4p-gdCo-OOgZ-nOa9-AV3H-LkvrNc PV Status allocatable Total PE / Free PE 392 / 389 PV Name /dev/sdb7 PV UUID iF5Att-fVIj-9dOy-5055-rJlq-pOrS-aW8g2P PV Status allocatable Total PE / Free PE 392 / 392 [root@root ~]# lvdisplay -v /dev/vg0/lv0 Using logical volume(s) on command line --- Logical volume --- LV Name /dev/vg0/lv0 VG Name vg0 LV UUID H6uskM-6clf-NVh2-KMiO-1Gk2-0iBz-nXOav2 LV Write Access read/write LV Status available # open 0 LV Size 12.00 MB Current LE 3 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2
Creating a File System
- Run the mkfs.xx command to create a file system. The following is an example:
[root@root ~]# mkfs.ext3 /dev/vg0/rlv0 mke2fs 1.39 (29-May-2006) Filesystem label= OS type: Operating system: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) 3072 inodes, 12288 blocks 614 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=12582912 2 block groups 8192 blocks per group, 8192 fragments per group 1536 inodes per group Superblock backups stored on blocks: 8193 Writing inode tables: done Creating journal (1024 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 20 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
- Create a mount point and mount the logical volume.
[root@root ~]# mkdir /test/mnt1 [root@root ~]# mount /dev/vg0/lv0 /test/mnt1/
- Display the mounting information.
[root@root ~]# df -l Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/VolGroup00-LogVol00 548527904 3105828 517108888 1% / /dev/sda1 101086 15667 80200 17% /boot tmpfs 8137904 0 8137904 0% /dev/shm /dev/mapper/vg0-lv0 11895 1138 10143 11% /test/mnt1
The output shows that the logical volume is mounted correctly and can be used for subsequent data read and write.
- (Optional) You can run the following command to unmount the logical volume:
[root@root ~]# umount /dev/vg0/lv0 [root@root ~]# df -l Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/VolGroup00-LogVol00 548527904 3105828 517108888 1% / /dev/sda1 101086 15667 80200 17% /boot tmpfs 8137904 0 8137904 0% /dev/shm
Expanding a Logical Volume
Run the lvextend command to expand a logical volume. The command syntax is as follows:
lvextend -L +Target capacity Logical volume path
The following is an example:
[root@root ~]# lvscan
ACTIVE '/dev/vg0/lv0' [12.00 MB] inherit
ACTIVE '/dev/VolGroup00/LogVol00' [540.03 GB] inherit
ACTIVE '/dev/VolGroup00/LogVol01' [17.59 GB] inherit
[root@root ~]# pvscan
PV /dev/sdb5 VG vg0 lvm2 [296.00 MB / 296.00 MB free]
PV /dev/sdb6 VG vg0 lvm2 [1.53 GB / 1.52 GB free]
PV /dev/sdb7 VG vg0 lvm2 [1.53 GB / 1.53 GB free]
PV /dev/sda2 VG VolGroup00 lvm2 [557.62 GB / 0 free]
Total: 4 [560.98 GB] / in use: 4 [560.98 GB] / in no VG: 0 [0 ]
[root@root ~]# lvextend -L +100m /dev/vg0/lv0
Extending logical volume lv0 to 112.00 MB
Logical volume lv0 successfully resized
[root@root ~]# lvscan
ACTIVE '/dev/vg0/lv0' [112.00 MB] inherit
ACTIVE '/dev/VolGroup00/LogVol00' [540.03 GB] inherit
ACTIVE '/dev/VolGroup00/LogVol01' [17.59 GB] inherit
[root@root ~]# pvscan
PV /dev/sdb5 VG vg0 lvm2 [296.00 MB / 296.00 MB free]
PV /dev/sdb6 VG vg0 lvm2 [1.53 GB / 1.42 GB free]
PV /dev/sdb7 VG vg0 lvm2 [1.53 GB / 1.53 GB free]
PV /dev/sda2 VG VolGroup00 lvm2 [557.62 GB / 0 free]
Total: 4 [560.98 GB] / in use: 4 [560.98 GB] / in no VG: 0 [0 ]
The output shows that the logical volume capacity is expanded.
Downsizing a Logical Volume
Run the lvreduce command to downsize a logical volume. The command syntax is as follows:
lvreduce -L -Target capacity Logical volume path
The following is an example:
[root@root ~]# lvscan
ACTIVE '/dev/vg0/lv0' [112.00 MB] inherit
ACTIVE '/dev/VolGroup00/LogVol00' [540.03 GB] inherit
ACTIVE '/dev/VolGroup00/LogVol01' [17.59 GB] inherit
[root@root ~]# pvscan
PV /dev/sdb5 VG vg0 lvm2 [296.00 MB / 296.00 MB free]
PV /dev/sdb6 VG vg0 lvm2 [1.53 GB / 1.42 GB free]
PV /dev/sdb7 VG vg0 lvm2 [1.53 GB / 1.53 GB free]
PV /dev/sda2 VG VolGroup00 lvm2 [557.62 GB / 0 free]
Total: 4 [560.98 GB] / in use: 4 [560.98 GB] / in no VG: 0 [0 ]
[root@root ~]# lvreduce -L -100m /dev/vg0/lv0
WARNING: Reducing active logical volume to 12.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
Reducing logical volume lv0 to 12.00 MB
Logical volume lv0 successfully resized
[root@root ~]# lvscan
ACTIVE '/dev/vg0/lv0' [12.00 MB] inherit
ACTIVE '/dev/VolGroup00/LogVol00' [540.03 GB] inherit
ACTIVE '/dev/VolGroup00/LogVol01' [17.59 GB] inherit
[root@root ~]# pvscan
PV /dev/sdb5 VG vg0 lvm2 [296.00 MB / 296.00 MB free]
PV /dev/sdb6 VG vg0 lvm2 [1.53 GB / 1.52 GB free]
PV /dev/sdb7 VG vg0 lvm2 [1.53 GB / 1.53 GB free]
PV /dev/sda2 VG VolGroup00 lvm2 [557.62 GB / 0 free]
Total: 4 [560.98 GB] / in use: 4 [560.98 GB] / in no VG: 0 [0 ]
The output shows that the logical volume capacity is reduced.
Activating a Volume Group
Run the following command to activate a volume group:
vgchange -a y Volume group name
The following is an example:
[root@root ~]# vgchange -a y /dev/vg0
1 logical volume(s) in volume group "vg0" now active
Deactivating a Volume Group
Run the following command to deactivate a volume group:
vgchange –a n y Volume group name
The following is an example:
[root@root ~]# vgchange -a n /dev/vg0
0 logical volume(s) in volume group "vg0" now active
Exporting a Volume Group
A volume group needs to be imported or exported in clusters, data backup, or recovery.
Run the following command to export a volume group:
vgexport Volume group name
The following is an example:
[root@root ~]# vgexport vg0
Volume group "vg0" successfully exported
[root@root ~]# pvscan
PV /dev/sdb5 is in exported VG vg0 [296.00 MB / 296.00 MB free]
PV /dev/sdb6 is in exported VG vg0 [1.53 GB / 1.52 GB free]
PV /dev/sdb7 is in exported VG vg0 [1.53 GB / 1.53 GB free]
PV /dev/sda2 VG VolGroup00 lvm2 [557.62 GB / 0 free]
Total: 4 [560.98 GB] / in use: 4 [560.98 GB] / in no VG: 0 [0 ]
Importing a Volume Group
Run the following command to import a volume group:
vgimport Volume group name
The following is an example (importing a volume group on a local computer):
[root@root ~]# vgimport vg0
Volume group "vg0" successfully imported
[root@root ~]# pvscan
PV /dev/sdb5 VG vg0 lvm2 [296.00 MB / 296.00 MB free]
PV /dev/sdb6 VG vg0 lvm2 [1.53 GB / 1.52 GB free]
PV /dev/sdb7 VG vg0 lvm2 [1.53 GB / 1.53 GB free]
PV /dev/sda2 VG VolGroup00 lvm2 [557.62 GB / 0 free]
Total: 4 [560.98 GB] / in use: 4 [560.98 GB] / in no VG: 0 [0 ]
Deleting a Logical Volume
Run the following command to delete a logical volume:
lvremove Logical volume name
The following is an example:
[root@root ~]# lvscan
inactive '/dev/vg0/lv0' [12.00 MB] inherit
ACTIVE '/dev/VolGroup00/LogVol00' [540.03 GB] inherit
ACTIVE '/dev/VolGroup00/LogVol01' [17.59 GB] inherit
[root@root ~]# lvremove /dev/vg0/lv0
Logical volume "lv0" successfully removed
[root@root ~]# lvscan
ACTIVE '/dev/VolGroup00/LogVol00' [540.03 GB] inherit
ACTIVE '/dev/VolGroup00/LogVol01' [17.59 GB] inherit
Deleting a Volume Group
Run the following command to delete a volume group:
vgremove Volume group name
Perform the following steps:
- Ensure that all logical volumes are deleted from the volume group.
[root@root ~]# vgdisplay -v /dev/vg0 Using volume group(s) on command line Finding volume group "vg0" --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 8 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 3 Act PV 3 VG Size 3.35 GB PE Size 4.00 MB Total PE 858 Alloc PE / Size 0 / 0 Free PE / Size 858 / 3.35 GB VG UUID ARkbdL-9ID6-5HCy-DSQG-Aj5z-dQap-9VkM5X --- Physical volumes --- PV Name /dev/sdb5 PV UUID g60zN0-3sYn-qPbd-7y0M-dGfZ-hVs7-763Ywo PV Status allocatable Total PE / Free PE 74 / 74 PV Name /dev/sdb6 PV UUID 5UhmY2-fS4p-gdCo-OOgZ-nOa9-AV3H-LkvrNc PV Status allocatable Total PE / Free PE 392 / 392 PV Name /dev/sdb7 PV UUID iF5Att-fVIj-9dOy-5055-rJlq-pOrS-aW8g2P PV Status allocatable Total PE / Free PE 392 / 392
- Delete the volume group.
[root@root ~]# vgremove /dev/vg0 Volume group "vg0" successfully removed [root@root ~]# vgdisplay -v /dev/vg0 Using volume group(s) on command line Finding volume group "vg0" Wiping cache of LVM-capable devices Volume group "vg0" not found
Deleting a Physical Volume
Run the following command to delete a physical volume:
Pvremove Raw device name
The following is an example:
[root@root ~]# pvremove /dev/sdb5
Labels on physical volume "/dev/sdb5" successfully wiped
[root@root ~]# pvremove /dev/sdb6
Labels on physical volume "/dev/sdb6" successfully wiped
[root@root ~]# pvremove /dev/sdb7
Labels on physical volume "/dev/sdb7" successfully wiped