linux系統中對邏輯卷(lvm)的實現

來源:互聯網
上載者:User

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的容量進行調整

 

本文出自 “一起走過的日子” 部落格,謝絕轉載!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.