Linux - 掛載磁碟 + 通過LVM動態實現磁碟的動態擴容
文章目錄
- 1 LVM是什麼
- 2 普通的掛載磁碟方法
- 3 LVM方式掛載磁碟 - 推薦
- 3.1 檢視磁碟容量資訊
- 3.2 檢視磁碟扇區資訊
- 3.3 建立分割槽
- 3.4 建立物理卷
- 3.5 擴展卷組
- 3.6 擴充套件邏輯卷
- 3.7 檢視磁碟卷組資訊
- 3.8 調整檔案系統的大小
- 3.9 附錄 - 建立卷組
- 參考資料
- 版權宣告
1 LVM是什麼
1.1 概念解釋
LVM(Logical Volume Manager)邏輯卷管理, 是一種將一至多個硬碟的分割槽在邏輯上進行組合, 當成一個大硬碟來使用. 當硬碟空間不足時, 可以動態地新增其它硬碟的分割槽到已有的卷組中 —— 磁碟空間的動態管理.
1.2 為什麼用LVM
LVM通常用於裝備大量磁碟的系統, 比如伺服器中的磁碟陣列.
但LVM同樣適用於僅有一、兩塊硬碟的小系統.
1.2.1 不使用LVM時的擴容思路
傳統的檔案系統是基於分割槽的, 一個檔案系統對應一個分割槽, 這種方式比較直觀, 但不易改變:
-
不同的分割槽相互獨立, 單獨的檔案不能跨分割槽儲存, 容易出現硬碟的利用率不均衡;
-
當一個檔案系統/分割槽裝滿時, 是不能對其進行擴容的, 只能採用重新分割槽/建立檔案系統, 重新分割槽會丟失資料, 就要: ① 做資料的遷移和備份, ② 或者把分割槽中的資料移到另一個更大的分割槽中, ③ 或者採用符號連線的方式使用其它分割槽的空間 —— 都非常麻煩;
-
如果要把硬碟上的多個分割槽合併在一起使用, 只能採用重新分割槽的方式, —— 需要做好資料的備份與恢復.
1.2.2 使用LVM時的擴容思路
使用LVM時技術時, 情況有所不同:
-
硬碟的多個分割槽由LVM統一管理為卷組, 可以很輕鬆地加入或移走某個分割槽 —— 也就是擴大或減小卷組的可用容量, 充分利用硬碟空間;
-
檔案系統建立在邏輯捲上, 而邏輯卷可以根據需要改變大小(在卷組容量範圍內)以滿足要求;
-
檔案系統建立在LVM上, 可以跨分割槽儲存訪問, 更加方便;
強烈建議對擁有多個磁碟的系統, 使用LVM管理磁碟.
1.3 名詞解釋
PV(Physical Volume): 物理卷, 處於LVM最底層, 可以是物理硬碟或者分割槽;
PP(Physical Extend): 物理區域, PV中可以用於分配的最小儲存單元, 可以在建立PV的時候指定, 如1M, 2M, 4M, 8M……組成同一VG中所有PV的PE大小應該相同;
VG(Volume Group): 卷組, 建立在PV之上, 可以含有一個到多個PV;
LV(Logical Volume): 邏輯卷, 建立在VG之上, 相當於原來分割槽的概念, 不過大小可以動態改變.
2 普通的掛載磁碟方法
2.1 建立分割槽的主要操作
- 檢視分割槽情況 - fdisk -l
[[email protected] ~]# fdisk -l
Disk /dev/sda: 299.0 GB, 298999349248 bytes # 磁碟/dev/sda
255 heads, 63 sectors/track, 36351 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: 0x4d69fe0e
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux # 分為2個區, sda1
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 36352 291785728 8e Linux LVM # sda2
# 磁碟/dev/sdb沒有分割槽
Disk /dev/sdb: 4000.0 GB, 3999999721472 bytes
255 heads, 63 sectors/track, 486305 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
......
- 檢視已有磁碟 - lsblk
[[email protected] ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 278.5G 0 disk
├─sda1 8:1 0 200M 0 part /boot
└─sda2 8:2 0 278.3G 0 part
└─VolGroup-LogVol (dm-0) 253:0 0 1.9T 0 lvm / # LVM型別的分割槽
sdb 8:32 0 3.7T 0 disk # 還沒有分割槽的新磁碟
- 對新磁碟進行分割槽 - fdisk /dev/sdb
[[email protected] ~]# 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 0xf91f8c4c.
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: The size of this disk is 4.0 TB (4000225165312 bytes).
DOS partition table format can not be used on drives for volumes
larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID
partition table format (GPT).
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): n # n 表示新建分割槽
Command action
e extended
p primary partition (1-4)
p # p 表示分割槽型別為主分割槽, 主分割槽只有1-4種選擇
Partition number (1-4): 1 # 主分割槽的編號
First cylinder (1-486333, default 1): # 開始扇區號, 直接回車, 使用預設值1
Using default value 1
# 結束扇區號, 使用預設值 --- 這裡只加載了新磁碟的一半(2T), 所以還需要再次建立分割槽/dev/sdb2使用剩下的一半.
Last cylinder, +cylinders or +size{K,M,G} (1-267349, default 267349):
Using default value 267349
Command (m for help): w # 將上述設定寫入分割槽表並退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
- 再次檢視分割槽情況 - fdisk -l
多出來一個/dev/sdb1的區, 這個1就是之前主分割槽之後指定的分割槽編號.
[[email protected] ~]# fdisk -l
Disk /dev/sda: 299.0 GB, 298999349248 bytes
255 heads, 63 sectors/track, 36351 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: 0x4d69fe0e
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 36352 291785728 8e Linux LVM
# /dev/sdb磁碟:
Disk /dev/sdb: 4000.0 GB, 3999999721472 bytes
255 heads, 63 sectors/track, 486305 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: 0x8f3043b5
# 多出來的分割槽/dev/sdb1
Device Boot Start End Blocks Id System
/dev/sdb1 1 267349 2147480811 83 Linux
......
- 檢視當前分割槽表中的分割槽資訊 - cat /proc/partitions
[[email protected] ~]# cat /proc/partitions
major minor #blocks name
8 0 291991552 sda
8 1 204800 sda1
8 2 291785728 sda2
8 32 3906249728 sdb # 新增的新磁碟
8 33 2147480811 sdb1 # 建立的新分割槽
253 0 2046660608 dm-0
如果建立完之後,cat /proc/partitions
檢視不到對應的分割槽, 使用 parprobe
重新整理命令即可:
[[email protected] ~]# partprobe /dev/sdc
2.2 格式化新分割槽
- 格式化新分割槽 - mkfs -t
這裡建議將新分割槽格式化為ext4
檔案型別, 還有ext2
, ext3
等檔案型別, 區別請參考部落格 ext2、ext3與ext4的區別 .
[[email protected] ~]# mkfs -t ext4 /dev/sdb1
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
134217728 inodes, 536870202 blocks
26843510 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
16384 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, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848, 512000000
Writing inode tables: 8874/16384
- 等待一小會後, 將出現下述提示, 說明格式化完成:
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
2.3 掛載新分割槽
- 建立目錄, 並將
/dev/sdb1
掛在到該目錄下:
[[email protected] /]# mkdir data && cd /data
[[email protected] data]# mount /dev/sdc1 /data1
- 檢視掛載是否成功:
[[email protected] data]# df -l
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup-LogVol 286901696 18601728 253726196 7% /
tmpfs 66020980 0 66020980 0% /dev/shm
/dev/sda1 495844 33476 436768 8% /boot
# 掛載成功:
/dev/sdb1 2113784984 202776 2006208168 1% /data
2.4 設定開機自動掛載
編輯檔案 /etc/fstab
:
[[email protected] data]# vim /etc/fstab
# 檔案內容如下:
# /etc/fstab
# Created by anaconda on Wed Sep 12 10:41:40 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-LogVol / ext4 defaults 1 1
/dev/sdb1 /data ext4 defaults 1 1
UUID=22b1d425-d050-43c3-a735-06d48bbb9051 /boot ext4 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
3 LVM方式掛載磁碟 - 推薦
3.1 檢視磁碟容量資訊
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VG-LogVol 1.9T 1.8T 61G 97% / # LVM卷組-邏輯卷
tmpfs 63G 0 63G 0% /dev/shm
/dev/sda1 485M 40M 421M 9% /boot
3.2 檢視磁碟扇區資訊
[[email protected] ~]# fdisk -l
Disk /dev/sda: 299.0 GB, 298999349248 bytes # 磁碟/dev/sda
255 heads, 63 sectors/track, 36351 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: 0x4d69fe0e
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux # 分為2個區, sda1
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 36352 291785728 8e Linux LVM # LVM型別的sda2
# 新新增的磁碟/dev/sdb, 沒有分割槽
Disk /dev/sdb: 4000.0 GB, 3999999721472 bytes
255 heads, 63 sectors/track, 486305 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
# LVM格式的卷組資訊:
Disk /dev/mapper/VolGroup-LogVol: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 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
3.3 建立分割槽
[[email protected] ~]# 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 0x5b3d66ba.
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: The size of this disk is 4.0 TB (3999999721472 bytes).
DOS partition table format can not be used on drives for volumes
larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID
partition table format (GPT).
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): n # 新增分割槽
Command action
e extended
p primary partition (1-4)
p # 新增主分割槽
Partition number (1-4): 1 # 1號主分割槽, 即/dev/sdb1
First cylinder (1-486305, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-267349, default 267349): 486305
Value out of range.
Last cylinder, +cylinders or +size{K,M,G} (1-267349, default 267349):
Using default value 267349
Command (m for help): n # 繼續新增分割槽
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2 # 2號主分割槽, 即/dev/sdc2
First cylinder (267350-486305, default 267350):
Using default value 267350
Last cylinder, +cylinders or +size{K,M,G} (267350-486305, default 486305):
Using default value 486305
Command (m for help): p # 列印分割槽資訊:
Disk /dev/sdb: 4000.0 GB, 3999999721472 bytes
255 heads, 63 sectors/track, 486305 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: 0x5b3d66ba
Device Boot Start End Blocks Id System
/dev/sdb1 1 267349 2147480811 83 Linux
/dev/sdb2 267350 486305 1758764070 83 Linux
Command (m for help): t # 轉換型別
Partition number (1-4): 1
Partition number (1-4): 1 # 修改/dev/sdb1為Linux LVM型別:
Hex code (type L to list codes): L # 檢視可用型別:
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, 即Linux LVM型別
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-4): 2 # 修改/dev/sdc2為Linux LVM型別
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/sdc: 4000.0 GB, 3999999721472 bytes
255 heads, 63 sectors/track, 486305 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: 0x5b3d66ba
Device Boot Start End Blocks Id System
/dev/sdb1 1 267349 2147480811 8e Linux LVM # Id已改變
/dev/sdb2 267350 486305 1758764070 8e Linux LVM
Command (m for help): w # 儲存並退出
The partition table has been altered! # 修改成功
Calling ioctl() to re-read partition table.
Syncing disks.
3.4 建立物理卷
[[email protected] ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
[[email protected] ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created
3.5 擴展卷組
# 檢視已有卷組, 發現該卷組就是需要擴容的卷組, 就不必再次建立卷組, 而是直接擴展卷組即可:
[[email protected] ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 2 2 0 wz--n- 1.91t 0
# 擴展卷組:
[[email protected] ~]# vgextend VolGroup /dev/sdb1
Volume group "VolGroup" successfully extended
[[email protected] ~]# vgextend VolGroup /dev/sdb2
Volume group "VolGroup" successfully extended
-
說明: 如果出現下述無法掛載物理磁碟到卷組中的資訊, 說明該物理磁碟已經掛載了, 需要先解除安裝, 然後再執行建立分割槽+卷組的操作:
[[email protected] /]# vgextend VolGroup /dev/sdb1 No physical volume label read from /dev/sdb1 Physical volume /dev/sdb1 not found Can't open /dev/sdb1 exclusively. Mounted filesystem? Unable to add physical volume '/dev/sdb1' to volume group 'VolGroup'.
3.6 擴充套件邏輯卷
# 擴充套件邏輯卷, 即擴容:
[[email protected] ~]# lvextend -l +100%FREE /dev/mapper/VolGroup-LogVol
Extending logical volume lv_root to 5.54 TiB
Logical volume lv_root successfully resized
# 上述命令是將所有的空閒空間都擴容到邏輯卷中, 也可指定擴容的大小:
lvextend -l +100G /dev/mapper/VolGroup-LogVol
3.7 檢視磁碟卷組資訊
[[email protected] ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 278.5G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 278G 0 part
├─VolGroup-LogVol (dm-0) 253:0 0 5.6T 0 lvm /
sdb 8:16 0 1.6T 0 disk
└─sdb1 8:17 0 1.6T 0 part
└─VolGroup-LogVol (dm-0) 253:0 0 5.6T 0 lvm /
sdc 8:32 0 3.7T 0 disk
├─sdc1 8:33 0 2T 0 part
│ └─VolGroup-LogVol (dm-0) 253:0 0 5.6T 0 lvm /
└─sdc2 8:34 0 1.7T 0 part
└─VolGroup-LogVol (dm-0) 253:0 0 5.6T 0 lvm /
3.8 調整檔案系統的大小
# CentOS 7重新讀取磁碟大小:
[[email protected] ~]# xfs_growfs /dev/mapper/VolGroup-LogVol
xfs_growfs: /dev/mapper/VolGroup-LogVol is not a mounted XFS filesystem
# CentOS 6.5重新讀取磁碟大小:
# ext4格式, resize2fs會遍歷整個磁碟, 速度比較慢, 但是不影響讀寫資料, 可以令其在後臺執行.
[[email protected] ~]# resize2fs /dev/mapper/VolGroup-LogVol
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/VolGroup-LogVol is mounted on /; on-line resizing required
old desc_blocks = 122, new_desc_blocks = 355
Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 1487098880 (4k) blocks.
# 等了差不多20分鐘, 出來了下面這貨:
The filesystem on /dev/mapper/VolGroup-LogVol is now 1487098880 blocks long.
# 趕緊看下擴容成果吧:
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol 5.5T 1.8T 3.5T 34% / # 擴容成功
tmpfs 63G 0 63G 0% /dev/shm
/dev/sda1 485M 40M 421M 9% /boot
3.9 附錄 - 建立卷組
對應 [3.5] 節的擴展卷組, 如果卷組不存在, 則需要建立之. 下述VolGroup是卷組名稱.
vgcreate VolGroup /dev/sdb1
# 建立邏輯卷, 名稱為: mylv. (作業系統中將產生: /dev/VolGroup/mylv目錄)
# 將當前卷組中的100G空間分配到邏輯卷中
lvcreate -L 100G VolGroup -n mylv
# 或將當前卷組中的所有空閒空間全都分配到邏輯卷中:
lvcreate -l +100%FREE VolGroup -n mylv
# 格式化邏輯卷組:
mkfs -t ext4 /dev/VolGroup/mylv
# 掛載卷組到指定目錄下, 如果是掛載到根目錄, 則無需向/etc/fstab檔案中新增啟動項.
mount -t ext4 /dev/VolGroup/mylv /data
參考資料
版權宣告
作者: ma_shoufeng(馬瘦風)
出處: CSDN 馬瘦風的部落格
您的支援是對博主的極大鼓勵, 感謝您的閱讀.
本文版權歸博主所有, 歡迎轉載, 但未經博主同意必須保留此段宣告, 且在文章頁面明顯位置給出原文連結, 否則博主保留追究相關人員法律責任的權利.