常用配置命令
创建物理卷
操作步骤描述如下:
- 主分区与逻辑分区的创建。
通过fdisk -l命令发现映射过来的LUN后,就可以键入命令fdisk /dev/sdb(如果新映射过来的LUN显示为sdb)对磁盘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):
键入命令n创建新分区,选择p命令创建主分区,选择分区编号为1;选择起始柱面为默认值,结束柱面自己输入一个确定的值。
Command (m for help): n 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
键入命令n创建新分区,选择e命令创建扩展分区,选择p命令查看分区情况。
Command (m for help): n 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
键入命令n创建新分区,选择l命令创建逻辑分区,选择p命令查看分区情况,选择w命令保存分区情况并退出。
Command (m for help): n 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
- 创建LVM类型分区。
下面的操作将盘符sdb的分区5和6转变成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 /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
- 使用pvcreate命令创建物理卷。
[root@root ~]# pvcreate /dev/sdb5 Physical volume "/dev/sdb5" successfully created [root@root ~]# pvcreate /dev/sdb6 Physical volume "/dev/sdb6" successfully created
- 使用pvdisplay -v查询物理卷是否创建成功。
[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
修改物理卷大小
使用命令pvresize改变物理卷大小,命令格式为
pvresize --setphysicalvolumesize 容量大小(单位m、g)设备名称
下面的操作将原大小为1.53GB的PV更改为300M。
[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]
创建卷组
使用命令vgcreate创建卷组。
[root@root ~]# vgcreate vg0 /dev/sdb5 /dev/sdb6
Volume group "vg0" successfully created
扩展卷组
命令格式如下:
vgextend vgname pvname
操作例子如下所示:
[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
卷组/dev/vg0中原本包含有物理卷:/dev/sdb5和/dev/sdb6。上面的命令将物理卷:/dev/sdb7加入该卷组中,从而达到扩展卷组的目的。
创建逻辑卷
操作步骤描述如下:
- 使用交互式命令lvcreate命令创建逻辑卷,例子如下所示:
[root@root ~]# lvcreate -L 10m -n lv0 vg0 Rounding up size to full physical extent 12.00 MB Logical volume "lv0" created
上面的命令中,部分参数解释如下:
- -L:Logical_volume_size,以MB为单位的逻辑卷大小。
- -n:逻辑卷的名字。
- 查看创建的逻辑卷,确认卷的信息无误。
[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
创建文件系统
操作步骤描述如下:
- 使用mkfs.xx命令创建文件系统。例子如下:
[root@root ~]# mkfs.ext3 /dev/vg0/rlv0 mke2fs 1.39 (29-May-2006) Filesystem label= OS type: 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.
- 创建相应的挂载点,并将逻辑卷挂载起来。
[root@root ~]# mkdir /test/mnt1 [root@root ~]# mount /dev/vg0/lv0 /test/mnt1/
- 显示挂载信息。
[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
如上所示,逻辑卷挂载正常,后续即可对其进行读写操作。
- 如果需要卸载卷,则可以执行下面的命令:
[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
扩展逻辑卷
扩展逻辑卷使用lvextend命令,其格式为
lvextend -L +要扩展的容量大小 逻辑卷路径
示例如下:
[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 ]
从上面的显示可以发现,逻辑卷的容量已经更新,扩展成功。
缩减逻辑卷
缩减逻辑卷使用lvreduce命令,其格式为
lvreduce -L -要扩展的容量大小 逻辑卷路径
示例如下:
[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 ]
从上面的显示可以发现,逻辑卷的容量已经更新,缩减成功。
激活卷组
激活卷组的命令格式如下:
vgchange -a y 卷组名称
例子如下所示:
[root@root ~]# vgchange -a y /dev/vg0
1 logical volume(s) in volume group "vg0" now active
去激活卷组
去激活命令格式如下:
vgchange –a n 卷组名称
例子如下所示:
[root@root ~]# vgchange -a n /dev/vg0
0 logical volume(s) in volume group "vg0" now active
导出卷组
在集群、数据备份和数据恢复等应用场景时,需要导入或导出卷组。
导出卷组命令格式如下:
vgexport卷组名称
例子如下所示:
[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 ]
导入卷组
导入卷组命令格式如下:
vgimport卷组名称
例子如下所示(在本机上导入卷组):
[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 ]
删除逻辑卷
命令格式如下:
lvremove lvname
例子如下所示:
[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
删除卷组
命令格式如下所示:
vgremove vgname
操作步骤描述如下:
- 确保卷组上的逻辑卷已经全部删除。
[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
- 删除卷组。
[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
删除物理卷
删除物理卷的命令格式如下:
pvremove 裸设备名
例子如下所示:
[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