1. Partition and convert lvm format
[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. Convert the partition to the pv physical volume, add the pv to a vg group, and draw the lv logical volume from the vg.
[Root @ localhost ~] # Pvcreate/dev/sdb {1, 2, 3, 4} -- convert partitions to Physical volumes Physical volume "/dev/sdb1" successfully createdPhysical volume "/dev/sdb2" successfully createdPhysical volume "/dev/sdb3" successfully createdPhysical volume "/ dev/sdb4 "successfully created [root @ localhost ~] # Pvscan -- view the physical volume 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} -- add physical volumes 1, 2, and 3 to the Volume group "vg0" successfully created [root @ localhost ~] # Vgscan -- view the volume group 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 150 M vg0 -- separate the pv logical volume from the volume group Rounding up size to full physical extent 152.00 MiBLogical volume "test" created [root @ localhost ~] # Lvcreate-n test1-L 150 M vswitchrounding 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 -- format the physical volume 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 = 1 Maximum filesystem blocks = 6737100819 block groups8192 blocks per group, 8192 fragments per group2048 inodes per groupSuperblock B Ackups stored on blocks: 8193,245 77, 40961,573 45, 73729 Writing 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 -- mount [root @ localhost ~] # Mkdir/mnt/sdb/12 -- you can use [root @ localhost ~] # Ll/mnt/sdb/total 14drwxr-xr-x. 2 root 1024 Jan 9 12drwx ------. 2 root 12288 Jan 9 lost + found [root @ localhost ~] #
3. Expand the volume group (vg) and expand the logical volume (pv)
[Root @ localhost ~] # Vgdisplay vg0 --- Volume group --- VG Name vg0System IDFormat lvm2Metadata Areas 3 Metadata Sequence No 3VG Access read/writeVG Status resizableMAX LV 0Cur LV 2 Open LV 1Max PV 0Cur PV 3Act PV 3VG Size 588.00 MiB -- The volume group Size is 588MPE Size 4.00 MiBTotal PE 147 Alloc 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 -- View lv (test) --- Logical volume --- LV Path/dev/vg0/testLV Name testVG Name v1_lv UUID uY4frU-gSy4-Pkwc-76Tw-Nh04-M7Ob-Tz8fPYLV Write Access read/writeLV Creation host, time localhost. localdomain, 15:22:40 + 0800LV Status available # open 1LV Size 152.00 MiB -- lv Size is 152 MCurrent LE 38 Segments 1 Allocation inheritRead ahead sectors auto-currently set to 256 Block d Evice 253: 2 [root @ localhost ~] # Df-THFilesystem Type Size Used Avail Use % Mounted on/dev/mapper/VolGroup-lv_rootext4 6.9G 6.4G 171 M 98%/tmpfs 262 M 0 262 M 0%/dev/shm/dev /sda1 ext4 508 M 48 M 435 M 10%/boot/dev/mapper/vg0-testext4 155 M 5.8 M 141 M 4%/mnt/sdb -- the size after mounting is 155 M [root @ localhost ~] # Lvextend-L + 50 M/dev/vg0/test -- increase the logical volume test by 50 MRounding 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 -- display the size of the test Logical volume --- LV Path/dev/vg0/testLV Name testVG Name v1_lv UUID uY4frU-gSy4-Pkwc-76Tw-Nh04-M7Ob-Tz8fPYLV Write Access read/writeLV Creation host, time localhost. localdomain, 15:22:40 + 0800LV Status available # open 1LV Size 204.00 MiB -- Size changed to 204 MCurrent LE 51 Segments 2 Allocation inheritRead ahead sectors auto-currently set to 256 Block devi Ce 253: 2 [root @ localhost ~] # Df-THFilesystem Type Size Used Avail Use % Mounted on/dev/mapper/VolGroup-lv_rootext4 6.9G 6.4G 171 M 98%/tmpfs 262 M 0 262 M 0%/dev/shm/dev /sda1 ext4 508 M 48 M 435 M 10%/boot/dev/mapper/vg0-testext4 155 M 5.8 M 141 M 4%/mnt/sdb -- the mounted directory has not changed [root @ localhost ~] # Resize2fs-f/dev/vg0/test -- force modify the partition size 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 = 1 sort Ming an on-line resize of/dev/vg0/test to 208896 (1 k) 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 171 M 98%/tmpfs 262 M 0 262 M 0%/dev/shm/dev /sda1 ext4 508 M 48 M 435 M 10%/boot/dev/mapper/vg0-testext4 208 M 6.1 M 191 M 4%/mnt/sdb -- size changed [root @ localhost ~] # [Root @ localhost ~] # Vgdisplay vg0 -- View Volume group --- VG Name v1_system IDFormat lvm2Metadata Areas 3 Metadata Sequence No 3VG Access read/writeVG Status resizableMAX LV 0Cur LV 2 Open LV 1Max PV 0Cur PV 3Act PV 3VG Size 588.00 MiB -- the volume group Size is 588MPE Size 4.00 MiBTotal PE 147 Alloc 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 -- add partition 4 to the Volume group "vg0" successfully extended [root @ localhost ~] In the vg Volume group # Vgdisplay vg0 -- View Volume group --- VG Name v1_system IDFormat lvm2Metadata Areas 4 Metadata Sequence No 4VG Access read/writeVG Status resizableMAX LV 0Cur LV 2 Open LV 1Max PV 0Cur PV 4Act PV 4VG Size 784.00 MiB -- the volume group Size is 784MPE Size 4.00 MiBTotal PE 196 Alloc PE/Size 76/304 .00 MiBFree PE/Size 120/480 .00 MiBVG UUID Pau9QC-3eru-dhF2-nJ3C-clpZ-vbDy-X023YM [root @ localhost ~] #
4. Reduce the volume group (vg) and the logical volume (pv)
[Root @ localhost ~] # Pvscan -- view the volume group size 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 -- Remove physical volume 4 from the vg volume group Removed "/dev/sdb4" from volume group "vg0" [root @ localhost ~] # Pvscan -- view the size of the vg volume group 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 -- view the Volume group size --- Volume group --- VG Name v1_system IDFormat lvm2Metadata Areas 3 Metadata Sequence No 6VG Access read/writeVG Status resizableMAX LV 0Cur LV 2 Open LV 1Max PV 0Cur PV 3Act PV 3VG Size 588.00 MiBPE Size 4.00 MiBTotal PE 147 Alloc 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/-- when reducing the logical volume, you must uninstall [root @ localhost ~] # Resize2fs-f/dev/vg0/test 100 M -- change the logical volume size by 100Mresize2fs 1.41.12 (17-May-2010) resizing the filesystem on/dev/vg0/test to 102400 (1 k) 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 (1 k) blocks. the filesystem on/dev/vg0/test is now 102400 blocks long. [root @ localhost ~] # Lvreduce-L 100 M/dev/vg0/test -- Adjust the logical volume test size to 100 MWARNING: Variable active logical volume to 100.00 MiBTHIS may destroy your data (filesystem etc .) do you really want to reduce test? [Y/n]: y -- enter y to confirm the configuration of the volume test to 100.00 MiBLogical volume test successfully resized [root @ localhost ~] # Mount/dev/vg0/test/mnt/sdb/-- mount the logical volume [root @ localhost ~] # Df-THFilesystem Type Size Used Avail Use % Mounted on/dev/mapper/VolGroup-lv_rootext4 6.9G 6.4G 171 M 98%/tmpfs 262 M 0 262 M 0%/dev/shm/dev /sda1 ext4 508 M 48 M 435 M 10%/boot/dev/mapper/vg0-testext4 102 M 5.8 M 91 M 7%/mnt/sdb -- Logical volume test size changed to 100 M [root @ localhost ~] #
5. Create a logical volume Snapshot
[Root @ localhost ~] # Cp-a/etc/passwd/etc/shadow/mnt/sdb/[root @ localhost ~] # Ll/mnt/sdb/total 142drwxr-xr-x. 2 root 1024 Jan 9 12drwx ------. 2 root 12288 Jan 9 lost + found-rw -------. 1 root 125811 Feb 22 2013 moduli-rw-r --. 1 root 953 Jan 9 13:09 passwd ----------. 1 root 627 Jan 9 13:09 shadow [root @ localhost ~] # Lvcreate-s-L 50 M-n sanp/dev/vg0/test -- create a 50 M logical snapshot 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/-- mount a snapshot [root @ localhost ~] # Df-THFilesystem Type Size Used Avail Use % Mounted on/dev/mapper/VolGroup-lv_rootext4 6.9G 6.4G 171 M 98%/tmpfs 262 M 0 262 M 0%/dev/shm/dev /sda1 ext4 508 M 48 M 435 M 10%/boot/dev/mapper/vg0-testext4 102 M 6.0 M 91 M 7%/mnt/sdb -- Logical volume size/dev/mapper/vg0-sanpext4 102 M 6.0 M 91 M 7%/mnt/sdb2 -- snapshot size [root @ localhost ~] # Ll/mnt/sdb2/total 142drwxr-xr-x. 2 root 1024 Jan 9 12drwx ------. 2 root 12288 Jan 9 lost + found-rw -------. 1 root 125811 Feb 22 2013 moduli-rw-r --. 1 root 953 Jan 9 13:09 passwd ----------. 1 root 627 Jan 9 13:09 shadow [root @ localhost ~] #
6. Delete the volume group (vg), delete the physical volume (pv), and delete the logical volume (lv)
[Root @ localhost ~] # Lvscan -- view the logical volume 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 -- remove a logical volume (all directories must be detached) Do you really want to remove active logical volume sanp? [Y/n]: y -- enter y to confirm Logical volume "sanp" successfully removedDo you really want to remove active logical volume sanp1? [Y/n]: y -- enter y to confirm Logical volume "sanp1" successfully removedDo you really want to remove active logical volume test? [Y/n]: y -- enter y to confirm Logical volume "test" successfully removed [root @ localhost ~] # Vgscan -- view the volume group (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 -- remove volume group (vg) Do you really want to remove volume group "vg0" containing 1 logical volumes? [Y/n]: y -- enter y to confirm Do you really want to remove active logical volume test1? [Y/n]: y -- enter y to confirm 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} -- Remove physical volumes Labels on physical volume "/dev/sdb1" using wipedLabels on physical volume "/dev/sdb2" using wipedLabels on physical volume "/dev/sdb3" using wipedLabels on physical volume "/dev/sdb4" successfully wiped [root @ localhost ~] #
7. Common commands in lvm logical volumes
Pvcreate -- create a physical partition and build pvpvscan -- display a disk with pv pvdisplay -- list pvpvremove on the system -- delete the pv attribute vgcreate -- create the vg command vgscan -- find whether the vg in the system exists vgdisplay -- current System vg status vgextend -- add pvvgremove in vg -- delete a vgvgchange -- set whether vg starts vgreduce -- delete pvlvcreate in vg -- create lvlvscan -- Query System lvdisplay -- Display System lv status lvextend -- add capacity lvreduce in lv -- reduce capacity in lv lvremove -- delete lvlvresize -- Adjust the lv capacity
This article is from the blog "The days that have passed together" and will not be reproduced!