首页 > 代码库 > Linux 逻辑卷LVM创建扩展删除实例
Linux 逻辑卷LVM创建扩展删除实例
我这里用sdb这个20G的硬盘创建四个5G分区,并使用这四个分区创建test_lvm卷组,最后从卷组中创建两个8G的逻辑卷test_web和test_data.
详细步骤:需要操作和注意的内容均已用深色标注
1,首先对sdb创建分区
[root@localhost ~]# fdisk -l #查看磁盘分区
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x00000000
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000a25c6
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 274 2097152 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 274 2611 18770944 83 Linux
[root@localhost ~]# fdisk /dev/sdb #对sdb进行分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x7c32b0fe.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won‘t be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
switch off the mode (command ‘c‘) and change display units to
sectors (command ‘u‘).
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5G
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (655-2610, default 655):
Last cylinder, +cylinders or +size{K,M,G} (655-2610, default 2610): +5G
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1309-2610, default 1309):
Using default value 1309
Last cylinder, +cylinders or +size{K,M,G} (1309-2610, default 2610): +5G
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Selected partition 4
First cylinder (1963-2610, default 1963):
Using default value 1963
Last cylinder, +cylinders or +size{K,M,G} (1963-2610, default 2610):
Using default value 2610
Command (m for help): n
First cylinder (1963-2610, default 1963):
Using default value 1963
Last cylinder, +cylinders or +size{K,M,G} (1963-2610, default 2610): +5G
Using default value 2610
Command (m for help): t
Partition number (1-5): 1 #指定分区编号
Hex code (type L to list codes): 8e #修改分区标签为LVM
Changed system type of partition 1 to 8e (Linux LVM)
(四个分区都要修改标签为LVM,8e代表是lvm)
Command (m for help): t
Partition number (1-5): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-5): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)
Command (m for help): p #查看分区情况
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x7c32b0fe
Device Boot Start End Blocks Id System
/dev/sdb1 1 654 5253223+ 8e Linux LVM
/dev/sdb2 655 1308 5253255 8e Linux LVM
/dev/sdb3 1309 1962 5253255 8e Linux LVM
/dev/sdb4 1963 2610 5205060 5 Extended
/dev/sdb5 1963 2610 5205028+ 8e Linux LVM
Command (m for help): w #保存分区
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# yum -y install parted (安装part命令)
[root@localhost ~]# partprobe /dev/sdb #让内核读取新的分区表
[root@localhost ~]# yum install lvm-common lvm2 evms #安装lvm命令包
[root@localhost ~]# pvcreate /dev/sdb{1,2,3,5} #初始化分区
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
Physical volume "/dev/sdb3" successfully created
Physical volume "/dev/sdb5" successfully created
[root@localhost ~]# pvdisplay #查看初始化状态
"/dev/sdb1" is a new physical volume of "5.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 5.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID TEc8TG-gvFL-6HRW-Pzds-pgz4-yUat-JVKh9l
.......
[root@localhost ~]# vgcreate test_lvm /dev/sdb{1,2,3,5} #创建test_lvm卷组
Volume group "test_lvm" successfully created
[root@localhost ~]# vgdisplay #查看创建的卷组
--- Volume group ---
VG Name test_lvm
System ID
Format lvm2
Metadata Areas 4
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 4
Act PV 4
VG Size 19.98 GiB
PE Size 4.00 MiB
Total PE 5116
Alloc PE / Size 0 / 0
Free PE / Size 5116 / 19.98 GiB
VG UUID Ds6v3E-0tfi-p5Pm-0s8H-BMoU-LhDu-WmwW2V
[root@localhost ~]# lvcreate -L 8G -n test_web test_lvm #创建test_web逻辑卷,8G
Logical volume "test_web" created.
[root@localhost ~]# lvcreate -L 8G -n test_data test_lvm #创建test_data逻辑卷,8G
Logical volume "test_data" created.
[root@localhost ~]# lvdisplay #查看创建的逻辑卷
--- Logical volume ---
LV Path /dev/test_lvm/test_web
LV Name test_web
VG Name test_lvm
LV UUID ZwkBV8-LOWR-GGk7-643t-Ne15-557M-vwxc08
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2016-12-29 05:27:48 +0800
LV Status available
# open 0
LV Size 8.00 GiB
Current LE 2048
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Logical volume ---
LV Path /dev/test_lvm/test_data
LV Name test_data
VG Name test_lvm
LV UUID mxI9hQ-79uP-R0fw-McA7-483X-TPXf-0ELTdo
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2016-12-29 05:28:12 +0800
LV Status available
# open 0
LV Size 8.00 GiB
Current LE 2048
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
[root@localhost ~]# mkfs.ext4 /dev/test_lvm/test_web #格式化逻辑卷
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
524288 inodes, 2097152 blocks
104857 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2147483648
64 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# mkfs.ext4 /dev/test_lvm/test_data #格式化逻辑卷
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
524288 inodes, 2097152 blocks
104857 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2147483648
64 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# mkdir -p /web #创建挂载点
[root@localhost ~]# mkdir -p /data #创建挂载点
[root@localhost ~]# mount /dev/test_lvm/test_web /web #挂载逻辑卷
[root@localhost ~]# mount /dev/test_lvm/test_data /data/ #挂载逻辑卷
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 992M 16G 6% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 93M 27M 62M 31% /boot
/dev/mapper/test_lvm-test_web
7.8G 18M 7.4G 1% /web
/dev/mapper/test_lvm-test_data
7.8G 18M 7.4G 1% /data
增大逻辑卷
[root@localhost ~]# lvextend -L +2G /dev/test_lvm/test_web #扩大逻辑卷,加2G
Size of logical volume test_lvm/test_web changed from 8.00 GiB (2048 extents) to 10.00 GiB (2560 extents).
Logical volume test_web successfully resized.
[root@localhost ~]# resize2fs /dev/test_lvm/test_web #格式化增大的逻辑卷
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/test_lvm/test_web is mounted on /web; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/test_lvm/test_web to 2621440 (4k) blocks.
The filesystem on /dev/test_lvm/test_web is now 2621440 blocks long.
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 992M 16G 6% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 93M 27M 62M 31% /boot
/dev/mapper/test_lvm-test_web
9.8G 18M 9.3G 1% /web
/dev/mapper/test_lvm-test_data
7.8G 18M 7.4G 1% /data
删除LVM分区
[root@localhost ~]# umount /dev/test_lvm/test_web #卸载逻辑卷
[root@localhost ~]# umount /dev/test_lvm/test_data
[root@localhost ~]# lvremove /dev/test_lvm/test_web #删除逻辑卷
Do you really want to remove active logical volume test_web? [y/n]: y
Logical volume "test_web" successfully removed
[root@localhost ~]# lvremove /dev/test_lvm/test_data
Do you really want to remove active logical volume test_data? [y/n]: y
Logical volume "test_data" successfully removed
[root@localhost ~]# vgremove test_lvm #删除卷组
Volume group "test_lvm" successfully removed
[root@localhost ~]# pvremove /dev/sdb{1,2,3,5} #去除PV还原磁盘
Labels on physical volume "/dev/sdb1" successfully wiped
Labels on physical volume "/dev/sdb2" successfully wiped
Labels on physical volume "/dev/sdb3" successfully wiped
Labels on physical volume "/dev/sdb5" successfully wiped
本文出自 “实用Linux知识技能分享” 博客,请务必保留此出处http://superleedo.blog.51cto.com/12164670/1887178
Linux 逻辑卷LVM创建扩展删除实例