一、对磁盘分区
1、使用fdisk分区(此处以sdb为例)
1、查看所要添加的硬盘信息
fdisk -l
Disk /dev/sda: 161.1 GB, 161061273600 bytes, 314572800 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: 0x000c60f2
Device Boot Start End Blocks Id System
/dev/sda1 2048 4095 1024 83 Linux
/dev/sda2 * 4096 2052095 1024000 83 Linux
/dev/sda3 2052096 106909695 52428800 83 Linux
/dev/sda4 106909696 314572799 103831552 5 Extended
/dev/sda5 106911744 148854783 20971520 83 Linux
/dev/sda6 148856832 190799871 20971520 83 Linux
/dev/sda7 190801920 224356351 16777216 82 Linux swap / Sol
/dev/sda8 224358400 314572799 45107200 8e Linux LVM
Disk /dev/sdb: 1099.5 GB, 1099511627776 bytes, 2147483648 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-sec_data: 46.2 GB, 46187675648 bytes, 90210304 s
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
2、使用fdisk分区
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 0xb8ddcc19.
Command (m for help): p
Disk /dev/sdb: 1099.5 GB, 1099511627776 bytes, 2147483648 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: 0xb8ddcc19
Device Boot Start End Blocks Id System
Command (m for help): c^H^Hm
DOS Compatibility flag is set (DEPRECATED!)
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):
First sector (63-2147483647, default 63):
Using default value 63
Last sector, +sectors or +size{K,M,G} (63-2147483647, default 2147483647
Using default value 2147483647
Partition 1 of type Linux and of size 1024 GiB is set
Command (m for help): p
Disk /dev/sdb: 1099.5 GB, 1099511627776 bytes, 2147483648 sectors
255 heads, 63 sectors/track, 133674 cylinders
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: 0xb8ddcc19
Device Boot Start End Blocks Id System
/dev/sdb1 63 2147483647 1073741792+ 83 Linux
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes):
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: 1099.5 GB, 1099511627776 bytes, 2147483648 sectors
255 heads, 63 sectors/track, 133674 cylinders
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: 0xb8ddcc19
Device Boot Start End Blocks Id System
/dev/sdb1 63 2147483647 1073741792+ 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
其中相关参数解说:
p:打印已有分区信息
m:帮助,可列出相关参数
n:新建分区
t:修改分区的分区类型
w:保存更改
2、使用parted分区
用法和发disk类似,不同于fdisk的是一般超过2TB的硬盘会采用parted分区,格式会用gpt的分区格式
parted /dev/sdb
GNU Parted 3.3
使用 /dev/sdb
欢迎使用 GNU Parted!输入 'help' 来查看命令列表。
(parted)
#如果忘记有那些可用的命令按两次Tab键可以显示所以可用的指令,也支持Tab命令补齐,和上下查找历史记。可用命令如下:
align-check disk_toggle mklabel mktable print rescue resizepart select toggle version
disk_set help mkpart name quit resize rm set unit
(parted) help mklable #查看mklable命令的用法及支持的分区表
mklabel,mktable LABEL-TYPE create a new disklabel (partition table) #用法,用mktable也是一样的作用
LABEL-TYPE is one of: atari, aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, loop
(parted) mklable gpt #创建gpt分区表,如果以存在分区表也可以通过这里命令更改
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes # 警告用户磁盘上的数据将会被销毁,询问是否继续,我们这里是新的磁盘,输入yes后回车
(parted) print #查看分区表的信息
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt #分区表以是gpt格式
Disk Flags:
Number Start End Size File system Name Flags
(parted) mkpart #输入mkpart新建分区,如果输入mkpart指令不带任何参数,parted会一步步提示用户输入相关信息最终完成创建,如下所示:
Partition name? []? #分区的名字,这里可以直接回车不给分区命名,也可以输入用来标记的名字
File system type? [ext2]? #分区被格式化的文件系统,可以按两次Tab键查看支持的文件系统
affs0 affs6 amufs3 btrfs freebsd-ufs linux-swap ntfs
affs1 affs7 amufs4 ext2 hfs linux-swap(new) reiserfs
affs2 amufs amufs5 ext3 hfs+ linux-swap(old) sun-ufs
affs3 amufs0 apfs1 ext4 hfsx linux-swap(v0) swsusp
affs4 amufs1 apfs2 fat16 hp-ufs linux-swap(v1) xfs
affs5 amufs2 asfs fat32 jfs nilfs2 zfs
File system type? [ext2]? ext4 #输入被格式化的文件系统为ext4,这里必须要输入文件系统
Start? 0% #开始位置,这里要输入0%或2048s或1M都可以但是不要输入别的,下面有说明。
End? 1G #结束位置,默认单位为compact,我们想要用其他单位必须在数字后面加上单位
(parted) mkpart pname ext4 1G 2G
#如果想一步就把分区创建好可以像左边一样,pname表示分区名这样分区这里必须有,后面3项目分别是文件系统,开始位置,结束位置
(parted) mkpart pname 2G 100% #如果不想创建文件系统这里可以不加这个选项,后面两项分别是开始位置,和结束位置,100%表示使用全部剩余空间
(parted) p #输入print查看分区情况,如果指令是唯一的直接输入指令的首字母也可以。
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5369MB #磁盘的总容量
Sector size (logical/physical): 512B/512B #扇区的大小
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 1000MB 999MB ext4 #通过一步步创建的第一个分区
2 1000MB 2000MB 999MB ext4 pname #通过一步就创建的分区
3 2000MB 5368MB 3368MB pname #创建不带文件系统的分区
参数解释:
align-check 检查分区N的类型(min|opt)是否对齐
help 打印通用求助信息,或关于[指令]的帮助信息
mklabel 创建新的磁盘标签 (分区表)
mkpart 创建一个分区
name 给指定的分区命名
print 打印分区表,或者分区
quit 退出程序
rescue 修复丢失的分区
resizepart 调整分区大小
rm 删除分区
select 选择要编辑的设备,默认只对指定的设备操作,这里可以改变指定的设备
disk_set 更改选定设备上的标志
disk_toggle 切换选定设备上的标志状态
set 更改分区的标记
toggle 设置或取消分区的标记
unit 设置默认的单位
version 显示版本信息
主要常用命令:
1、 mklable gpt #修改硬盘的硬盘格式为gpt格式
2、mkpart pname 2G 100% #创建分区
二、创建lvm
1、创建物理卷(PV)
#这里比较简单,只是为了后面创建卷组(VG),基本语法格式为:
pvcreate 上面的分区
例如:
pvcreate /dev/sdb1
2、创建卷组(VG)
1、语法格式
扩容卷组: vgextend VGName devicename(/dev/sdb1)
举例: vgextend centos /dev/sdb1
创建卷组: vgcreate VGName devicename(/dev/sdb1)
举例:vgcreate centos /dev/sdb1
三、实现lvm扩容
最后一步比较简单,但有一个小细节需要注意一下
1、扩容的语法
lvextend [选项][容量][目标逻辑卷]
举例:
lvextend -L +1000G /dev/mapper/centos-sec_data
Size of logical volume centos/sec_data changed from <43.02 GiB (11012 ts) to <1.02 TiB (267012 extents).
Logical volume centos/sec_data successfully resized.
2、刷新逻辑分区的容量
##这个是针对分区格式为EXT*(EXT2、EXT3、EXT4)
resize2fs /dev/mapper/centos-sec_data
##这个是针对分区格式为xfs
xfs_growfs /dev/mapper/centos-sec_data
我这里的分区格式为xfs,刷新如下:
xfs_growfs /dev/mapper/centos-sec_data
meta-data=/dev/mapper/centos-sec_data isize=512 agcount=97, agsize=2819072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=273420288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=5506, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
或许在这里有些人会问,那我怎么知道我的分区格式是什么?
我给大家几种查看的方法:
1、查看/etc/fstab文件
cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Jul 29 10:20:52 2022
#
# 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
#
UUID=1b5b6989-03ee-4455-9602-4f1fa6adacc0 / xfs defaults 0 0
UUID=904d8a1b-36da-4a83-93d8-02cfda2af54e /boot xfs defaults 0 0
/dev/mapper/centos-sec_data /data xfs defaults 0 0
UUID=85cb7331-ec30-4e68-9514-ddff3d0eaabf /home xfs defaults 0 0
UUID=4a4fb18a-2d6b-4d10-ad5e-d9a8ba06ea97 /var xfs defaults 0 0
UUID=a0893193-1514-48a5-9946-c8d4c1652429 swap swap defaults 0 0
可以看得到这里的分区格式为xfs
/dev/mapper/centos-sec_data /data xfs
2、使用df -Th命令查看
df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 32G 0 32G 0% /dev
tmpfs tmpfs 32G 0 32G 0% /dev/shm
tmpfs tmpfs 32G 18M 32G 1% /run
tmpfs tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/sda5 xfs 20G 11G 9.6G 53% /
/dev/sda6 xfs 20G 7.7G 13G 39% /home
/dev/sda3 xfs 50G 1.8G 49G 4% /var
/dev/sda2 xfs 997M 149M 849M 15% /boot
/dev/mapper/centos-sec_data xfs 1.1T 43G 1001G 5% /data
tmpfs tmpfs 6.3G 0 6.3G 0% /run/user/0
在倒数第二行也可以看到是xfs
/dev/mapper/centos-sec_data xfs 1.1T 43G 1001G 5% /data