挂载新硬盘至新分区(传统方式)

linux挂载硬盘步骤(简单易用,直接上手,无废话)


查看磁盘挂载情况

fdisk -l

新硬盘分区

fdisk /dev/vdb

图片资源引用博客内容

格式化硬盘文件类型

给硬盘进行格式化

mkfs.xfs /dev/vdb1

挂载磁盘

创建新目录

mkdir -p /usr/local/services

将新分区 /dev/vdb1 挂载到目录下

mount /dev/vdb1 /usr/local/services

查看挂载情况

mount

设置自动挂载

获取需要挂载磁盘UUID

blkid

修改/etc/fstab文件

vim /etc/fstab

重启

reboot

查看磁盘信息

df -Th

修改磁盘挂载为lvm卷组方式

**注意使用pvdisplaylvdisplay查看有无卷组信息,当前博客是基于卷组扩容磁盘!!! **

博客基于#1 挂载新硬盘至新分区,在指定分区扩容则需如下步骤:

移除原挂载磁盘信息:

umount /dev/vdb1

注释代码配置文件中内容项:

vim /etc/fstab

删除原分区信息

fdisk -l

当前博客内容基于#1配置,所以需要删除原/dev/vdb已分区信息

挂载新硬盘至新分区(LVM卷组方式)

Linux系统系统盘扩容,亲测可用

linux创建物理卷、卷组、逻辑卷以及格式化、挂载

【解决】linux磁盘扩容大全:新增磁盘、原磁盘扩容、home分区root分区扩容

注:现在需要解锁…了,之前不需要的。


创建新分区

fdisk /dev/vdb

简要步骤(概览):

m #菜单menu选项提示
n #创建新分区
p #主分区 (需要确认位置、分区起始位、分区结束位)
t #变更分区类型
8e # 8e 对应 Linux LVM
p #打印分区信息(已变更类型)
w #保存退出

完整步骤(详细):

[root@localhost ~]# 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 0xaf2c1952.

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): 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):
Using default value 41943039
Partition 1 of type Linux and of size 20 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: 0xaf2c1952

Device Boot Start End Blocks Id System
/dev/sdb1 2048 41943039 20970496 83 Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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: 0xaf2c1952

Device Boot Start End Blocks Id System
/dev/sdb1 2048 41943039 20970496 8e Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#

强制让内核重新查找分区表,否则操作需要重启才能生效

partprobe

报错提示:Linux 执行partprobe命令时遇到Unable to open /dev/sr0 read-write (Read-only file system)

# 使用`partprobe`检查扫描指定设备`/dev/sdb`
partprobe /dev/sdb

新添加硬盘支持LVM技术

查询已创建新分区

fdisk -l
Disk /dev/vdb: 300 GiB, 322122547200 bytes, 629145600 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
Disklabel type: dos
Disk identifier: 0x6f25a77b

Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 629145599 629143552 300G 8e Linux LVM

创建物理卷

创建/dev/vdb分区的物理券/dev/vdb1

若命令pvcreate不存在,则需要执行yum -y install lvm2安装

pvcreate /dev/vdb1

查看物理卷

pvs
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 klas lvm2 a-- 98.00g 4.00m
/dev/vdb1 lvm2 --- <300.00g <300.00g

创建卷组

vg1是卷组名(需要新建的),/dev/vdb1是物理卷(上一个步骤创建的物理券)

vgcreate vg1 /dev/vdb1
[root@localhost ~]# vgcreate vg1 /dev/vdb1 
Physical volume "/dev/vdb1" successfully created.
Volume group "vg1" successfully created

查看卷组信息

vgs
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 3 0 wz--n- <59.00g 0
vg1 1 0 0 wz--n- <20.00g <20.00g

创建逻辑卷

lvcreate -n lv1 -l 100%FREE vg1
  • lvcreate 创建逻辑卷的命令
  • -n lv1 指定逻辑卷的名称为lv1
  • -l 100%FREE 指定使用卷组 vg1 中所有剩余的可用空间
  • vg1是卷组的名称
[root@localhost ~]# lvcreate -n lv1 -l 100%FREE vg1
Logical volume "lv1" created.

查看逻辑卷信息

lvs
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home centos -wi-ao---- <17.39g
root centos -wi-ao---- <35.61g
swap centos -wi-ao---- 6.00g
lv1 vg1 -wi-a----- <20.00g

逻辑卷制作文件系统

fdisk -l

[root@localhost ~]# mkfs.xfs /dev/mapper/vg1-lv1
meta-data=/dev/mapper/vg1-lv1 isize=512 agcount=4, agsize=1310464 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5241856, 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@localhost ~]# blkid /dev/mapper/vg1-lv1 
/dev/mapper/vg1-lv1: UUID="9bdbd023-6ca8-4e51-aabd-fd3d3de5d641" TYPE="xfs"

磁盘挂载(重启后失效)

创建目录

mkdir /test

挂载目录

mount /dev/mapper/vg1-lv1 /test

查看挂载情况

忽略当前挂载磁盘容量与实际相关部分命令不符情况,仅作示例。

[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
...
/dev/mapper/vg1-lv1 xfs 20G 33M 20G 1% /test

磁盘挂载(永久生效)

修改/etc/fstab文件,添加需要挂载的磁盘信息(空格隔开)。

UUID=9bdbd023-6ca8-4e51-aabd-fd3d3de5d641 /test xfs defaults 0 0

重启验证挂载是否成功

reboot

此种方式挂载磁盘的优点是,后续需要挂载新硬盘分区就特别容易实现,且方便管理。

扩容硬盘空间

Linux 扩容 / 根分区(LVM+非LVM)

关注博客:三、CentOS7,非LVM根分区扩容步骤

传统扩容磁盘方式

LVM卷组扩容磁盘方式

LVM分区扩容 - 扩展磁盘容量

查看磁盘信息

已拓展磁盘空间:20G (vmware虚拟机,拓展磁盘空间)

fdisk -l
Disk /dev/sdb: 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: dos
Disk identifier: 0xaf2c1952

Device Boot Start End Blocks Id System
/dev/sdb1 2048 41943039 20970496 8e Linux LVM

创建分区

[root@localhost ~]# 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): 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 (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2):
First sector (41943040-83886079, default 41943040):
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-83886079, default 83886079):
Using default value 83886079
Partition 2 of type Linux and of size 20 GiB is set

Command (m for help): p

Disk /dev/sdb: 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: dos
Disk identifier: 0xaf2c1952

Device Boot Start End Blocks Id System
/dev/sdb1 2048 41943039 20970496 8e Linux LVM
/dev/sdb2 41943040 83886079 20971520 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@localhost ~]#

刷新分区并创建物理卷

[root@localhost ~]# partprobe /dev/sdb
[root@localhost ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created.

查看卷组信息

[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 49.99 GiB
PE Size 4.00 MiB
Total PE 12798
Alloc PE / Size 12798 / 49.99 GiB
Free PE / Size 0 / 0
VG UUID 8yvHrE-6M4Y-6qNb-dunc-2gD5-vRnR-kKRfTZ

...

将物理券扩展到卷组

[root@localhost ~]# vgextend vg1 /dev/sdb2
Volume group "vg1" successfully extended

扩展逻辑卷

[root@localhost ~]# lvextend -l +100%FREE /dev/mapper/vg1-lv1 
Size of logical volume vg1/lv1 changed from 49.99 GiB (12798 extents) to <69.99 GiB (17917 extents).
Logical volume vg1/lv1 successfully resized.

扩展文件系统(对于 xfs 文件系统)

[root@localhost ~]# xfs_growfs /dev/mapper/vg1-lv1 
meta-data=/dev/mapper/vg1-lv1 isize=512 agcount=11, agsize=1310464 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=13105152, 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 13105152 to 18347008

查看磁盘信息(前后对照)

拓展分区/test空间前磁盘空间:50G

[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 3.9G 12M 3.8G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 36G 3.0G 33G 9% /
/dev/sda1 xfs 1014M 194M 821M 20% /boot
/dev/mapper/vg1-lv1 xfs 50G 33M 50G 1% /test
/dev/mapper/centos-home xfs 18G 33M 18G 1% /home
tmpfs tmpfs 781M 0 781M 0% /run/user/0

已拓展/test分区磁盘空间:70G

[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 3.9G 12M 3.8G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 36G 3.0G 33G 9% /
/dev/sda1 xfs 1014M 194M 821M 20% /boot
/dev/mapper/vg1-lv1 xfs 70G 33M 70G 1% /test
/dev/mapper/centos-home xfs 18G 33M 18G 1% /home
tmpfs tmpfs 781M 0 781M 0% /run/user/0
[root@localhost ~]#

LVM分区扩容 - 新增物理磁盘

vmware虚拟机,新增磁盘

查看卷组信息

[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name vg1
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 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <20.00 GiB
PE Size 4.00 MiB
Total PE 5119
Alloc PE / Size 5119 / <20.00 GiB
Free PE / Size 0 / 0
VG UUID 8yvHrE-6M4Y-6qNb-dunc-2gD5-vRnR-kKRfTZ
[root@localhost ~]#

查看新添加硬盘

[root@localhost ~]# fdisk -l
Disk /dev/sdc: 32.2 GB, 32212254720 bytes, 62914560 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@localhost ~]# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created.
[root@localhost ~]#

将物理卷添加到现有卷组中

[root@localhost ~]# vgextend vg1 /dev/sdc
Volume group "vg1" successfully extended
[root@localhost ~]#

验证卷组

新添加的物理卷:Free PE / Size 7679 / <30.00 GiB


[root@localhost ~]# vgdisplay vg1
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 49.99 GiB
PE Size 4.00 MiB
Total PE 12798
Alloc PE / Size 5119 / <20.00 GiB
Free PE / Size 7679 / <30.00 GiB
VG UUID 8yvHrE-6M4Y-6qNb-dunc-2gD5-vRnR-kKRfTZ

[root@localhost ~]#

扩展卷空间

# 扩展逻辑卷 
lvextend -l +100%FREE /dev/mapper/vg1-lv1
Size of logical volume vg1/lv1 changed from <20.00 GiB (5119 extents) to 49.99 GiB (12798 extents).
Logical volume vg1/lv1 successfully resized.
# 扩展文件系统(对于 xfs 文件系统)
[root@localhost ~]# xfs_growfs /dev/mapper/vg1-lv1
meta-data=/dev/mapper/vg1-lv1 isize=512 agcount=4, agsize=1310464 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=5241856, 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 5241856 to 13105152
[root@localhost ~]#

查看磁盘信息

[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 3.9G 12M 3.8G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 36G 3.0G 33G 9% /
/dev/sda1 xfs 1014M 194M 821M 20% /boot
/dev/mapper/centos-home xfs 18G 33M 18G 1% /home
/dev/mapper/vg1-lv1 xfs 50G 33M 50G 1% /test
tmpfs tmpfs 781M 0 781M 0% /run/user/0
[root@localhost ~]#