LVM 管理

創(chuàng)建LVM

  • 劃分分區(qū),并指定適合的LVM的ID
    [root@centos6 rf]# fdisk/dev/sdc使用fdisk劃分一個(gè)分區(qū);也可直接拿塊新硬盤(pán)直接創(chuàng)建LVM邏輯卷
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x3ca76d2c.
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): p 查看/dev/sdc硬盤(pán)的分區(qū)情況;由于是新硬盤(pán)所以沒(méi)有分區(qū)表

Disk /dev/sdc: 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: 0x3ca76d2c

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n 表示創(chuàng)建一個(gè)分區(qū)
Command action
   e   extended
   p   primary partition (1-4)
p   表示主分區(qū)  MBR分區(qū)表最多只允許4個(gè)主分區(qū)或3個(gè)主分區(qū)加一個(gè)擴(kuò)展分區(qū),可以擴(kuò)展分區(qū)創(chuàng)建多個(gè)邏輯分區(qū)(e|p) e表示擴(kuò)展分區(qū);p表示主分區(qū)
Partition number (1-4): 1   分區(qū)號(hào)
First cylinder (1-2610, default 1):   指定開(kāi)始的柱面這里如果不指定表示選擇的是默認(rèn)柱面,擴(kuò)展分區(qū)不要讓系統(tǒng)默認(rèn)否則會(huì)出錯(cuò)
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G  創(chuàng)建的大小可以指定柱面 也可以指定空間的大小

Command (m for help): t 調(diào)整分區(qū)的ID
Selected partition 1
Hex code (type L to list codes): l 查看系統(tǒng)默認(rèn)支持的所有分區(qū)ID

 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 這里選擇8e 8e表示LVM
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p 查看分區(qū)情況

Disk /dev/sdc: 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: 0x3ca76d2c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1306    10490413+  8e  Linux LVM
Command (m for help): w保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

  • 注意 這里此磁盤(pán)分區(qū)已同步到內(nèi)存中centos6新增磁盤(pán)同步分區(qū)表的
  • partx -a /dev/DEVICE 或者 kpartx -a /dev/DEVICE -f: force
  • centos6刪除分區(qū)表同步的方法
  • partx -d --nr M-N /dev/DEVICE
  • CentOS 5 ,7: 增加分區(qū)或刪除分區(qū) 使用partprobe

分區(qū)已劃分創(chuàng)建LVM

  • 創(chuàng)建物理卷
  • [root@centos6 rf]# pvcreate /dev/sdc1
  • Physical volume "/dev/sdc1" successfully created
  • [root@centos6 rf]# pvdisplay 查看系統(tǒng)上所有的物理卷的詳細(xì)信息
"/dev/sdc1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name                PV Size               10.00 GiB
  Allocatable           NO
   PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               n4NV1m-sr2p-A5K0-ckl4-681D-ah7g-HtYjmo
  • [root@centos6 rf]# pvs 顯示系統(tǒng)上物理卷的信息
 PV         VG   Fmt  Attr PSize  PFree 
 /dev/sdc1       lvm2 ---- 10.00g 10.00g
  • [root@centos6 rf]# vgcreate -s 10M vg0 /dev/sdc1 創(chuàng)建卷組 -s 指定PE大小 可以省略,省略表示選擇默認(rèn)值 后面跟的是卷組的名字 指定從哪個(gè)物理卷劃分
  • Volume group "vg0" successfully created
  • [root@centos6 rf]# vgdisplay 查看卷組相關(guān)的信息
--- Volume group ---
 VG Name               vg0   卷組名稱(chēng)
 System ID             
 Format                lvm2
 Metadata Areas        1
 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                1
 Act PV                1
 VG Size               10.00 GiB 卷組總大小
 PE Size               10.00 MiB PE大小
 Total PE              1024 PE個(gè)數(shù)
 Alloc PE / Size       0 / 0   
 Free  PE / Size       1024 / 10.00 GiB
 VG UUID               ZujzGf-nNnJ-LfZb-ge6F-fiAP-MtHW-EepPhI
  • [root@centos6 rf]# vgs 查看系統(tǒng)中的卷組
VG   #PV #LV #SN Attr   VSize  VFree 
vg0    1   0   0 wz--n- 10.00g 10.00g

[root@centos6 rf]# lvcreate -L 10G -n cx vg0 創(chuàng)建邏輯卷 -L 表示指定空間大小 -n 表示指定邏輯卷的名字 后面跟卷組名字表示從哪個(gè)卷組中劃分邏輯分區(qū)

  Logical volume "cx" created.

[root@centos6 rf]# mkfs.ext4 /dev/vg0/cx 創(chuàng)建文件系統(tǒng)

mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 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

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
  • [root@centos6 rf]# mkdir /cx 創(chuàng)建一個(gè)目錄做掛載點(diǎn)
  • [root@centos6 rf]# vim /etc/fstab 編輯 /etc/fstab添加“UUID=d7ab49ba-156c-4ca4-8e57-d2c6e8dd3076 /cx ext4 defaults 1 0
  • [root@centos6 ~]# blkid /dev/vg0/cx 查看UUID
/dev/vg0/cx: UUID="d7ab49ba-156c-4ca4-8e57-d2c6e8dd3076" TYPE="ext4"
  • [root@centos6 rf]# mount -a 掛載/etc/fstab 文件中未掛載的設(shè)備
    [root@centos6 rf]# mount 查看已掛載的設(shè)備
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sda6 on /home type ext4 (rw,usrquota)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/vg0-cx on /cx type ext4 (rw)

擴(kuò)展LVM分區(qū)

  • 1如果卷組里還有多余的空間,可以直接擴(kuò)展邏輯卷;如果沒(méi)有多余的空間就需要先擴(kuò)展卷組
  • 我這里先用一塊新硬盤(pán)擴(kuò)展物理卷再擴(kuò)展卷組最后擴(kuò)展邏輯分區(qū)
  • [root@centos6 ~]# pvcreate /dev/sdd # 擴(kuò)展物理卷 ;如果有多余的物理卷,可以直接擴(kuò)展卷組
  Physical volume "/dev/sdd" successfully created

[root@centos6 ~]# pvs 查看物理卷PV的信息

  PV         VG   Fmt  Attr PSize  PFree 
  /dev/sdc1  vg0  lvm2 a--u 10.00g     0 
  /dev/sdd        lvm2 ---- 60.00g 60.00g

[root@centos6 ~]# vgextend vg0 /dev/sdd 使用vgextend 擴(kuò)展卷組(vg) 后面跟卷組名 指定把那個(gè)物理卷擴(kuò)展到卷組

  Volume group "vg0" successfully extended
  • [root@centos6 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg0  卷組名字
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               69.99 GiB 卷組總大小
  PE Size               10.00 MiB PE的大小
  Total PE              7167  PE個(gè)數(shù)
  Alloc PE / Size       1024 / 10.00 GiB  已分配的給邏輯卷(lv)使用的空間
  Free  PE / Size       6143 / 59.99 GiB  未分配空間
  VG UUID               ZujzGf-nNnJ-LfZb-ge6F-fiAP-MtHW-EepPhI

[root@centos6 ~]# lvextend -r -l +1000 /dev/vg0/cx 擴(kuò)展邏輯卷并同步文件系統(tǒng)-r 表示同步文件系統(tǒng)

  Size of logical volume vg0/cx changed from 10.00 GiB (1024 extents) to 19.77 GiB (2024 extents).
  Logical volume cx successfully resized.
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg0-cx is mounted on /cx; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/vg0-cx to 5181440 (4k) blocks.
The filesystem on /dev/mapper/vg0-cx is now 5181440 blocks long.
注意擴(kuò)展邏輯卷無(wú)需取消掛載;建議提前備份數(shù)據(jù)

縮減邏輯卷

  • 先取消掛載
  • [root@centos6 ~]# umount /dev/vg0/cx
  • 檢查文件系統(tǒng)完整性,這一步必須否則系統(tǒng)不讓縮
  • [root@centos6 ~]# e2fsck -f /dev/vg0/cx
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg0/cx: 11/1302528 files (0.0% non-contiguous), 121551/5181440 blocks
  • 縮減文件系統(tǒng)到15G
    [root@centos6 ~]# resize2fs /dev/vg0/cx 15G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg0/cx to 3932160 (4k) blocks.
The filesystem on /dev/vg0/cx is now 3932160 blocks long.
  • 縮減邏輯卷到15G,注意這里縮減的大小與文件系統(tǒng)大小一致
  • [root@centos6 ~]# lvreduce -L 15G /dev/vg0/cx
 WARNING: Reducing active logical volume to 15.00 GiB.
 THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/cx? [y/n]: y  提醒用戶(hù)縮減空間可能會(huì)損壞數(shù)據(jù)
 Size of logical volume vg0/cx changed from 19.77 GiB (2024 extents) to 15.00 GiB (1536 extents).
 Logical volume cx successfully resized.

縮減邏輯卷成

創(chuàng)建邏輯卷快照

  • [root@centos6 ~]# mount -a 掛載文件系統(tǒng)
  • [root@centos6 ~]# mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sda6 on /home type ext4 (rw,usrquota)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/vg0-cx on /cx type ext4 (rw)

拷貝數(shù)據(jù)

  • [root@centos6 ~]# cp /etc/passwd /cx/
  • [root@centos6 ~]# ls /cx/
lost+found  passwd
  • 創(chuàng)建快照
  • [root@centos6 ~]# lvcreate -L 1G -s -n cx-date -p r /dev/vg0/cx
  Rounding up size to full physical extent 1.01 GiB
  Logical volume "cx-date" created.
  • [root@centos6 ~]# mkdir ll
  • [root@centos6 ~]# mount /dev/vg0/cx-date ll
mount: block device /dev/mapper/vg0-cx--date is write-protected, mounting read-only  掛載快照
  • [root@centos6 ~]# ls l 查看快照
lost+found  passwd
  • [root@centos6 ~]# rm -rf /cx/passwd 刪除源文件的部分?jǐn)?shù)據(jù)
    取消邏輯卷與快照的掛載
  • [root@centos6 ~]# umount /cx/
  • [root@centos6 ~]# umount ll/
  • 恢復(fù)快照 注意恢復(fù)快照會(huì)被刪除
  • [root@centos6 ~]# lvconvert --merge /dev/vg0/cx-date
    Merging of volume cx-date started.
    cx: Merged: 100.0%
    Merge of snapshot into logical volume cx has finished.
    Logical volume "cx-date" successfully removed
    
  • 快照被刪除
  • [root@centos6 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg0/cx
  LV Name                cx
  VG Name                vg0
  LV UUID                YqujVe-SSvL-BDFZ-TuPu-YMTg-lXTe-DnlubY
  LV Write Access        read/write
  LV Creation host, time centos6.chenxi.com, 2017-06-24 08:52:01 +0800
  LV Status              available
  # open                 0
  LV Size                15.00 GiB
  Current LE             1536
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

查看是否成功恢復(fù)

  • [root@centos6 ~]# mount -a
  • [root@centos6 ~]# cat /cx/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin......

遷移LVM到其他主機(jī),先取消掛載

  • 遷移前改個(gè)名字 如果目標(biāo)主機(jī)沒(méi)有重復(fù)名字的卷組就不用更改
  • vgrename 原名 新名字
  • lvrename 原名 新名字 改邏輯卷的名字
  • umount /cx/取消掛載
  • pvmove /dev/sdc1 遷移PE空間,遷移前應(yīng)先查看卷組PE空間是否夠sdc1 PE空間使用,如果不夠應(yīng)先擴(kuò)展卷組空間
  • vgchange -an vg0 禁用卷組
  • vgexport vg0 導(dǎo)出卷組
  • 拔出對(duì)應(yīng)的硬盤(pán)拔出
    在目標(biāo)計(jì)算機(jī)上
  • 在新系統(tǒng)中安裝舊硬盤(pán),并vgimport vg0.
  • vgchange –ay vg0 啟用卷組
  • mount 掛載
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容