簡介: LVM ( Logical Volume Manager ) 邏輯捲管理 一、創建 LV 1、首先在你的虛擬機上添加一塊新的硬碟用來做實驗。 2、安裝 lvm : yum -y install lvm2 3、查看新添加的磁碟 ## 其中,/dev/sdb 就是我新添加的磁碟了 4、創建物理分 ...
簡介:
LVM ( Logical Volume Manager ) 邏輯捲管理
一、創建 LV
1、首先在你的虛擬機上添加一塊新的硬碟用來做實驗。
2、安裝 lvm : yum -y install lvm2
3、查看新添加的磁碟
shell > fdisk -l Disk /dev/sdb: 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: 0x00000000
## 其中,/dev/sdb 就是我新添加的磁碟了
4、創建物理分區
shell > fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x62beb6a4. 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): m # 幫助信息 Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition # 刪除一個分區 l list known partition types # 列印分區類型對應的 ID 號 m print this menu n add a new partition # 添加一個分區 o create a new empty DOS partition table p print the partition table # 列印分區表 q quit without saving changes # 不保存退出 s create a new empty Sun disklabel t change a partition's system id # 更改分區 ID u change display/entry units v verify the partition table w write table to disk and exit # 保存退出 x extra functionality (experts only)
## 執行的命令是 fdisk /dev/sdb .
Command (m for help): n # 執行添加分區指令 Command action e extended p primary partition (1-4) p # 添加一個主分區 Partition number (1-4): 1 # 分區號為 1 First cylinder (1-2610, default 1): # 分區起始位置(直接回車) Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1024M # 分區結束位置(+1024M 表示 1G 大小) Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (133-2610, default 133): Using default value 133 Last cylinder, +cylinders or +size{K,M,G} (133-2610, default 2610): +1024M Command (m for help): p # 執行列印分區表 Disk /dev/sdb: 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: 0x62beb6a4 Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 83 Linux /dev/sdb2 133 264 1060290 83 Linux
## 可以看到分了兩個區 /dev/sdb1 /dev/sdb2
Command (m for help): t # 執行更改分區 ID (83 表示Linux 普通分區、82 表示swap、8e 表示LVM) Partition number (1-4): 1 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): p Disk /dev/sdb: 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: 0x62beb6a4 Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 8e Linux LVM /dev/sdb2 133 264 1060290 8e Linux LVM
## 已經看到分區 ID 變為 8e ; 文件系統為 Linux LVM
Command (m for help): w # 執行保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. shell > fdisk -l /dev/sdb Disk /dev/sdb: 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: 0x62beb6a4 Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 8e Linux LVM /dev/sdb2 133 264 1060290 8e Linux LVM
## 可以看到磁碟 /dev/sdb 有兩個分區 /dev/sdb1 /dev/sdb2
5、創建 PV (物理捲)
shell > pvcreate /dev/sdb1 /dev/sdb2 Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created
## 使用 pvcreate 創建 PV 後面跟想要創建的分區
shell > pvscan PV /dev/sda3 VG vg_study lvm2 [18.80 GiB / 0 free] PV /dev/sdb1 lvm2 [1.01 GiB] PV /dev/sdb2 lvm2 [1.01 GiB] Total: 3 [20.82 GiB] / in use: 1 [18.80 GiB] / in no VG: 2 [2.02 GiB]
## 使用 pvscan 、pvdisplay 、pvs 都可以查看 PV
## PV /dev/sdb1 pv /dev/sdb2 為我新創建的 PV 大小為 1G(PV /dev/sda3 為之前創建的屬 VG vg_study )
6、創建 VG (捲組)
shell > vgcreate my_vg /dev/sdb1 /dev/sdb2 Volume group "my_vg" successfully created
## 使用 vgcreate 創建捲組,後面接 vg 名,接 pv
shell > vgdisplay --- Volume group --- VG Name my_vg System ID Format lvm2 Metadata Areas 2 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 2 Act PV 2 VG Size 2.02 GiB PE Size 4.00 MiB Total PE 516 Alloc PE / Size 0 / 0 Free PE / Size 516 / 2.02 GiB VG UUID ynCJG8-ZIvI-9gBF-dO5K-aQws-N9WW-r26hch
## 同樣可以使用 vgscan 、vgs 、vgdisplay 來查看 VG
## 可以看到 VG名為 my_vg 、VG 大小為 2G 、PE 大小 4M 等
7、創建 LV (邏輯捲)
shell > lvcreate -L 2G -n my_lv my_vg Logical volume "my_lv" created
## 使用 lvcreate 創建 LV ,-L 指定 LV大小,-n 指定 LV名稱,後面接 VG名
shell > lvdisplay --- Logical volume --- LV Path /dev/my_vg/my_lv LV Name my_lv VG Name my_vg LV UUID j9Bduh-3TB9-Owhz-X3PX-fVeQ-bvJe-OAFBVp LV Write Access read/write LV Creation host, time study.localhost.localdomain, 2014-11-26 11:14:28 +0800 LV Status available # open 0 LV Size 2.00 GiB Current LE 512 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1
## 同樣可以使用 lvscan 、lvs 、lvdisplay 來查看 LV
## 可以看到 LV路徑 /dev/my_vg/my_lv 、LV名 my_lv 、VG名 my_vg 、LV大小 2G 等
8、創建文件系統 (格式化)
shell > mkfs.ext4 /dev/my_vg/my_lv mke2fs 1.41.12 (17-May-2010) 文件系統標簽= 操作系統:Linux 塊大小=4096 (log=2) 分塊大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 131072 inodes, 524288 blocks 26214 blocks (5.00%) reserved for the super user 第一個數據塊=0 Maximum filesystem blocks=536870912 16 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 正在寫入inode表: 完成 Creating journal (16384 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 32 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
## 使用 mkfs.ext4 格式化為 ext4 文件系統
9、掛載使用
shell > mkdir /data shell > mount /dev/my_vg/my_lv /data/ shell > df -HT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_study-LogVol00 ext4 20G 1.4G 18G 8% / tmpfs tmpfs 521M 0 521M 0% /dev/shm /dev/sda1 ext4 204M 30M 164M 16% /boot /dev/mapper/my_vg-my_lv ext4 2.2G 71M 2.0G 4% /data
## 創建 /data 目錄,將 /dev/my_vg/my_lv 掛載到 /data
shell > umount /data/ shell > blkid /dev/my_vg/my_lv /dev/my_vg/my_lv: UUID="a8cb322e-1872-4ea8-bfba-f21b678bc849" TYPE="ext4" shell > echo "UUID="a8cb322e-1872-4ea8-bfba-f21b678bc849" /data ext4 defaults 0 0" >> /etc/fstab shell > mount -a shell > df -HT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_study-LogVol00 ext4 20G 1.5G 18G 8% / tmpfs tmpfs 521M 0 521M 0% /dev/shm /dev/sda1 ext4 204M 30M 164M 16% /boot /dev/mapper/my_vg-my_lv ext4 2.2G 71M 2.0G 4% /data
## 也可以使用 blkid 生成 UUID 然後寫入到 /etc/fstab 文件中,實現開機自動掛載
二、擴容 LV
1、首先創建物理分區(重點是這次創建後要格式化)
2、創建 PV
3、擴容 VG
shell > vgextend my_vg /dev/sdb3 Volume group "my_vg" successfully extended
## 使用 vgextend 指令擴展 VG ,後面接要擴展的 VG名 ,後面接將哪個 PV 擴展進去
shell > vgdisplay my_vg --- Volume group --- VG Name my_vg System ID Format lvm2 Metadata Areas 3 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 3 Act PV 3 VG Size 3.02 GiB PE Size 4.00 MiB Total PE 774 Alloc PE / Size 512 / 2.00 GiB Free PE / Size 262 / 1.02 GiB VG UUID ynCJG8-ZIvI-9gBF-dO5K-aQws-N9WW-r26hch
## 可以使用 vgdisplay my_vg 來指定查看哪個 VG
## 可以看到 VG 總大小為 3G 、已使用 2G 、空閑 1G(因為我們擴進去 1G 的 PV)
4、擴容 LV
shell > lvextend -L +1G /dev/my_vg/my_lv Size of logical volume my_vg/my_lv changed from 2.00 GiB (512 extents) to 3.00 GiB (768 extents). Logical volume my_lv successfully resized
## 使用 lvextend 擴容 LV ,-L 指定擴多大 ,後面接 LV 實際路徑 /dev/my_vg/my_lv
shell > lvdisplay /dev/my_vg/my_lv --- Logical volume --- LV Path /dev/my_vg/my_lv LV Name my_lv VG Name my_vg LV UUID j9Bduh-3TB9-Owhz-X3PX-fVeQ-bvJe-OAFBVp LV Write Access read/write LV Creation host, time study.localhost.localdomain, 2014-11-26 11:14:28 +0800 LV Status available # open 1 LV Size 3.00 GiB Current LE 768 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1
## 可以使用 lvdisplay /dev/my_vg/my_lv 來查看 LV 大小
## LV 大小變為了 3G (原 2G)
5、投入使用
shell > df -HT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_study-LogVol00 ext4 20G 1.4G 18G 8% / tmpfs tmpfs 521M 0 521M 0% /dev/shm /dev/sda1 ext4 204M 30M 164M 16% /boot /dev/mapper/my_vg-my_lv ext4 2.2G 71M 2.0G 4% /data
## 我們發現分區 /data 大小還是 2G
shell > resize2fs /dev/my_vg/my_lv resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/my_vg/my_lv is mounted on /data; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/my_vg/my_lv to 786432 (4k) blocks. The filesystem on /dev/my_vg/my_lv is now 786432 blocks long. shell > df -HT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_study-LogVol00 ext4 20G 1.4G 18G 8% / tmpfs tmpfs 521M 0 521M 0% /dev/shm /dev/sda1 ext4 204M 30M 164M 16% /boot /dev/mapper/my_vg-my_lv ext4 3.2G 71M 3.0G 3% /data
## 使用 resize2fs /dev/my_vg/my_lv 重新計算分區大小後 發現擴容到了 3G
三、縮減 LV
1、模擬數據寫入
shell > dd if=/dev/zero of=/data/block.db bs=1024M count=1 記錄了1+0 的讀入 記錄了1+0 的寫出 1073741824位元組(1.1 GB)已複製,81.6381 秒,13.2 MB/秒
## 我們使用 dd 命令寫了 1G 的數據到 /data
shell > cp /etc/passwd /data/ ## 複製這個文件的目的是驗證數據是否丟失 shell > df -HT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_study-LogVol00 ext4 20G 1.4G 18G 8% / tmpfs tmpfs 521M 0 521M 0% /dev/shm /dev/sda1 ext4 204M 30M 164M 16% /boot /dev/mapper/my_vg-my_lv ext4 3.2G 1.2G 1.9G 39% /data
## 現在這個剩餘空間 我們想把他撤出來 1G
2、首先檢查一下文件系統
shell > umount /data/ ## 一定要先 umount shell > e2fsck -f /dev/my_vg/my_lv e2fsck 1.41.12 (17-May-2010) 第一步: 檢查inode,塊,和大小 第二步: 檢查目錄結構 第3步: 檢查目錄連接性 Pass 4: Checking reference counts 第5步: 檢查簇概要信息 /dev/my_vg/my_lv: 13/196608 files (0.0% non-contiguous), 291646/786432 blocks
3、重新計算大小
shell > resize2fs /dev/my_vg/my_lv 2G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/my_vg/my_lv to 524288 (4k) blocks. The filesystem on /dev/my_vg/my_lv is now 524288 blocks long.
## 重新定義大小為 2G
4、縮小 LV
shell > lvreduce -L -1G /dev/my_vg/my_lv WARNING: Reducing active logical volume to 2.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce my_lv? [y/n]: y Size of logical volume my_vg/my_lv changed from 3.00 GiB (768 extents) to 2.00 GiB (512 extents). Logical volume my_lv successfully resized
## 使用 lvreduce 來縮小 LV ,-L -1G 縮小 1G
shell > mount -a shell > df -HT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_study-LogVol00 ext4 20G 1.4G 18G 8% / tmpfs tmpfs 521M 0 521M 0% /dev/shm /dev/sda1 ext4 204M 30M 164M 16% /boot /dev/mapper/my_vg-my_lv ext4 2.2G 1.2G 863M 58% /data shell > ls /data/ block.db lost+found passwd
## 重新掛載分區,發現數據沒有問題 (僅是測試,沒有發現問題)
5、VG 中移除 PV
shell > vgdisplay my_vg --- Volume group --- VG Name my_vg System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 6 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 3 Act PV 3 VG Size 3.02 GiB PE Size 4.00 MiB Total PE 774 Alloc PE / Size 637 / 2.49 GiB Free PE / Size 137 / 548.00 MiB VG UUID ynCJG8-ZIvI-9gBF-dO5K-aQws-N9WW-r26hch
## 首先我們 vgdisplay my_vg ,發現 VG 空閑 548M
shell > pvdisplay --- Physical volume --- PV Name /dev/sdb1 VG Name my_vg PV Size 1.01 GiB / not usable 3.41 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 258 Free PE 0 Allocated PE 258 PV UUID JB8X8v-jBPU-eOiN-aEOU-TOig-tv01-SuHytd --- Physical volume --- PV Name /dev/sdb2 VG Name my_vg PV Size 1.01 GiB / not usable 3.44 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 258 Free PE 0 Allocated PE 258 PV UUID YXBMJJ-RyG3-z1xa-tlw6-piaI-cTgj-SIBIlz --- Physical volume --- PV Name /dev/sdb3 VG Name my_vg PV Size 1.01 GiB / not usable 3.44 MiB Allocatable yes PE Size 4.00 MiB Total PE 258 Free PE 137 Allocated PE 121 PV UUID jSpEdJ-fxZh-LP27-rFIM-0gnO-mL2x-PpBJpK
## 其次使用 pvdisplay 查看 PV 的使用情況發現:
/dev/sdb1 PE 空閑 0
/dev/sdb2 PE 空閑 0
/dev/sdb3 PE 空閑 137 (上圖顯示每個 PE 為 4M ,那麼 /dev/sdb3 總空閑容量=137*4 = 548M)
## 正好等於 VG 的空閑大小,而 PV /dev/sdb3 大小為 1G ,也就是說有部分空間被占用,所以無法移除該 PV
shell > vgreduce my_vg /dev/sdb3 Physical volume "/dev/sdb3" still in use shell > umount /data/ shell > vgreduce my_vg /dev/sdb3 Physical volume "/dev/sdb3" still in use
## 提示正在被占用,無法移除( umount 分區也不行 )
shell > umount /data/ shell > e2fsck -f /dev/my_vg/my_lv e2fsck 1.41.12 (17-May-2010) 第一步: 檢查inode,塊,和大小 第二步: 檢查目錄結構 第3步: 檢查目錄連接性 Pass 4: Checking reference counts 第5步: 檢查簇概要信息 /dev/my_vg/my_lv: 14/163840 files (0.0% non-contiguous), 289591/652288 blocks shell > resize2fs /dev/my_vg/my_lv 2G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/my_vg/my_lv to 524288 (4k) blocks. The filesystem on /dev/my_vg/my_lv is now 524288 blocks long. shell > lvreduce -L -500M /dev/my_vg/my_lv WARNING: Reducing active logical volume to 2.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce my_lv? [y/n]: y Size of logical volume my_vg/my_lv changed from 2.49 GiB (637 extents) to 2.00 GiB (512 extents). Logical volume my_lv successfully resized
## 我們把 LV 縮小了 500M
shell > vgdisplay my_vg --- Volume group --- VG Name my_vg System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 9 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 0 Max PV 0 Cur PV 3 Act PV 3 VG Size 3.02 GiB PE Size 4.00 MiB Total PE 774 Alloc PE / Size 512 / 2.00 GiB Free PE / Size 262 / 1.02 GiB VG UUID ynCJG8-ZIvI-9gBF-dO5K-aQws-N9WW-r26hch
## 現在再看 VG 大小 空閑 1G
shell > pvdisplay --- Physical volume --- PV Name /dev/sdb3 VG Name my_vg PV Size 1.01 GiB / not usable 3.44 MiB Allocatable yes PE Size 4.00 MiB Total PE 258 Free PE 258 Allocated PE 0 PV UUID jSpEdJ-fxZh-LP27-rFIM-0gnO-mL2x-PpBJpK
## pvdisplay 也發現 /dev/sdb3 正好全部空閑
shell > vgreduce my_vg /dev/sdb3 Removed "/dev/sdb3" from volume group "my_vg"
## 已經成功將 PV /dev/sdb3 從 VG my_vg 中移除
shell > vgs VG #PV #LV #SN Attr VSize VFree my_vg 2 1 0 wz--n- 2.02g 16.00m
## 使用 vgs 可以看到 vg my_vg 中包含 PV 數為 2
6、刪除 PV
shell > pvremove /dev/sdb3 Labels on physical volume "/dev/sdb3" successfully wiped
## 使用 pvremove 刪除 PV /dev/sdb3
shell > pvs PV VG Fmt Attr PSize PFree /dev/sdb1 my_vg lvm2 a-- 1.01g 0 /dev/sdb2 my_vg lvm2 a-- 1.01g 16.00m
## 使用 pvs 看到只有兩個 PV 了
## 然後 fdisk /dev/sdb 將 sdb3 的 ID 改為 83 ,保存
mkfs.ext4 /dev/sdb3 ,重新掛載到目錄,至此 /dev/sdb3 成為普通分區
四、刪除整個 lv vg pv
shell > umount /data/ shell > lvremove /dev/my_vg/my_lv Do you really want to remove active logical volume my_lv? [y/n]: y Logical volume "my_lv" successfully removed shell > vgremove my_vg Volume group "my_vg" successfully removed shell > pvremove /dev/sdb1 Labels on physical volume "/dev/sdb1" successfully wiped shell > pvremove /dev/sdb2 Labels on physical volume "/dev/sdb2" successfully wiped
## 刪除比創建容易多了 ...