Linux系统中对逻辑卷(LVM)的实现
1.分区并转换lvm格式
[root@localhost ~]# fdisk -cu /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xd8e8fb90.
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)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-4485119, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4485119, default 4485119): +200M
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First sector (411648-4485119, default 411648):
Using default value 411648
Last sector, +sectors or +size{K,M,G} (411648-4485119, default 4485119): +200M
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First sector (821248-4485119, default 821248):
Using default value 821248
Last sector, +sectors or +size{K,M,G} (821248-4485119, default 4485119): +200M
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Selected partition 4
First sector (1230848-4485119, default 1230848):
Using default value 1230848
Last sector, +sectors or +size{K,M,G} (1230848-4485119, default 4485119): +200M
Command (m for help): p
Disk /dev/sdb: 2296 MB, 2296381440 bytes
255 heads, 63 sectors/track, 279 cylinders, total 4485120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd8e8fb90
Device Boot Start End Blocks Id System
/dev/sdb1 2048 411647 204800 83 Linux
/dev/sdb2 411648 821247 204800 83 Linux
/dev/sdb3 821248 1230847 204800 83 Linux
/dev/sdb4 1230848 1640447 204800 83 Linux
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access
a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
f W95 Ext’d (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT
1e Hidden W95 FAT1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-4): 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-4): 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-4): 4
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sdb: 2296 MB, 2296381440 bytes
255 heads, 63 sectors/track, 279 cylinders, total 4485120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd8e8fb90
Device Boot Start End Blocks Id System
/dev/sdb1 2048 411647 204800 8e Linux LVM
/dev/sdb2 411648 821247 204800 8e Linux LVM
/dev/sdb3 821248 1230847 204800 8e Linux LVM
/dev/sdb4 1230848 1640447 204800 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 ~]#
2.将分区转换为pv物理卷,将pv加入到一个vg组中,从vg划出lv逻辑卷
[root@localhost ~]# pvcreate /dev/sdb{1,2,3,4} –将分区转换为物理卷
Physical volume “/dev/sdb1” successfully created
Physical volume “/dev/sdb2” successfully created
Physical volume “/dev/sdb3” successfully created
Physical volume “/dev/sdb4” successfully created
[root@localhost ~]# pvscan –查看物理卷
PV /dev/sda2 VG VolGroup lvm2 [7.51 GiB / 0 free]
PV /dev/sdb1 lvm2 [200.00 MiB]
PV /dev/sdb2 lvm2 [200.00 MiB]
PV /dev/sdb3 lvm2 [200.00 MiB]
PV /dev/sdb4 lvm2 [200.00 MiB]
Total: 5 [8.29 GiB] / in use: 1 [7.51 GiB] / in no VG: 4 [800.00 MiB]
[root@localhost ~]# vgcreate vg0 /dev/sdb{1,2,3} –将物理卷1,2,3加入到卷组
Volume group “vg0” successfully created
[root@localhost ~]# vgscan –查看卷组
Reading all physical volumes. This may take a while…
Found volume group “vg0” using metadata type lvm2
Found volume group “VolGroup” using metadata type lvm2
[root@localhost ~]# lvcreate -n test -L 150M vg0 –从卷组中分出pv逻辑卷
Rounding up size to full physical extent 152.00 MiB
Logical volume “test” created
[root@localhost ~]# lvcreate -n test1 -L 150M vg0
Rounding up size to full physical extent 152.00 MiB
Logical volume “test1” created
[root@localhost ~]# lvscan
ACTIVE ‘/dev/vg0/test’ [152.00 MiB] inherit
ACTIVE ‘/dev/vg0/test1’ [152.00 MiB] inherit
ACTIVE ‘/dev/VolGroup/lv_root’ [6.51 GiB] inherit
ACTIVE ‘/dev/VolGroup/lv_swap’ [1.00 GiB] inherit
[root@localhost ~]# mkfs.ext4 /dev/vg0/test –格式化物理卷
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
38912 inodes, 155648 blocks
7782 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
19 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# mount /dev/vg0/test /mnt/sdb –挂载
[root@localhost ~]# mkdir /mnt/sdb/12 –可以使用了
[root@localhost ~]# ll /mnt/sdb/
total 14
drwxr-xr-x. 2 root root 1024 Jan 9 15:24 12
drwx——. 2 root root 12288 Jan 9 15:23 lost+found
[root@localhost ~]#
相关阅读:
mysqldump和LVM逻辑卷快照 http://www.linuxidc.com/Linux/2013-09/90382.htm
LVM的综合性介绍及实现 http://www.linuxidc.com/Linux/2013-03/81482.htm
MySQL高性能备份方案解决数据不间断访问(LVM快照方式备份) http://www.linuxidc.com/Linux/2013-07/87887.htm
LVM上实施ASM http://www.linuxidc.com/Linux/2012-11/73475.htm
3.扩大卷组(vg),扩大逻辑卷(pv)
[root@localhost ~]# vgdisplay 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 2
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size 588.00 MiB –卷组的大小是588M
PE Size 4.00 MiB
Total PE 147
Alloc PE / Size 76 / 304.00 MiB
Free PE / Size 71 / 284.00 MiB
VG UUID Pau9QC-3eru-dhF2-nJ3C-clpZ-vbDy-X023YM
[root@localhost ~]# lvdisplay /dev/vg0/test –查看lv(test)的大小
— Logical volume —
LV Path /dev/vg0/test
LV Name test
VG Name vg0
LV UUID uY4frU-gSy4-Pkwc-76Tw-Nh04-M7Ob-Tz8fPY
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2014-01-09 15:22:40 +0800
LV Status available
# open 1
LV Size 152.00 MiB –lv的大小是152M
Current LE 38
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:2
[root@localhost ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
ext4 6.9G 6.4G 171M 98% /
tmpfs tmpfs 262M 0 262M 0% /dev/shm
/dev/sda1 ext4 508M 48M 435M 10% /boot
/dev/mapper/vg0-test
ext4 155M 5.8M 141M 4% /mnt/sdb –挂载后的大小是155M
[root@localhost ~]# lvextend -L +50M /dev/vg0/test –将逻辑卷test增加50M
Rounding size to boundary between physical extents: 52.00 MiB
Extending logical volume test to 204.00 MiB
Logical volume test successfully resized
[root@localhost ~]# lvdisplay /dev/vg0/test –显示test逻辑卷的大小
— Logical volume —
LV Path /dev/vg0/test
LV Name test
VG Name vg0
LV UUID uY4frU-gSy4-Pkwc-76Tw-Nh04-M7Ob-Tz8fPY
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2014-01-09 15:22:40 +0800
LV Status available
# open 1
LV Size 204.00 MiB –大小变为204M
Current LE 51
Segments 2
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:2
[root@localhost ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
ext4 6.9G 6.4G 171M 98% /
tmpfs tmpfs 262M 0 262M 0% /dev/shm
/dev/sda1 ext4 508M 48M 435M 10% /boot
/dev/mapper/vg0-test
ext4 155M 5.8M 141M 4% /mnt/sdb –挂载的目录还没有变
[root@localhost ~]# resize2fs -f /dev/vg0/test –强制修改分区的大小
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/test is mounted on /mnt/sdb; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg0/test to 208896 (1k) blocks.
The filesystem on /dev/vg0/test is now 208896 blocks long.
[root@localhost ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
ext4 6.9G 6.4G 171M 98% /
tmpfs tmpfs 262M 0 262M 0% /dev/shm
/dev/sda1 ext4 508M 48M 435M 10% /boot
/dev/mapper/vg0-test
ext4 208M 6.1M 191M 4% /mnt/sdb –大小改变了
[root@localhost ~]#
[root@localhost ~]# vgdisplay 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 2
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size 588.00 MiB –卷组的大小是588M
PE Size 4.00 MiB
Total PE 147
Alloc PE / Size 76 / 304.00 MiB
Free PE / Size 71 / 284.00 MiB
VG UUID Pau9QC-3eru-dhF2-nJ3C-clpZ-vbDy-X023YM
[root@localhost ~]# vgextend vg0 /dev/sdb4 –将分区4加入到vg卷组中
Volume group “vg0” successfully extended
[root@localhost ~]# vgdisplay vg0 –查看卷组
— Volume group —
VG Name vg0
System ID
Format lvm2
Metadata Areas 4
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 4
Act PV 4
VG Size 784.00 MiB –卷组大小为784M
PE Size 4.00 MiB
Total PE 196
Alloc PE / Size 76 / 304.00 MiB
Free PE / Size 120 / 480.00 MiB
VG UUID Pau9QC-3eru-dhF2-nJ3C-clpZ-vbDy-X023YM
[root@localhost ~]#
4.减小卷组(vg),减小逻辑卷(pv)
[root@localhost ~]# pvscan –查看卷组大小
PV /dev/sdb1 VG vg0 lvm2 [196.00 MiB / 0 free]
PV /dev/sdb2 VG vg0 lvm2 [196.00 MiB / 36.00 MiB free]
PV /dev/sdb3 VG vg0 lvm2 [196.00 MiB / 196.00 MiB free]
PV /dev/sdb4 VG vg0 lvm2 [196.00 MiB / 196.00 MiB free]
PV /dev/sda2 VG VolGroup lvm2 [7.51 GiB / 0 free]
Total: 5 [8.27 GiB] / in use: 5 [8.27 GiB] / in no VG: 0 [0 ]
[root@localhost ~]# vgreduce vg0 /dev/sdb4 –将物理卷4移出vg卷组中
Removed “/dev/sdb4” from volume group “vg0”
[root@localhost ~]# pvscan –查看vg卷组的大小
PV /dev/sdb1 VG vg0 lvm2 [196.00 MiB / 0 free]
PV /dev/sdb2 VG vg0 lvm2 [196.00 MiB / 36.00 MiB free]
PV /dev/sdb3 VG vg0 lvm2 [196.00 MiB / 196.00 MiB free]
PV /dev/sda2 VG VolGroup lvm2 [7.51 GiB / 0 free]
PV /dev/sdb4 lvm2 [200.00 MiB]
Total: 5 [8.28 GiB] / in use: 4 [8.08 GiB] / in no VG: 1 [200.00 MiB]
[root@localhost ~]# vgdisplay vg0 –查看卷组的大小
— Volume group —
VG Name vg0
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size 588.00 MiB
PE Size 4.00 MiB
Total PE 147
Alloc PE / Size 89 / 356.00 MiB
Free PE / Size 58 / 232.00 MiB
VG UUID Pau9QC-3eru-dhF2-nJ3C-clpZ-vbDy-X023YM
[root@localhost ~]# umount /mnt/sdb/ –减小逻辑卷时必须卸载
[root@localhost ~]# resize2fs -f /dev/vg0/test 100M –改变逻辑卷的大小100M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg0/test to 102400 (1k) blocks.
The filesystem on /dev/vg0/test is now 102400 blocks long.
[root@localhost ~]# resize2fs -f /dev/vg0/test 100M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg0/test to 102400 (1k) blocks.
The filesystem on /dev/vg0/test is now 102400 blocks long.
[root@localhost ~]# lvreduce -L 100M /dev/vg0/test –将逻辑卷test大小调为100M
WARNING: Reducing active logical volume to 100.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce test? [y/n]: y –输入y确认
Reducing logical volume test to 100.00 MiB
Logical volume test successfully resized
[root@localhost ~]# mount /dev/vg0/test /mnt/sdb/ –挂载逻辑卷
[root@localhost ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
ext4 6.9G 6.4G 171M 98% /
tmpfs tmpfs 262M 0 262M 0% /dev/shm
/dev/sda1 ext4 508M 48M 435M 10% /boot
/dev/mapper/vg0-test
ext4 102M 5.8M 91M 7% /mnt/sdb –逻辑卷test大小变为100M
[root@localhost ~]#
5.创建逻辑卷快照
[root@localhost ~]# cp -a /etc/passwd /etc/shadow /mnt/sdb/
[root@localhost ~]# ll /mnt/sdb/
total 142
drwxr-xr-x. 2 root root 1024 Jan 9 15:24 12
drwx——. 2 root root 12288 Jan 9 15:23 lost+found
-rw——-. 1 root root 125811 Feb 22 2013 moduli
-rw-r–r–. 1 root root 953 Jan 9 13:09 passwd
———-. 1 root root 627 Jan 9 13:09 shadow
[root@localhost ~]# lvcreate -s -L 50M -n sanp /dev/vg0/test –创建50M的逻辑快照
Rounding up size to full physical extent 52.00 MiB
Logical volume “sanp” created
[root@localhost ~]# mkdir /mnt/sdb2
[root@localhost ~]# mount /dev/vg0/sanp /mnt/sdb2/ –挂载快照
[root@localhost ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
ext4 6.9G 6.4G 171M 98% /
tmpfs tmpfs 262M 0 262M 0% /dev/shm
/dev/sda1 ext4 508M 48M 435M 10% /boot
/dev/mapper/vg0-test
ext4 102M 6.0M 91M 7% /mnt/sdb –逻辑卷大小
/dev/mapper/vg0-sanp
ext4 102M 6.0M 91M 7% /mnt/sdb2 –快照大小
[root@localhost ~]# ll /mnt/sdb2/
total 142
drwxr-xr-x. 2 root root 1024 Jan 9 15:24 12
drwx——. 2 root root 12288 Jan 9 15:23 lost+found
-rw——-. 1 root root 125811 Feb 22 2013 moduli
-rw-r–r–. 1 root root 953 Jan 9 13:09 passwd
———-. 1 root root 627 Jan 9 13:09 shadow
[root@localhost ~]#
6.删除卷组(vg),删除物理卷(pv),删除逻辑卷(lv)
[root@localhost ~]# lvscan –查看逻辑卷
ACTIVE Original ‘/dev/vg0/test’ [100.00 MiB] inherit
ACTIVE ‘/dev/vg0/test1’ [152.00 MiB] inherit
ACTIVE Snapshot ‘/dev/vg0/sanp’ [52.00 MiB] inherit
ACTIVE Snapshot ‘/dev/vg0/sanp1’ [52.00 MiB] inherit
ACTIVE ‘/dev/VolGroup/lv_root’ [6.51 GiB] inherit
ACTIVE ‘/dev/VolGroup/lv_swap’ [1.00 GiB] inherit
[root@localhost ~]# lvremove /dev/vg0/test –移除逻辑卷(必须卸载所有目录)
Do you really want to remove active logical volume sanp? [y/n]: y –输入y确认
Logical volume “sanp” successfully removed
Do you really want to remove active logical volume sanp1? [y/n]: y –输入y确认
Logical volume “sanp1” successfully removed
Do you really want to remove active logical volume test? [y/n]: y –输入y确认
Logical volume “test” successfully removed
[root@localhost ~]# vgscan –查看卷组(vg)
Reading all physical volumes. This may take a while…
Found volume group “vg0” using metadata type lvm2
Found volume group “VolGroup” using metadata type lvm2
[root@localhost ~]# vgremove vg0 –移除卷组(vg)
Do you really want to remove volume group “vg0” containing 1 logical volumes? [y/n]: y –输入y确认
Do you really want to remove active logical volume test1? [y/n]: y –输入y确认
Logical volume “test1” successfully removed
Volume group “vg0” successfully removed
[root@localhost ~]# pvscan
PV /dev/sda2 VG VolGroup lvm2 [7.51 GiB / 0 free]
PV /dev/sdb1 lvm2 [200.00 MiB]
PV /dev/sdb2 lvm2 [200.00 MiB]
PV /dev/sdb3 lvm2 [200.00 MiB]
PV /dev/sdb4 lvm2 [200.00 MiB]
Total: 5 [8.29 GiB] / in use: 1 [7.51 GiB] / in no VG: 4 [800.00 MiB]
[root@localhost ~]# pvremove /dev/sdb{1,2,3,4} –移除物理卷
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/sdb4” successfully wiped
[root@localhost ~]#
7.在lvm逻辑卷中常用的命令
pvcreate –建物理分区建成pv
pvscan –显示具有pv的磁盘
pvdisplay –列出系统上的pv
pvremove –删除pv属性
vgcreate –创建vg命令
vgscan –查找系统中是否有vg存在
vgdisplay –目前系统vg的状态
vgextend –在vg中增加pv
vgremove –删除一个vg
vgchange –设置vg是否启动
vgreduce –在vg中删除pv
lvcreate –创建lv
lvscan –查询系统上的lv
lvdisplay –显示系统上的lv状态
lvextend –在lv中增加容量
lvreduce –在lv中减少容量
lvremove –删除lv
lvresize –对lv的容量进行调整