本節內容
磁盤管理
LVM
一、磁盤管理
1、硬盤接口
種類及其應用:
IDE接口硬盤多用於家用產品,部分應用於服務器
SATA
SCSI接口硬盤主要應用於服務器
SAS只在高端服務器上,價格昂貴
2、硬盤種類
a、SATA硬盤:SATA接口,又叫串口硬盤。PC機主流發展方向。有較強的糾錯能力,錯誤一經發現能自動糾正,這樣就大大的提高了數據傳輸的安全性。(0.5V電壓,轉速可達7200轉/分)
b、SCSI硬盤:SCSI接口。優點:SCSI接口應用范圍廣、多任務、帶寬大、CPU占用率低,以及熱插拔等。它由於性能好、穩定性高,因此在服務器上得到廣泛應用。缺點:由於SCSI硬盤價格非常昂貴,所以一般的PC是不會使用SCSI硬盤。 一般轉速可達10000轉/分。
c、SAS硬盤:SAS(Serial Attached SCSI)即串行連接SCSI,是新一代的SCSI技術,和現在流行的Serial ATA(SATA)硬盤相同,都是采用串行技術以獲得更高的傳輸速度,並通過縮短連結線改善內部空間等。改善存儲系統的效能、可用性和擴充性。 一般轉速可達15000轉/分,甚至更高。
3、分區符
在Linux系統中一切皆文件,所以硬盤也是文件,分區也是文件
設備文件都存放在/dev目錄中,/dev又叫做設備文件目錄
MBR(Master Boot Record)硬盤的主引導記錄
硬盤的0柱面、0磁頭、1扇區稱為主引導扇區(也叫主引導記錄MBR)。它由三個部分組成,主引導程序、硬盤分區表DPT(Disk Partition table)和分區有效標志(55AA)。在總共512字節的主引導扇區里主引導程序(boot loader)占446個字節,第二部分是Partition table區(分區表),即DPT,占64個字節,硬盤中分區有多少以及每一分區的大小都記在其中。第三部分是magic number,占2個字節,固定為55AA。
512字節
三部分組成
主引導程序:bootloader 446
分區表:64字節
魔數:55AA
分區作用:
主分區:主要是用來啟動操作系統的,它主要放的是操作系統的啟動或引導程序,/boot分區最好放在主分區上
擴展分區:不能使用的,它只是做為邏輯分區的容器存在的,先創建一個擴展分區,在拓展分區之上創建邏輯分區;我們真正存放數據的是主分區和邏輯分區,大量數據都放在邏輯分區中。
注意:使用分區工具fdisk對磁盤進行操作,分區,格式化(重點)
主分區:最多是4個。編號默認是從1開始(1-4)。
擴展分區:最多是一個,可以是0個。(擴展分區也算是主分區一種,也就是說,當存在擴展分區時,主分區還可以划分3個)
邏輯分區:可以很多個,邏輯分區是建立在擴展分區之上的(編號從5開始)
命名方式: /dev/sd[a-z]n
其中:(s代表scsi或者sata;h為ide接口;)(d代表硬盤;r代表光驅;)a-z表示設備的序號,如sda表示第一塊scsi硬盤,sdb就是第二塊......,n表示每塊磁盤上划分的磁盤分區編號。
4、文件系統
用戶在硬件存儲設備中執行的文件建立、寫入、讀取、修改、轉存與控制等操作都是依靠文件系統來完成的。文件系統的作用是合理規划硬盤,以保證用戶正常的使用需求。Linux系統支持數十種的文件系統,而最常見的文件系統如下所示。
centos6系統默認的是ext4--ext2,Linux second extended file system,ext2fx
centos7系統默認的是xfs
Ext4:Ext3的改進版本,作為RHEL 6系統中的默認文件管理系統,它支持的存儲容量高達1EB(1EB=1,073,741,824GB),且能夠有無限多的子目錄。另外,Ext4文件系統能夠批量分配block塊,從而極大地提高了讀寫效率。
XFS:是一種高性能的日志文件系統,而且是RHEL 7中默認的文件管理系統,它的優勢在發生意外宕機后尤其明顯,即可以快速地恢復可能被破壞的文件,而且強大的日志功能只用花費極低的計算和存儲性能。並且它最大可支持的存儲容量為18EB,這幾乎滿足了所有需求。
5、fdisk分區管理工具(manipulate disk partition table)
m 查看全部可用的參數
n 添加新的分區
d 刪除某個分區信息
l 列出所有可用的分區類型(查看)
t 改變某個分區的類型
p 打印分區信息(查看分區表信息)
w 保存分區信息並退出
q 不保存直接退出
fdisk:磁盤分區,是Linux發行版本中最常用的分區工具
用法:fdisk [選項] device
例子1:創建主分區
第一步:創建主分區
[root@renyz08 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
更改將停留在內存中,直到您決定將更改寫入磁盤。
使用寫入命令前請三思。
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x1eff4d75.
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
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
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
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): n #創建新的分區
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): #創建主分區,回車默認是主分區
Using default response p
Partition number (1-4, default 1): #回車默認編號為1
First sector (2048-41943039, default 2048): #默認分區起始值
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +30G #分區大小不能超過磁盤總大小(錯誤嘗試)(G單位要大寫)
Value out of range.
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1G
#輸入想要分區的大小
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): p #打印分區信息
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x1eff4d75
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
Command (m for help): w #保存當前分區信息
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
第二步:查看分區
[root@renyz08 ~]# ls /dev/sdb*
/dev/sdb /dev/sdb1
第三步:進行格式化,就是安裝文件系統的過程mkfs(meke xfs)
[root@renyz08 ~]# mkfs.xfs /dev/sdb1
或者[root@renyz08 ~]# mkfs -t xfs /dev/sdb1
第四步:掛載使用
[root@renyz08 ~]# df -h #查看原先的掛載信息
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root 17G 1.2G 16G 7% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.6M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
[root@renyz08 ~]# mkdir /ren1 #創建掛載點
[root@renyz08 ~]# mount /dev/sdb1 /ren1
#把磁盤分區掛載到linux系統一個目錄下
[root@renyz08 ~]# df -h #查看掛載信息
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root 17G 1.2G 16G 7% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.6M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb1 1014M 33M 982M 4% /ren1
第五步:驗證數據寫入位置
[root@renyz08 ~]# touch /ren1/{1..50}.txt #數據寫入磁盤中
[root@renyz08 ~]# ls /ren1 #查看數據
10.txt 15.txt 1.txt 24.txt 29.txt 33.txt 38.txt 42.txt 47.txt 5.txt
11.txt 16.txt 20.txt 25.txt 2.txt 34.txt 39.txt 43.txt 48.txt 6.txt
12.txt 17.txt 21.txt 26.txt 30.txt 35.txt 3.txt 44.txt 49.txt 7.txt
13.txt 18.txt 22.txt 27.txt 31.txt 36.txt 40.txt 45.txt 4.txt 8.txt
14.txt 19.txt 23.txt 28.txt 32.txt 37.txt 41.txt 46.txt 50.txt 9.txt
[root@renyz08 ~]# umount /ren1 #取消掛載點
[root@renyz08 ~]# ls /ren1 #磁盤中的數據不在原先掛載目錄下
[root@renyz08 ~]# mkdir /ren2 #創建新的掛載點
[root@renyz08 ~]# mount /dev/sdb1 /ren2 #掛載到新的目錄下
[root@renyz08 ~]# ls /ren2 #原先的數據存放到了新的掛載目錄下
10.txt 15.txt 1.txt 24.txt 29.txt 33.txt 38.txt 42.txt 47.txt 5.txt
11.txt 16.txt 20.txt 25.txt 2.txt 34.txt 39.txt 43.txt 48.txt 6.txt
12.txt 17.txt 21.txt 26.txt 30.txt 35.txt 3.txt 44.txt 49.txt 7.txt
13.txt 18.txt 22.txt 27.txt 31.txt 36.txt 40.txt 45.txt 4.txt 8.txt
14.txt 19.txt 23.txt 28.txt 32.txt 37.txt 41.txt 46.txt 50.txt 9.txt
第六步:實現開機自動掛載
方法一:
[root@renyz08 ~]# vim /etc/fstab
/dev/sdb1 /ren2 xfs defaults 0 0
/dev/sdb1 要掛載的分區設備
/ren2 掛載點
xfs 文件系統類型
defaults 掛載選項
0 是否備份
0 是否檢測
寫到該文件中系統容易啟動不起來,解決辦法:
bios方法如下圖:
1、打開電源時進入固件模式
2、CD-ROM Drive系統啟動模式調整到第一位(按+號),F10保存退出
3、選擇Troubleshooting模式
4、選擇Rescue a CentOS system
5、依次按下圖操作,並重啟系統;最后關閉系統,進入固件模式,將BOOT中的系統啟動項還原
方法二:
[root@renyz08 ~]# vim /etc/rc.local
mount /dev/sdb1 /ren1
[root@renyz08 ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 7月 10 14:27 /etc/rc.local -> rc.d/rc.local
#/etc/rc.local是rc.d/rc.local的軟連接文件
[root@renyz08 ~]# chmod +x /etc/rc.d/rc.local
#一定要加執行權限,否則文件中的命令無法執行
例子2:創建擴展分區
第一步:創建擴展分區
[root@renyz08 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e #創建拓展分區選擇e
Partition number (2-4, default 2): 3 #分區編號可以在范圍內自由選擇
First sector (2099200-41943039, default 2099200): #起始扇區默認
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039): +3G #指定擴展分區大小
Partition 3 of type Extended and of size 3 GiB is set
Command (m for help): p #查看
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x1eff4d75
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb3 2099200 8390655 3145728 5 Extended
Command (m for help): w #保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
第二步:查看分區磁盤
[root@renyz08 ~]# ls /dev/sdb*
/dev/sdb /dev/sdb1 /dev/sdb3
#如果發現沒有新創建的磁盤,執行第三步
第三步:同步分區表
[root@ken1 ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: 無法以讀寫方式打開 /dev/sr0 (只讀文件系統)。/dev/sr0 已按照只讀方式打開。
例子3:創建邏輯分區
第一步:創建邏輯分區
[root@renyz08 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l #選擇l,邏輯分區編號是從5開始的,即使主分區的編號沒用完
Adding logical partition 5
First sector (2101248-8390655, default 2101248):
Using default value 2101248
Last sector, +sectors or +size{K,M,G} (2101248-8390655, default 8390655): +4G
Value out of range.
Last sector, +sectors or +size{K,M,G} (2101248-8390655, default 8390655): +1G
Partition 5 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x1eff4d75
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb3 2099200 8390655 3145728 5 Extended
/dev/sdb5 2101248 4198399 1048576 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@renyz08 ~]# partprobe #同步磁盤信息
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@renyz08 ~]# ls /dev/sdb* #查看邏輯分區
/dev/sdb /dev/sdb1 /dev/sdb3 /dev/sdb5
第二步:格式化
[root@renyz08 ~]# mkfs.xfs /dev/sdb5
第三步:掛載
[root@renyz08 ~]# mount /dev/sdb5 /ren2
[root@renyz08 ~]# df -h
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root 17G 1.2G 16G 7% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.6M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb5 1014M 33M 982M 4% /ren2
第四步:開機自啟
[root@renyz08 ~]# echo "mount /dev/sdb5 /ren2" >> /etc/rc.local
二、swap(交換)分區
SWAP(交換)分區是一種通過在硬盤中預先划分一定的空間,然后將把內存中暫時不常用的數據臨時存放到硬盤中,以便騰出物理內存空間讓更活躍的程序服務來使用的技術,其設計目的是為了解決真實物理內存不足的問題。但由於交換分區畢竟是通過硬盤設備讀寫數據的,速度肯定要比物理內存慢,所以只有當真實的物理內存耗盡后才會調用交換分區的資源。
swap交換分區就是把磁盤中一塊當中內存使用
工作中建議不用!
如果工作中發現交換分區用的太多,需要考慮加大內存!
工作中建議交換分區大小設置為真實物理內存的1.5~2倍左右(物理內存為16G的話,交換分區大小設置32G)
查看交換分區:
[root@renyz08 ~]# free -h
total used free shared buff/cache available
Mem: 972M 88M 752M 7.6M 131M 733M
Swap: 2.0G 0B 2.0G
例子1:添加2G交換分區
第一步:創建分區
略
第二步:格式化為swap類型
[root@renyz08 ~]# mkswap /dev/sdb2
正在設置交換空間版本 1,大小 = 2097148 KiB
無標簽,UUID=bfdc606b-8a6a-4375-a7a2-c96edfc2a949
第三步:啟動swap交換分區
[root@renyz08 ~]# swapon /dev/sdb2
[root@renyz08 ~]# free -h
total used free shared buff/cache available
Mem: 972M 86M 768M 7.6M 118M 742M
Swap: 4.0G 0B 4.0G
第四步:swap交換分區開機自啟
vim /etc/fstab
/dev/sdb2 swap swap defaults 0 0
第五步:停止swap
使用swapoff可以停止swap
[root@renyz08 ~]# free -h
total used free shared buff/cache available
Mem: 972M 86M 763M 7.6M 122M 740M
Swap: 4.0G 0B 4.0G
[root@renyz08 ~]# swapoff
Usage:
swapoff [options] [<spec>]
Options:
-a, --all disable all swaps from /proc/swaps
-v, --verbose verbose mode
-h, --help display this help and exit
-V, --version output version information and exit
The <spec> parameter:
-L <label> LABEL of device to be used
-U <uuid> UUID of device to be used
LABEL=<label> LABEL of device to be used
UUID=<uuid> UUID of device to be used
<device> name of device to be used
<file> name of file to be used
For more details see swapoff(8).
[root@renyz08 ~]# swapoff -a
[root@renyz08 ~]# free -h
total used free shared buff/cache available
Mem: 972M 83M 766M 7.6M 122M 742M
Swap: 0B 0B 0B
三、LVM邏輯卷
邏輯卷管理器是Linux系統用於對硬盤分區進行管理的一種機制,理論性較強,其創建初衷是為了解決硬盤設備在創建分區后不易修改分區大小的缺陷。盡管對傳統的硬盤分區進行強制擴容或縮容從理論上來講是可行的,但是卻可能造成數據的丟失。而LVM技術是在硬盤分區和文件系統之間添加了一個邏輯層,它提供了一個抽象的卷組,可以把多塊硬盤進行卷組合並。這樣一來,用戶不必關心物理硬盤設備的底層架構和布局,就可以實現對硬盤分區的動態調整。
物理存儲介質(The physical media):VM存儲介質可以是磁盤分區,整個磁盤,RAID陣列或SAN磁盤,設備必須初始化為LVM物理卷,才能與LVM結合使用
物理卷PV(physical volume):物理卷就是LVM的基本存儲邏輯塊,但和基本的物理存儲介質(如分區、磁盤等)比較,卻包含有與LVM相關的管理參數,創建物理卷它可以用硬盤分區,也可以用硬盤本身;
卷組VG(Volume Group):一個LVM卷組由一個或多個物理卷組成??
邏輯卷LV(logical volume):LV建立在VG之上,可以在LV之上建立文件系統
PE(physical extents):PV物理卷中可以分配的最小存儲單元,PE的大小是可以指定的,默認為4MB
LE(logical extent):LV邏輯卷中可以分配的最小存儲單元,在同一個卷組中,LE的大小和PE是相同的,並且一一對應
常用的LVM部署命令:
功能/命令 物理卷管理 卷組管理 邏輯卷管理
掃描 pvscan vgscan lvscan
建立 pvcreate vgcreate lvcreate
顯示 pvdisplay vgdisplay lvdisplay
刪除 pvremove vgremove lvremove
擴展 vgextend lvextend
縮小 vgreduce lvreduce
例子1:創建邏輯卷
第一步:創建物理卷
[root@renyz08 ~]# pvcreate /dev/sd{b,c,d}
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
Physical volume "/dev/sdd" successfully created.
第二步:創建卷組
[root@renyz08 ~]# vgcreate ren /dev/sd{b,c,d}
Volume group "ren" successfully created
第三步:查看卷組信息
[root@renyz08 ~]# vgdisplay ren
第四步:創建邏輯卷
[root@renyz08 ~]# lvcreate -L 5G -n lv_ren ren
#-L指定創建磁盤的大小,-l指定PE的數量,PE大小4M
# lvcreate -l 100%vg -n lv_ren ren #利用全部空間來創建lv(或者100%free)
第五步:查看邏輯卷詳細
[root@renyz08 ~]# lvdisplay /dev/ren/lv_ren
第六步:格式化
[root@renyz08 ~]# mkfs.xfs /dev/ren/lv_ren
第七步:掛載
[root@renyz08 ~]# mkdir /ren
[root@renyz08 ~]# mount /dev/ren/lv_ren /ren
[root@renyz08 ~]# df -h
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root 17G 1.2G 16G 7% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/ren-lv_ren 5.0G 33M 5.0G 1% /ren
四、LVM擴容
ext4擴容不能在線擴容,必須卸載才能擴容,支持擴容和縮容
xfs支持在線動態擴容,不支持縮容
1、xfs格式擴容
例子:
第一步:擴容
[root@renyz08 ~]# lvextend -L +5G /dev/ren/lv_ren
Size of logical volume ren/lv_ren changed from 5.00 GiB (1280 extents) to 10.00 GiB (2560 extents).
Logical volume ren/lv_ren successfully resized.
第二步:查看磁盤大小
[root@renyz08 ~]# df -h
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root 17G 1.2G 16G 7% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/ren-lv_ren 5.0G 33M 5.0G 1% /ren
第三步:執行xfs_growfs
[root@renyz08 ~]# xfs_growfs /dev/ren/lv_ren
meta-data=/dev/mapper/ren-lv_ren isize=512 agcount=4, agsize=327680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=1310720, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 1310720 to 2621440
第四步:再次查看磁盤大小
[root@renyz08 ~]# df -h
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root 17G 1.2G 16G 7% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/ren-lv_ren 10G 33M 10G 1% /ren
五、LVM邏輯卷刪除
第一步:卸載光盤(取消掛載)
[root@renyz08 ~]# umount /ren
第二步:刪除邏輯卷
[root@renyz08 ~]# lvremove /dev/ren/lv_ren
Do you really want to remove active logical volume ren/lv_ren? [y/n]: y
Logical volume "lv_ren" successfully removed
第三步:刪除卷組
[root@renyz08 ~]# vgremove ren
Volume group "ren" successfully removed
第四步:刪除物理卷
[root@renyz08 ~]# pvremove /dev/sd{b,c,d}
Labels on physical volume "/dev/sdb" successfully wiped.
Labels on physical volume "/dev/sdc" successfully wiped.
Labels on physical volume "/dev/sdd" successfully wiped.
作業:擴容根磁盤,在原有基礎上加10G
(vg的剩余空間不足)
[root@renyz08 ~]# df -h #查看原先的磁盤容量
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root 17G 1.2G 16G 7% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.6M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
第一步:查詢vg的剩余空間
[root@renyz08 ~]# vgdisplay
第二步:查看是否有可用的pv卷
[root@renyz08 ~]# pvscan
PV /dev/sda2 VG centos lvm2 [<19.00 GiB / 0 free]
Total: 1 [<19.00 GiB] / in use: 1 [<19.00 GiB] / in no VG: 0 [0 ]
第三步:添加硬盤並轉換為pv卷
[root@renyz08 ~]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.
[root@renyz08 ~]# pvscan
PV /dev/sda2 VG centos lvm2 [<19.00 GiB / 0 free]
PV /dev/sdb lvm2 [20.00 GiB]
Total: 2 [<39.00 GiB] / in use: 1 [<19.00 GiB] / in no VG: 1 [20.00 GiB]
第四步:將新pv卷添加至vg
[root@renyz08 ~]# vgextend centos /dev/sdb
Volume group "centos" successfully extended
第五步:調整lv的大小
[root@renyz08 ~]# lvresize -L +10G /dev/centos/root (或者lvextend)
Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <27.00 GiB (6911 extents).
Logical volume centos/root successfully resized.
第六步:將lv的最新容量通知給文件系統
[root@renyz08 ~]# xfs_growfs /dev/centos/root
[root@renyz08 ~]# df -h #再次查看,磁盤大小已改變
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root 27G 1.2G 26G 5% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.6M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
vg的剩余空間充足
1. 查詢vg的剩余空間
# vgdisplay
2. 調整lv的大小
# lvresize -L +5G /dev/vgwtl/lvwtl
3. 將lv的最新容量通知給文件系統
# xfs_growfs /dev/vgwtl/lvwtl
4. 查看更新結果
# df -h
六、ext4格式擴容
1、ext4格式擴容
第一步:卸載
[root@ken ~]# umount /test1
第二步:把ken1邏輯卷擴展到300M
[root@ken ~]# umount /test1
[root@ken ~]# lvextend -L 300M /dev/ken/ken1
Size of logical volume ken/ken1 changed from 100.00 MiB (25 extents) to 300.00 MiB (75 extents).
Logical volume ken/ken1 successfully resized.
第三步:檢查磁盤完整性
[root@ken ~]# e2fsck -f /dev/ken/ken1
e2fsck 1.42.9 (28-Dec-2013)
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/ken/ken1: 11/25688 files (9.1% non-contiguous), 8896/102400 blocks
第四步:重置硬盤容量
[root@ken ~]# resize2fs /dev/ken/ken1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/ken/ken1 to 307200 (1k) blocks.
The filesystem on /dev/ken/ken1 is now 307200 blocks long.
第五步:重新掛載
可以發現現在已經是300M了
[root@ken ~]# mount /dev/ken/ken1 /test1
[root@ken ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.2G 16G 7% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 130M 885M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/ken-ken1 287M 2.0M 266M 1% /test1
2、縮小邏輯卷
相較於擴容邏輯卷,在對邏輯卷進行縮容操作時,其丟失數據的風險更大。所以在生產環境中執行相應操作時,一定要提前備份好數據。另外Linux系統規定,在對LVM邏輯卷進行縮容操作之前,要先檢查文件系統的完整性(當然這也是為了保證我們的數據安全)。在執行縮容操作前記得先把文件系統卸載掉。
第一步:卸載
[root@ken ~]# umount /test1/
第二步:檢查系統完整性
[root@ken ~]# e2fsck -f /dev/ken/ken1
e2fsck 1.42.9 (28-Dec-2013)
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/ken/ken1: 11/75088 files (9.1% non-contiguous), 15637/307200 blocks
第三步:大小重置
[root@ken ~]# resize2fs /dev/ken/ken1 200M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/ken/ken1 to 204800 (1k) blocks.
The filesystem on /dev/ken/ken1 is now 204800 blocks long.
第四步:縮小到200M
[root@ken ~]# lvreduce -L 200M /dev/ken/ken1
WARNING: Reducing active logical volume to 200.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce ken/ken1? [y/n]: y
Size of logical volume ken/ken1 changed from 300.00 MiB (75 extents) to 200.00 MiB (50 extents).
Logical volume ken/ken1 successfully resized.
第五步:重新掛載使用
[root@ken ~]# mount /dev/ken/ken1 /test1/
[root@ken ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.2G 16G 7% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 130M 885M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/ken-ken1 190M 1.6M 175M 1% /test1