【轉載】Linux系統磁盤擴容

 

一、基本概念:

邏輯卷管理(Logical Volume Manager):LVM將一個或多個硬盤的分區在邏輯上集合,相當於一個大硬盤來使用,
當硬盤的空間不夠使用的時候,可以繼續將其它的硬盤的分區加入其中,這樣可以實現磁盤空間的動態管理。
物理存儲介質(The Physical Media):這裏指系統的存儲設備:硬盤(如/dev/sda),是存儲系統最低層的存儲單元。
物理卷(Physical Volume):指硬盤分區或從邏輯上與磁盤分區具有同樣功能的設備(如RAID),是LVM的基本存儲邏輯塊。
卷組(Volume Group):LVM卷組由一個或多個物理卷組成。可以在卷組上創建一個或多個邏輯卷。
邏輯卷(Logical Volume):類似於非LVM系統中的硬盤分區,在邏輯卷之上可以建立文件系統 (如/home),即爲邏輯分區。
 
如下圖所示PV、VG、LV三者關係:
 
 

二、常規操作及命令

1、實施LVM存儲

# a.進入交互式界面後,創建新分區
fdisk /dev/sda
 
# b.創建物理卷(pv)
pvcreate /dev/sda4
 
### 可使用pvs、pvdisplay查看
 
# c.創建卷組(vg)
vgcreate vg4 /dev/sda4
 
### 可使用vgs、vgdisplay查看
 
# d.創建邏輯卷(lv)
lvcreate -n lv4 vg4 -L 1G
 
### 可使用lvs、lvdisplay查看
 
# e.建立文件系統(-t指定文件系統類型)
mkfs -t xfs /dev/vg4/lv4
 
# f.掛載文件目錄
mount /dev/vg4/lv4 /data //臨時掛載
vi /etc/fstab //永久掛載(在配置文件追加)
/dev/vg4/lv4 /data xfs defaults 0 0
 
# g.重新加載並查看效果
mount -a && df -Th

 

2、刪除邏輯卷

# a.卸載掛載點
umount /data
 
### 若提示繁忙,可使用 fuser -m -k /data 結束掉
 
# b.刪除邏輯卷
lvremove /dev/vg4/lv4
 
# c.刪除卷組
vgremove vg4
 
# d.刪除物理卷
pvremove /dev/sda4

 

3、擴展和縮減

# 假如vg容量不夠,需要新建分區-創建物理卷-擴展卷組
fdisk /dev/sda
pvcreate /dev/sda4
vgextend vg4 /dev/sda4
 
# 若想縮減卷組需要縮減卷組-縮減物理卷
vgreduce vg4 /dev/vg1   
pvremove /dev/sda4
 
# 擴展邏輯卷後需根據文件系統類型進行擴展
lvextend /dev/vg4/lv1 -L 500M //將LV擴大至500M
xfs_growfs /dev/vg4/lv4     //xfs文件系統擴展方法
resize2fs /dev/vg4/lv4     //ext4文件系統擴展方法

 

 

三、實戰演示:

1、前期準備

a)以虛擬機作爲演示,先查看磁盤信息( fdisk -l )和系統磁盤大小(df -h ),當前均爲40G。
 
b)關機虛擬機,擴展磁盤大小到50G。
 
c)啓動虛擬機,再次查看磁盤信息,增加到50G(系統磁盤還是40G,是因爲磁盤沒有做分區)。
 

2、創建和刪除分區

a)創建一個普通分區( fdisk /dev/sda )

注意:不同操作系統的磁盤命名方式不同,比如Ubuntu是/dev/vda,請根據磁盤信息首行顯示Disk值。
[root@k8s-master ~]# fdisk -l
 
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000a4e32
 
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 4812799 2098176 82 Linux swap / Solaris
/dev/sda3 4812800 83886079 39536640 83 Linux
[root@k8s-master ~]#
[root@k8s-master ~]# fdisk /dev/sda
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): 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 partitions 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 (3 primary, 0 extended, 1 free) //選擇主分區(最多4個)
e extended //擴展分區
Select (default e): p
Selected partition 4 //選擇分區號
First sector (83886080-104857599, default 83886080): //分區起始位置(空格則使用默認值)
Using default value 83886080
Last sector, +sectors or +size{K,M,G} (83886080-104857599, default 104857599):
Using default value 104857599 //分區結束位置(或指定大小+10G)
Partition 4 of type Linux and of size 10 GiB is set
 
Command (m for help): p //查看分區情況
 
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000a4e32
 
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 4812799 2098176 82 Linux swap / Solaris
/dev/sda3 4812800 83886079 39536640 83 Linux
/dev/sda4 83886080 104857599 10485760 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@k8s-master ~]# partprobe //使分區表生效(無需重啓)

 

b)創建一個LVM分區

[root@k8s-master ~]# fdisk /dev/sda
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 (3 primary, 0 extended, 1 free)
e extended
Select (default e): p
Selected partition 4
First sector (83886080-104857599, default 83886080):
Using default value 83886080
Last sector, +sectors or +size{K,M,G} (83886080-104857599, default 104857599): +5G
Partition 4 of type Linux and of size 5 GiB is set //指定分區大小
 
Command (m for help): t //修改分區格式
Partition number (1-4, default 4): 4 //選擇分區
Hex code (type L to list all codes): 8e //8e類型即爲LVM
Changed type of partition 'Linux' to 'Linux LVM'
 
Command (m for help): p //查看當前分區
 
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000a4e32
 
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 4812799 2098176 82 Linux swap / Solaris
/dev/sda3 4812800 83886079 39536640 83 Linux
/dev/sda4 83886080 94371839 5242880 8e Linux LVM

 

c)刪除一個分區

[root@k8s-master ~]# fdisk /dev/sda
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): d
Partition number (1-4, default 4): 4
Partition 4 is deleted
 
Command (m for help): q
 

 

3、掛載磁盤到指定目錄

[root@Server]# mkfs.xfs -f /dev/vdb1
meta-data=/dev/vdb1 isize=512 agcount=4, agsize=32767936 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=0
data = bsize=4096 blocks=131071744, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=63999, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
 
[root@Server /]# mkdir /data
[root@Server /]# mount /dev/vdb1 /data
[root@Server /]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 16G 0 16G 0% /dev
tmpfs tmpfs 16G 256K 16G 1% /dev/shm
tmpfs tmpfs 16G 22M 16G 1% /run
tmpfs tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/vda3 xfs 14G 7.9G 6.0G 58% /
tmpfs tmpfs 16G 64K 16G 1% /tmp
/dev/vda2 xfs 1014M 222M 793M 22% /boot
/dev/vda1 vfat 200M 5.8M 195M 3% /boot/efi
tmpfs tmpfs 3.2G 576K 3.2G 1% /run/user/0
/dev/sr1 iso9660 478K 478K 0 100% /run/media/root/config-2
/dev/vdb1 xfs 500G 543M 500G 1% /data
[root@Server /]# echo "/dev/vdb1 /data xfs defaults 0 0" >> /etc/fstab
[root@Server-6822768a-984b-4aa9-afb4-d6e34b24e4bc /]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Dec 1 10:40:10 2020
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=f9d06f7f-0fe3-4dbf-8c30-2a8cd8f3ac3b / xfs defaults 0 0
UUID=c5f65c3b-114d-4c04-adf7-1e2e7351276e /boot xfs defaults 0 0
UUID=D071-A4FA /boot/efi vfat umask=0077,shortname=winnt 0 2
/dev/vdb1 /data xfs defaults 0 0
 
[root@Server /]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 256K 16G 1% /dev/shm
tmpfs 16G 22M 16G 1% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/vda3 14G 7.9G 6.0G 58% /
tmpfs 16G 64K 16G 1% /tmp
/dev/vda2 1014M 222M 793M 22% /boot
/dev/vda1 200M 5.8M 195M 3% /boot/efi
tmpfs 3.2G 576K 3.2G 1% /run/user/0
/dev/sr1 478K 478K 0 100% /run/media/root/config-2
/dev/vdb1 500G 543M 500G 1% /data

 

 

4、創建和刪除邏輯卷

a)創建一個邏輯卷並掛載

注意:這裏/dev/sda4 分區名請根據自己實際情況修改。
[root@k8s-master ~]# pvcreate /dev/sda4
Physical volume "/dev/sda4" successfully created.
[root@k8s-master ~]# pvdisplay
"/dev/sda4" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda4
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID bp2axz-XfE1-m1Ca-6BX4-G22d-uliq-1q88Ky
 
[root@k8s-master ~]# vgcreate vg4 /dev/sda4
Volume group "vg4" successfully created
[root@k8s-master ~]# vgdisplay
--- Volume group ---
VG Name vg4
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size <10.00 GiB
PE Size 4.00 MiB
Total PE 2559
Alloc PE / Size 256 / 1.00 GiB
Free PE / Size 2303 / <9.00 GiB
VG UUID 0zg2zi-EcJx-d26s-cTwN-XSqD-oF4H-pmo8UP
 
[root@k8s-master ~]# lvcreate -n lv4 vg4 -L 1G
Logical volume "lv4" created.
[root@k8s-master ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg4/lv4
LV Name lv4
VG Name vg4
LV UUID FPWpmo-5cad-mRe1-LM3D-65ab-AcZW-kRxZpZ
LV Write Access read/write
LV Creation host, time k8s-master, 2021-06-11 02:56:36 -0700
LV Status available
# open 0
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
 
[root@k8s-master ~]# mkfs -t xfs /dev/vg4/lv4
meta-data=/dev/vg4/lv4 isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
 
[root@k8s-master ~]# mkdir /data
[root@k8s-master ~]# mount /dev/vg4/lv4 /data
[root@k8s-master ~]# df -Th | grep data
/dev/mapper/vg4-lv4 xfs 1014M 33M 982M 4% /data

 

b)刪除一個邏輯卷

[root@k8s-master ~]# umount /data
[root@k8s-master ~]#
[root@k8s-master ~]# lvremove /dev/vg4/lv4
Do you really want to remove active logical volume vg4/lv4? [y/n]: y
Logical volume "lv4" successfully removed
[root@k8s-master ~]#
[root@k8s-master ~]# lvremove /dev/vg4/lv4
Failed to find logical volume "vg4/lv4"
[root@k8s-master ~]#
[root@k8s-master ~]# vgremove vg4
Volume group "vg4" successfully removed
[root@k8s-master ~]#
[root@k8s-master ~]# pvremove /dev/sda4
Labels on physical volume "/dev/sda4" successfully wiped.
[root@k8s-master ~]#
[root@k8s-master ~]# df -hT | grep data
[root@k8s-master ~]#

 

使用LVM對根分區進行擴容:https://blog.csdn.net/qq_29292203/article/details/106237908

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章