Linux 根分区扩容

背景内容

1、在生产环境中,我们会遇到分区大于2T的磁盘(比如:添加一个3TB的存储),由于MBR分区表只支持2T磁盘,所以大于2T的磁盘必须使用GPT分区表

2、而fdisk是不支持GPT分区的,我们可以使用parted来对GPT磁盘操作。parted功能很强大,既可用命令行也可以用于交互式,在提示符下输入parted就会进入交互式模式,如果有多个磁盘的话,我们需要运行select sdX(X为磁盘)来进行磁盘的选择,也可直接用parted /dev/sdX指定相应的硬盘

3、根分区扩容2T

正文

一、2T硬盘挂载

查看磁盘信息:
[root@charseki ~]# fdisk -l

Disk /dev/sda: 2199.0 GB, 2199023255552 bytes, 4294967296 sectorsUnits = sectors of 1 * 512 = 512 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes Device Boot Start End Blocks Id System
/dev/sdm1 1 2089 16777216 82 Linux swap / Solaris /dev/sdm2 * 2089 36405 275643392 83 Linux [root@charseki ~]# parted GNU Parted 3.1 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) (parted) select /dev/sda //选择磁盘sda Using /dev/sda (parted) mklabel gpt //将MBR磁盘格式化为GPT (parted) mkpart primary 0% 100% //将整块磁盘分成一个分区 (parted) quit //退出 [root@charseki ~]# mkfs.ext4 /dev/sda //格式化 [root@charseki ~]# mkdir /waf [root@charseki ~]# mount /dev/sda /waf //挂载 [root@yoon ~]# blkid /dev/sda /dev/sda: UUID="c4b75461-571b-4c63-9054-50625a1f8554" TYPE="ext4" [root@yoon ~]# vi /etc/fstab UUID=c4b75461-571b-4c63-9054-50625a1f8554 /waf ext4 defaults 0 0 [root@yoon ~]# df -h /dev/sda                 2.0T   81M  1.9T   1% /waf

 一、扩容根分区

[root@10-20-186-237 ~]# fdisk -l    //查看磁盘
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: gpt
Disk identifier: B570F729-2229-4D65-920D-BA479A4FD9A5


#         Start          End    Size  Type            Name
 1         2048      1050623    512M  EFI System      EFI System Partition
 2      1050624      2099199    512M  Microsoft basic 
 3      2099200     83884031     39G  Linux LVM       

Disk /dev/sda: 2199.0 GB, 2199023255552 bytes, 4294967296 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 /dev/mapper/centos-root: 33.3 GB, 33281802240 bytes, 65003520 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 /dev/mapper/centos-swap: 8589 MB, 8589934592 bytes, 16777216 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


------------------------------------------------------------------------

[root@10-20-186-237 ~]# 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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xf9674e64.

WARNING: The size of this disk is 2.2 TB (2199023255552 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).


Command (m for help):

------------------------------------------------------------------------

Command (m for help): m    //输入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    //输入n新增分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):

 

------------------------------------------------------------------------

Select (default p): p    //输入 p 选择新增主分区并选择分区号、第一个扇区、最后一个扇区号(在此均用默认值)
Partition number (1-4, default 1): 1
First sector (2048-4294967295, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4294967294, default 4294967294):
Using default value 4294967294
Partition 1 of type Linux and of size 2 TiB is set

Command (m for help): w    //输入 w 写入分区表
The partition table has been altered!

Calling ioctl() to re-read partition table.

------------------------------------------------------------------------

 

[root@10-20-186-237 ~]# fdisk -l    //再次查看磁盘情况,已经多了一块分区

WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: gpt
Disk identifier: B570F729-2229-4D65-920D-BA479A4FD9A5


# Start End Size Type Name
1 2048 1050623 512M EFI System EFI System Partition
2 1050624 2099199 512M Microsoft basic
3 2099200 83884031 39G Linux LVM

Disk /dev/sda: 2199.0 GB, 2199023255552 bytes, 4294967296 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: 0xf9674e64

Device Boot Start End Blocks Id System
/dev/sda1 2048 4294967294 2147482623+ 83 Linux

Disk /dev/mapper/centos-root: 33.3 GB, 33281802240 bytes, 65003520 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 /dev/mapper/centos-swap: 8589 MB, 8589934592 bytes, 16777216 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

------------------------------------------------------------------------

[root@10-20-186-237 ~]# vgdisplay -v    //查看卷分组
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <39.00 GiB
PE Size 4.00 MiB
Total PE 9983
Alloc PE / Size 9983 / <39.00 GiB
Free PE / Size 0 / 0
VG UUID 0JZg9n-aoRg-9duT-VikF-p7Ju-UU2j-8eqKke

--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID paEgF1-YSfJ-sfDd-sVvy-RS0r-9zBi-j7k4Cu
LV Write Access read/write
LV Creation host, time 10-20-186-166, 2020-09-01 11:49:13 +0800
LV Status available
# open 2
LV Size 8.00 GiB
Current LE 2048
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1

--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID 8Vsd3A-22o1-Qj73-EW5j-a5ot-my9C-u8LuUM
LV Write Access read/write
LV Creation host, time 10-20-186-166, 2020-09-01 11:49:14 +0800
LV Status available
# open 1
LV Size <31.00 GiB
Current LE 7935
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

--- Physical volumes ---
PV Name /dev/vda3
PV UUID azZoyV-vpTs-4ZVG-n1kc-Mq6q-2Hh9-IBqQpo
PV Status allocatable
Total PE / Free PE 9983 / 0

------------------------------------------------------------------------

[root@10-20-186-237 ~]# pvcreate /dev/sda1    //为之前新增的分区创建物理卷
Physical volume "/dev/sda1" successfully created.

------------------------------------------------------------------------

 

[root@10-20-186-237 ~]# pvdisplay    //再次查看结果

--- Physical volume ---
PV Name /dev/vda3
VG Name centos
PV Size <39.00 GiB / not usable 2.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 9983
Free PE 0
Allocated PE 9983
PV UUID azZoyV-vpTs-4ZVG-n1kc-Mq6q-2Hh9-IBqQpo

"/dev/sda1" is a new physical volume of "<2.00 TiB"
--- NEW Physical volume ---
PV Name /dev/sda1
VG Name
PV Size <2.00 TiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID NNEV5v-gh4P-MHTP-kpX9-jxiN-41dQ-3cGZfU

------------------------------------------------------------------------

[root@10-20-186-237 ~]# vgextend centos /dev/sda1    //扩展卷分组
Volume group "centos" successfully extended

------------------------------------------------------------------------

[root@10-20-186-237 ~]# lvdisplay    //查看逻辑卷
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID paEgF1-YSfJ-sfDd-sVvy-RS0r-9zBi-j7k4Cu
LV Write Access read/write
LV Creation host, time 10-20-186-166, 2020-09-01 11:49:13 +0800
LV Status available
# open 2
LV Size 8.00 GiB
Current LE 2048
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1

--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID 8Vsd3A-22o1-Qj73-EW5j-a5ot-my9C-u8LuUM
LV Write Access read/write
LV Creation host, time 10-20-186-166, 2020-09-01 11:49:14 +0800
LV Status available
# open 1
LV Size <31.00 GiB
Current LE 7935
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

------------------------------------------------------------------------

[root@10-20-186-237 ~]# lvextend -L +2000G /dev/centos/root    //扩展逻辑卷
Size of logical volume centos/root changed from <31.00 GiB (7935 extents) to 1.98 TiB (519935 extents).
Logical volume centos/root successfully resized.

------------------------------------------------------------------------

[root@10-20-186-237 ~]# xfs_growfs /dev/centos/root    //扩大文件系统
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=2031360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=8125440, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=3967, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 8125440 to 532413440

//查看扩容结果

 

作者:Nobita Chen

-----------------------------------------------

慢慢的,你总会发现,你的努力没有白费。

posted @ 2021-03-26 16:38  charseki  阅读(310)  评论(0编辑  收藏  举报