Linux挂载硬盘分区
一、分区
采用fdisk分区,本次新挂载扩展分区,因为是扩容。
# 查看分区前磁盘状态
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 256M 0 part /boot
└─sda2 8:2 0 19.8G 0 part
└─centos-root 253:0 0 19.8G 0 lvm /
sdb 8:16 0 100G 0 disk
sr0 11:0 1 1024M 0 rom
# 开始分区
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# fdisk -l
Disk /dev/sda: 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: 0x0009913a
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 526335 262144 83 Linux
/dev/sda2 526336 41943039 20708352 8e Linux LVM
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 21.2 GB, 21202206720 bytes, 41410560 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@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# 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 0xe957a076.
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):
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): e
Partition number (1-4, default 1):
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):
Using default value 209715199
Partition 1 of type Extended and of size 100 GiB is set
Command (m for help):
Command (m for help): n
Partition type:
p primary (0 primary, 1 extended, 3 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (4096-209715199, default 4096):
Using default value 4096
Last sector, +sectors or +size{K,M,G} (4096-209715199, default 209715199):
Using default value 209715199
Partition 5 of type Linux and of size 100 GiB is set
Command (m for help):
Expert command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
# 分区后查看磁盘清空
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 256M 0 part /boot
└─sda2 8:2 0 19.8G 0 part
└─centos-root 253:0 0 19.8G 0 lvm /
sdb 8:16 0 100G 0 disk
├─sdb1 8:17 0 1K 0 part
└─sdb5 8:21 0 100G 0 part
sr0 11:0 1 1024M 0 rom
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]#
二、格式化分区
# 查看支持格式(mkfs + 连续按2次TAB)
root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# mkfs
mkfs mkfs.btrfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.minix mkfs.xfs
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# fdisk -l
# 查看已分区磁盘
Disk /dev/sda: 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: 0x0009913a
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 526335 262144 83 Linux
/dev/sda2 526336 41943039 20708352 8e Linux LVM
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0xe957a076
Device Boot Start End Blocks Id System
/dev/sdb1 2048 209715199 104856576 5 Extended
/dev/sdb5 4096 209715199 104855552 83 Linux
Disk /dev/mapper/centos-root: 21.2 GB, 21202206720 bytes, 41410560 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@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# mkfs.ext4 /dev/sdb5
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26213888 blocks
1310694 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2174746624
800 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
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]#
此时格式分区后,通过mount /dec/sdb5 /data就已经挂载使用了,挂载前在根目下创建要挂载到的文件夹 。但是有个问题,此时一整块磁盘只能给一个目录使用。因此我们继续进行LVM磁盘管理。
三、LVM磁盘管理
在进行LVM磁盘管理前,我们可以重新将以上磁盘进行分区,通过 t 选择8eLVM类型。该操作非必须,这里就不在重复。
逻辑卷管理4个关键概念:
PE(Physical Extend) 物理扩展
PV(physical volume) 物料卷
VG (Volume Group) 卷组
LG (Logical volume)逻辑卷
【制作过程】
(1)挂载物理磁盘;
(2)将物理磁盘初始化为物理卷PV;
(3)将一个或多个物理卷PV转变为一个卷组VG。
(4)从VG中创建逻辑卷。
(5)根据实际需要将逻辑卷挂载使用。
(6)根据需要对磁盘or逻辑卷进行扩展和缩减。
现在演示,搭建一个LVM磁盘。
创建步骤:
# 创建物理卷
# 创建物理卷/dev/sdb5
pvcreate /dev/sdb5
# 创建物理卷组
# 创建物理卷组 vgc1
vgcreate vgc1 /dev/sdb5
# 创建逻辑卷
# -L指定创建的LV大小,-n是LV的名字
# 可以同组vgc创建多个lvc
# 在vgc1 卷组上创建名为lvc1,大小为1024M的逻辑卷
lvcreate -L 1024M -n lvc1 vgc1
主要操作过程记录
# 创建物理卷
1、pvcreate /dev/sdb5
2、vgcreate vgc1 /dev/sdb5
3、lvcreate -L 99G -n lvc1 vgc1
4、mkfs.ext4 /dev/vgc1/lvc1
5、mount /dev/vgc1/lvc1 /data
6、自动开机挂载:vim /etc/fstab
/dev/vgc1/lvc1 /data ext4 defaults 0 0
7、mount -a
8、查看 df -h
具体操作演示
# 首先查看系统当前情况
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <19.75 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 5055
Free PE 0
Allocated PE 5055
PV UUID SGVCW0-xlAA-9206-tE6M-LZdy-9opt-GeTX2v
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# pvcreate /dev/sdb5
WARNING: ext4 signature detected on /dev/sdb5 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/sdb5.
Physical volume "/dev/sdb5" successfully created.
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <19.75 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 5055
Free PE 0
Allocated PE 5055
PV UUID SGVCW0-xlAA-9206-tE6M-LZdy-9opt-GeTX2v
"/dev/sdb5" is a new physical volume of "<100.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb5
VG Name
PV Size <100.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 2Bm0Hf-WnIT-5cpM-lUtz-OPkI-D2e1-rijWFf
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# vgcreate vgc1 /dev/sdb5
Volume group "vgc1" successfully created
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <19.75 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 5055
Free PE 0
Allocated PE 5055
PV UUID SGVCW0-xlAA-9206-tE6M-LZdy-9opt-GeTX2v
--- Physical volume ---
PV Name /dev/sdb5
VG Name vgc1
PV Size <100.00 GiB / not usable 2.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 25599
Free PE 25599
Allocated PE 0
PV UUID 2Bm0Hf-WnIT-5cpM-lUtz-OPkI-D2e1-rijWFf
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# lvcreate -L 100G -n lvc1 vgc1
Volume group "vgc1" has insufficient free space (25599 extents): 25600 required.
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# lvcreate -L 99G -n lvc1 vgc1
Logical volume "lvc1" created.
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# lvdisplay /dev/vgc1/lvc1
--- Logical volume ---
LV Path /dev/vgc1/lvc1
LV Name lvc1
VG Name vgc1
LV UUID j0xioE-Kw9P-yG8F-Qnv6-FXJ1-csis-qSLBPc
LV Write Access read/write
LV Creation host, time vbs181-changsha-ctos7-dev-devops-nfs110, 2021-12-14 23:06:24 +0800
LV Status available
# open 0
LV Size 99.00 GiB
Current LE 25344
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 256M 0 part /boot
└─sda2 8:2 0 19.8G 0 part
└─centos-root 253:0 0 19.8G 0 lvm /
sdb 8:16 0 100G 0 disk
├─sdb1 8:17 0 1K 0 part
└─sdb5 8:21 0 100G 0 part
└─vgc1-lvc1 253:1 0 99G 0 lvm
sr0 11:0 1 1024M 0 rom
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]#
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# mkfs.ext4 /dev/vgc1/lvc1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6488064 inodes, 25952256 blocks
1297612 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2174746624
792 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
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# mkdir /data
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# mount /dev/vgc1/lvc1 /data
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]#
[root@vbs181-changsha-ctos7-dev-devops-nfs110 ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat Dec 11 14:27:49 2021
#
# 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/centos-root / xfs defaults 0 0
UUID=76554459-613b-41dc-83e7-d3d833920f11 /boot xfs defaults 0 0
/dev/vgc1/lvc1 /data ext4 defaults 0 0
[root@vbs181-changsha-ctos7-dev-devops-nfs110 data]# ll
total 16
drwx------ 2 root root 16384 Dec 14 23:11 lost+found
[root@vbs181-changsha-ctos7-dev-devops-nfs110 data]#
扩容
# 创建物理卷
1、pvcreate /dev/sdb5
2、vgextend centos /dev/sdb5
3、ll /dev/centos/root
4、lvextend -l +100%FREE /dev/centos/root
5、如果是xfs用 xfs_growfs /dev/centos/root
如果是ext4用 resize2fs /dev/centos/root
6、查看 df -h
至此,LVM方式管理磁盘搭建完成。
本文来自博客园,作者:刘文江,转载请注明原文链接:https://www.cnblogs.com/liuwenjiang/p/18907269

浙公网安备 33010602011771号