Linux系统中对逻辑卷(lvm)的实现

本文发布时间: 2019-Mar-22
1.分区并转换lvm格式[root@localhost ~]# fdisk -cu /dev/sdbDevice contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabelBuilding 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): nCommand actione extendedp primary partition (1-4)pPartition number (1-4): 1First sector (2048-4485119, default 2048):Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-4485119, default 4485119): +200MCommand (m for help): nCommand actione extendedp primary partition (1-4)pPartition number (1-4): 2First sector (411648-4485119, default 411648):Using default value 411648Last sector, +sectors or +size{K,M,G} (411648-4485119, default 4485119): +200MCommand (m for help): nCommand actione extendedp primary partition (1-4)pPartition number (1-4): 3First sector (821248-4485119, default 821248):Using default value 821248Last sector, +sectors or +size{K,M,G} (821248-4485119, default 4485119): +200MCommand (m for help): nCommand actione extendedp primary partition (1-4)pSelected partition 4First sector (1230848-4485119, default 1230848):Using default value 1230848Last sector, +sectors or +size{K,M,G} (1230848-4485119, default 4485119): +200MCommand (m for help): pDisk /dev/sdb: 2296 MB, 2296381440 bytes255 heads, 63 sectors/track, 279 cylinders, total 4485120 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xd8e8fb90Device 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 LinuxCommand (m for help): tPartition number (1-4): 1Hex code (type L to list codes): L0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris1 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 Syrinx5 Extended 42 SFS 86 NTFS volume set da Non-FS data6 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 Utility8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS accessa OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/Ob W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStorc W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fse W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPTf 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 b11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT1e Hidden W95 FAT1Hex code (type L to list codes): 8eChanged system type of partition 1 to 8e (Linux LVM)Command (m for help): tPartition number (1-4): 2Hex code (type L to list codes): 8eChanged system type of partition 2 to 8e (Linux LVM)Command (m for help): tPartition number (1-4): 3Hex code (type L to list codes): 8eChanged system type of partition 3 to 8e (Linux LVM)Command (m for help): tPartition number (1-4): 4Hex code (type L to list codes): 8eChanged system type of partition 4 to 8e (Linux LVM)Command (m for help): pDisk /dev/sdb: 2296 MB, 2296381440 bytes255 heads, 63 sectors/track, 279 cylinders, total 4485120 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xd8e8fb90Device 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 LVMCommand (m for help): wThe 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 createdPhysical volume "/dev/sdb2" successfully createdPhysical volume "/dev/sdb3" successfully createdPhysical 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 lvm2Found 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 MiBLogical volume "test" created[root@localhost ~]# lvcreate -n test1 -L 150M vg0Rounding up size to full physical extent 152.00 MiBLogical volume "test1" created[root@localhost ~]# lvscanACTIVE '/dev/vg0/test' [152.00 MiB] inheritACTIVE '/dev/vg0/test1' [152.00 MiB] inheritACTIVE '/dev/VolGroup/lv_root' [6.51 GiB] inheritACTIVE '/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: LinuxBlock size=1024 (log=0)Fragment size=1024 (log=0)Stride=0 blocks, Stripe width=0 blocks38912 inodes, 155648 blocks7782 blocks (5.00%) reserved for the super userFirst data block=1Maximum filesystem blocks=6737100819 block groups8192 blocks per group, 8192 fragments per group2048 inodes per groupSuperblock backups stored on blocks:8193, 24577, 40961, 57345, 73729Writing inode tables: doneCreating journal (4096 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 26 mounts or180 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 14drwxr-xr-x. 2 root root 1024 Jan 9 15:24 12drwx------. 2 root root 12288 Jan 9 15:23 lost+found[root@localhost ~]#3.扩大卷组(vg),扩大逻辑卷(pv)[root@localhost ~]# vgdisplay vg0--- Volume group ---VG Name vg0System IDFormat lvm2Metadata Areas 3Metadata Sequence No 3VG Access read/writeVG Status resizableMAX LV 0Cur LV 2Open LV 1Max PV 0Cur PV 3Act PV 3VG Size 588.00 MiB --卷组的大小是588MPE Size 4.00 MiBTotal PE 147Alloc PE / Size 76 / 304.00 MiBFree PE / Size 71 / 284.00 MiBVG UUID Pau9QC-3eru-dhF2-nJ3C-clpZ-vbDy-X023YM[root@localhost ~]# lvdisplay /dev/vg0/test --查看lv(test)的大小--- Logical volume ---LV Path /dev/vg0/testLV Name testVG Name vg0LV UUID uY4frU-gSy4-Pkwc-76Tw-Nh04-M7Ob-Tz8fPYLV Write Access read/writeLV Creation host, time localhost.localdomain, 2014-01-09 15:22:40 +0800LV Status available# open 1LV Size 152.00 MiB --lv的大小是152MCurrent LE 38Segments 1Allocation inheritRead ahead sectors auto- currently set to 256Block device 253:2[root@localhost ~]# df -THFilesystem Type Size Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_rootext4 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-testext4 155M 5.8M 141M 4% /mnt/sdb --挂载后的大小是155M[root@localhost ~]# lvextend -L +50M /dev/vg0/test --将逻辑卷test增加50MRounding size to boundary between physical extents: 52.00 MiBExtending logical volume test to 204.00 MiBLogical volume test successfully resized[root@localhost ~]# lvdisplay /dev/vg0/test --显示test逻辑卷的大小--- Logical volume ---LV Path /dev/vg0/testLV Name testVG Name vg0LV UUID uY4frU-gSy4-Pkwc-76Tw-Nh04-M7Ob-Tz8fPYLV Write Access read/writeLV Creation host, time localhost.localdomain, 2014-01-09 15:22:40 +0800LV Status available# open 1LV Size 204.00 MiB --大小变为204MCurrent LE 51Segments 2Allocation inheritRead ahead sectors auto- currently set to 256Block device 253:2[root@localhost ~]# df -THFilesystem Type Size Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_rootext4 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-testext4 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 requiredold desc_blocks = 1, new_desc_blocks = 1Performing 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 -THFilesystem Type Size Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_rootext4 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-testext4 208M 6.1M 191M 4% /mnt/sdb --大小改变了[root@localhost ~]#[root@localhost ~]# vgdisplay vg0 --查看卷组--- Volume group ---VG Name vg0System IDFormat lvm2Metadata Areas 3Metadata Sequence No 3VG Access read/writeVG Status resizableMAX LV 0Cur LV 2Open LV 1Max PV 0Cur PV 3Act PV 3VG Size 588.00 MiB --卷组的大小是588MPE Size 4.00 MiBTotal PE 147Alloc PE / Size 76 / 304.00 MiBFree PE / Size 71 / 284.00 MiBVG 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 vg0System IDFormat lvm2Metadata Areas 4Metadata Sequence No 4VG Access read/writeVG Status resizableMAX LV 0Cur LV 2Open LV 1Max PV 0Cur PV 4Act PV 4VG Size 784.00 MiB --卷组大小为784MPE Size 4.00 MiBTotal PE 196Alloc PE / Size 76 / 304.00 MiBFree PE / Size 120 / 480.00 MiBVG 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 vg0System IDFormat lvm2Metadata Areas 3Metadata Sequence No 6VG Access read/writeVG Status resizableMAX LV 0Cur LV 2Open LV 1Max PV 0Cur PV 3Act PV 3VG Size 588.00 MiBPE Size 4.00 MiBTotal PE 147Alloc PE / Size 89 / 356.00 MiBFree PE / Size 58 / 232.00 MiBVG UUID Pau9QC-3eru-dhF2-nJ3C-clpZ-vbDy-X023YM[root@localhost ~]# umount /mnt/sdb/ --减小逻辑卷时必须卸载[root@localhost ~]# resize2fs -f /dev/vg0/test 100M --改变逻辑卷的大小100Mresize2fs 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 100Mresize2fs 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大小调为100MWARNING: Reducing active logical volume to 100.00 MiBTHIS MAY DESTROY YOUR DATA (filesystem etc.)Do you really want to reduce test? [y/n]: y --输入y确认Reducing logical volume test to 100.00 MiBLogical volume test successfully resized[root@localhost ~]# mount /dev/vg0/test /mnt/sdb/ --挂载逻辑卷[root@localhost ~]# df -THFilesystem Type Size Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_rootext4 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-testext4 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 142drwxr-xr-x. 2 root root 1024 Jan 9 15:24 12drwx------. 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 MiBLogical volume "sanp" created[root@localhost ~]# mkdir /mnt/sdb2[root@localhost ~]# mount /dev/vg0/sanp /mnt/sdb2/ --挂载快照[root@localhost ~]# df -THFilesystem Type Size Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_rootext4 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-testext4 102M 6.0M 91M 7% /mnt/sdb --逻辑卷大小/dev/mapper/vg0-sanpext4 102M 6.0M 91M 7% /mnt/sdb2 --快照大小[root@localhost ~]# ll /mnt/sdb2/total 142drwxr-xr-x. 2 root root 1024 Jan 9 15:24 12drwx------. 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] inheritACTIVE '/dev/vg0/test1' [152.00 MiB] inheritACTIVE Snapshot '/dev/vg0/sanp' [52.00 MiB] inheritACTIVE Snapshot '/dev/vg0/sanp1' [52.00 MiB] inheritACTIVE '/dev/VolGroup/lv_root' [6.51 GiB] inheritACTIVE '/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 removedDo you really want to remove active logical volume sanp1? [y/n]: y --输入y确认Logical volume "sanp1" successfully removedDo 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 lvm2Found 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 removedVolume group "vg0" successfully removed[root@localhost ~]# pvscanPV /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 wipedLabels on physical volume "/dev/sdb2" successfully wipedLabels on physical volume "/dev/sdb3" successfully wipedLabels on physical volume "/dev/sdb4" successfully wiped[root@localhost ~]#7.在lvm逻辑卷中常用的命令pvcreate --建物理分区建成pvpvscan --显示具有pv的磁盘pvdisplay --列出系统上的pvpvremove --删除pv属性vgcreate --创建vg命令vgscan --查找系统中是否有vg存在vgdisplay --目前系统vg的状态vgextend --在vg中增加pvvgremove --删除一个vgvgchange --设置vg是否启动vgreduce --在vg中删除pvlvcreate --创建lvlvscan --查询系统上的lvlvdisplay --显示系统上的lv状态lvextend --在lv中增加容量lvreduce --在lv中减少容量lvremove --删除lvlvresize --对lv的容量进行调整


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:10pm
栏目列表