1、维护基本存储空间
1.1、分区的两种格式
1、MBR分区 MBR(Master Boot Record,主引导记录)是传统的分区机制,使用BIOS引导PC设备,寻址空间只有 32bit长。
- 分区空间最大支持2.2TB
- 支持的分区数量:4个主分区或者3个主分区1个扩展分区
为什么MBR最多只能有4个主分区?
因为分区表占据64个字节,其中每个分区的信息占用16个字节,分区表里面可以记录四个分区信 息描述。
-
MBR分区类型
-
主分区(primary partition)
一块硬盘最多4个主分区,主分区不可以再进行二次分区。
主分区可以直接建立文件系统,存放数据
可以用来引导、启动操作系统
-
扩展分区(extended partition)
一块硬盘最多一个扩展分区,加主分区最多4个
不能创建文件系统
可以划分逻辑分区
-
逻辑分区(logical partition)
可以创建文件系统,存放数据
逻辑分区的数量没有限制。
-
-
2、GPT分区
GPT(GUID Partition Table,全局唯一标识分区表)是一种比MBR分区更先进、更灵活的磁盘分区模式
- 在默认情况下,GPT最多可支持128个分区
- 支持大于2.2TB的总容量及大于2.2TB的分区,最大支持18EB(1EB=1024PB,1PB=1024TB,1TB=1024GB)
- GPT分区表自带备份
- 向后兼容MBR,GPT分区表上包含保护性的MBR区域
1.2、查看磁盘信息(块设备)
[root@kongd ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 6.6G 0 rom
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 19G 0 part
├─rhel-root 253:0 0 17G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
nvme0n2 259:3 0 2G 0 disk
[root@kongd ~]# lsscsi
不同接口的磁盘在linux中的设备文件名不同
1.3、使用fdisk管理分区
fdisk命令工具默认将磁盘划分为mbr格式的分区
命令: fdisk 设备名
fdisk命令以交互方式进行操作的,在菜单中选择相应功能键即可
指令 | 作用 | 指令 | 作用 |
---|---|---|---|
a | 调整磁盘的启动分区 | p | 显示当前磁盘的分区信息 |
d | 删除磁盘分区 | t | 更改分区类型 |
l | 显示所有支持的分区类型 | u | 切换所显示的分区大小单位 |
m | 查看所有指令的帮助信息 | n | 创建新分区 |
q | 不保存更改,退出fdisk命令 | w | 把修改写入磁盘分区表,然后退出fdisk命令 |
g | 新建一个空的GPT分区表 | o | 新建一个空的DOS分区表 |
[root@localhost ~]# fdisk /dev/sda #对sda进行分区
Command (m for help): # 进入fdisk交互界面进行分区
Command (m for help): m #输入m可查看帮助信息
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Command (m for help): n #输入n创建新分区
Partition type
p primary (0 primary, 0 extended, 4 free) #输入p创建主分区
e extended (container for logical partitions) #输入e创建扩展分区
Select (default p): p #输入p创建主分区
Partition number (1-4, default 1): 1 #选择主分区号,输入1表示第一个主分区
First sector (2048-10485759, default 2048): #直接按回车从当前第2048个扇区开始
分区
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759):
+300M #设置第一个分区为300M
Created a new partition 1 of type 'Linux' and of size 300 MiB.
#一个主分区创建完成,大小为300M
Command (m for help): p #输入p查看分区信息
Disk /dev/sda: 5 GiB, 5368709120 bytes, 10485760 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: 0x5bb0744f
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 616447 614400 300M 83 Linux
Command (m for help): p #输入p查看分区信息
Disk /dev/sda: 5 GiB, 5368709120 bytes, 10485760 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: 0x5bb0744f
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 616447 614400 300M 83 Linux
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 616447 614400 300M 83 Linux
/dev/sda2 616448 10485759 9869312 4.7G 0 Empty
#有两个分区,其中新添加的sda2有误,需要删除
Command (m for help): d #输入d删除分区
Partition number (1,2, default 2): 2 #输入2,选择需要删除的是第二个分区
Partition 2 has been deleted.
Command (m for help): p
Disk /dev/sda: 5 GiB, 5368709120 bytes, 10485760 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: 0x5bb0744f
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 616447 614400 300M 83 Linux
#查看现有分区,有误的sda2已删除
#创建一个扩展分区
Command (m for help): n #输入n创建新分区
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): e #输入e创建扩展分区
Partition number (2-4, default 2): 2 #选择分区号,输入2表示第二个分区
First sector (616448-10485759, default 616448): #直接按回车从当前第616448个扇区开始分区
Last sector, +sectors or +size{K,M,G,T,P} (616448-10485759, default 10485759):
+500M
#添加一个500M的分区
Created a new partition 2 of type 'Extended' and of size 500 MiB.
Command (m for help): p
Disk /dev/sda: 5 GiB, 5368709120 bytes, 10485760 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: 0x5bb0744f
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 616447 614400 300M 83 Linux
/dev/sda2 616448 1640447 1024000 500M 5 Extended
#输入p查看,新增一个500M的扩展分区
Command (m for help): w #所有分区设置完成后,输入w保存,即可退出fdisk交互界面
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
1.4、使用gdisk管理分区
gdisk命令工具默认将磁盘划分为GPT格式的分区
[root@localhost ~]# gdisk /dev/sda #进入gdisk交互界面
GPT fdisk (gdisk) version 1.0.3
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
Command (? for help): ? #输入?可查看帮助信息
b back up GPT data to a file
c change a partition's name
d delete a partition
i show detailed information on a partition
l list known partition types
n add a new partition
o create a new empty GUID partition table (GPT)
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu
#gdisk创建新分区(默认GPT格式)
Command (? for help): n #输入n创建新分区
Partition number (1-128, default 1): #输入分区编号,默认为1,因为是/dev/sda硬盘的第
一块分区
First sector (34-10485726, default = 2048) or {+-}size{KMGTP}: #输入扇区的开始位
置,选择默认即可,也可手动指定
Last sector (2048-10485726, default = 10485726) or {+-}size{KMGTP}: +300M
#输入扇区的结束位置,一般都是指定磁盘大小,这里输入+300M 表示新分区大小为300M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): #默认即可,分区完成后可以修
改
Changed type of partition to 'Linux filesystem'
Command (? for help): p #输入p查看分区情况
Disk /dev/sda: 10485760 sectors, 5.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 61FA16FB-FAD8-4873-A857-BD387FE0A592
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 10485726
Partitions will be aligned on 2048-sector boundaries
Total free space is 9871293 sectors (4.7 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 616447 300.0 MiB 8300 Linux filesystem
Command (? for help): w #输入w保存分区并退出gdisk交互界面
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sda.
The operation has completed successfully.
1.5、使用parted划分分区
parted的2种使用方式
- 交互式
[root@localhost ~]# parted /dev/sda #进入parted交互界面
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of
commands.
(parted) help #输入help查看帮助信息
align-check TYPE N check partition N for
TYPE(min|opt) alignment
help [COMMAND] print general help, or help on
COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition
table) # #创建新的磁盘标签 (分区表)
mkpart PART-TYPE [FS-TYPE] START END make a partition #创建一个分区
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table,
available devices, free space,
all found partitions, or a particular partition #打印分区表信息
quit exit program #退出程序
rescue START END rescue a lost partition near
START and END
resizepart NUMBER END resize partition NUMBER
rm NUMBER delete partition NUMBER #删除编号
为number的分区
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected
device
disk_toggle [FLAG] toggle the state of FLAG on
selected device
set NUMBER FLAG STATE change the FLAG on partition
NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on
partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and
copyright information of GNU
Parted
(parted) mklabel #创建一个分区表
New disk label type? yes
New disk label type? gpt #默认为msdos形式的分区,我们要正确分区大于2TB的磁盘,应
该使用gpt方式的分区表,输入gpt后回车)
(parted) mkpart #进行分区操作
Partition name? []? dp2 #输入分区名称
File system type? [ext2]? #文件系统 (类型:ext4,ext3,ext2,xfs,其他...... )
Start? 0 #开始位置 (0:设定当前分区的起始点为磁盘的第一个扇区;1G:设定当前分区的起始点为磁盘的1G处开始)
End? 10G #结束位置(-1:设定当前分区的结束点为磁盘的最后一个扇区;10G:设定当前分区的结束点为磁盘的10G处)
(parted) p #查看分区信息
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 10240MB 10240MB ext2 dp2
(parted) quit #退出parted交互界面
Information: You may need to update /etc/fstab.
- 非交互式
可将命令行写在脚本中,运行脚本实现一键创建;适用于远程批量管理多台主机的场景。
设置分区格式为gpt/mbr
[root@localhost ~]# parted /dev/sda mklabel gpt
[root@kongd ~]# parted /dev/sda mklabel msdos
分区命令用法:parted 设备 mkpart PART-TYPE [FS-TYPE] START END
PART-TYPE:分区类型,primary(主分区)、logical(逻辑分区)、extended(扩展分 区)
FS-TYPE:可选项,文件系统类型,ext4、ext3、xfs等等
START:设定磁盘分区起始点;可以为0或者numberMiB/GiB/TiB
END:设定磁盘分区结束点;可以为-1或者numberMiB/GiB/TiB
#创建1G大小的分区
[root@localhost ~]# parted /dev/sda mkpart primary 0 1G
#删除分区
[root@kongd ~]# parted /dev/sda rm 1
1.6、格式化
格式化的目的: 是为了形成文件系统,文件系统是操作系统用于明确存储设备或分区上的文件的方法和 数据结构;即在存储设备上组织文件的方法。
命令格式:mkfs|mkfs.xfs|mkfs.ext4 [选项] 分区的设备名
选项:
-t 文件系统类型——当命令名为mkfs时,指定要创建的文件系统的类型(如:xfs、ext4、vfat等)。
当命令名为mkfs.xfs、mkfs.ext4等时,不需要该选项。
-c——建立文件系统前先检查坏块。
-V——输出建立文件系统的详细信息。
注意:如果已有其他文件系统创建在此分区,必须在 mkfs.xfs 命令中加上选项 -f 强行进行格式化
[root@localhost ~]# mkfs.xfs /dev/sda1
[root@localhost ~]# mkfs.ext4 /dev/sda2
#查看格式化后的磁盘分区信息
[root@localhost ~]# blkid
/dev/sda1: UUID="350148b0-7975-4114-b91d-35ac1b211fd3" BLOCK_SIZE="512"
TYPE="xfs" PARTLABEL="primary" PARTUUID="e272d830-5778-48cd-aba6-230271397716"
/dev/sda2: UUID="acf864ef-1778-4bc4-8e5d-260adfe38b69" BLOCK_SIZE="4096"
TYPE="ext4" PARTUUID="71ca21ff-cd76-a946-9dc6-4a8dd1022e99"
1.7、挂载
挂载就是将一个分区或者设备挂载至挂载点目录。
挂载命令: mount [-t 文件系统类型] 设备名 挂载点目录
#创建挂载点目录
[root@localhost ~]# mkdir /guazai/dir1 -p
#查看格式化后的磁盘分区信息
[root@localhost ~]# blkid
/dev/sda1: UUID="350148b0-7975-4114-b91d-35ac1b211fd3" BLOCK_SIZE="512" TYPE="xfs" PARTLABEL="primary" PARTUUID="e272d830-5778-48cd-aba6-230271397716" /dev/sda2: UUID="acf864ef-1778-4bc4-8e5d-260adfe38b69" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="71ca21ff-cd76-a946-9dc6-4a8dd1022e99"
[root@localhost ~]# mount /dev/sda1 /guazai/dir1/ 或者[root@localhost ~]# mount -U 350148b0-7975-4114-b91d-35ac1b211fd3 /guazai/dir1/
#查看挂载信息
[root@localhost ~]# mount | grep /dev/sda
/dev/sda1 on /guazai/dir1 type xfs
(rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
- 卸载分区,要移除USB磁盘、U盘、光盘和硬盘时,需要先卸载。
命令: umount 挂载点目录或存储设备名
[root@localhost ~]# umount /dev/sda1 或者 [root@localhost ~]# umount
/guazai/dir1/
- 设置系统启动时的自动挂载
手动挂载的分区会在系统重启后失效,若用户需要永久挂载分区,则需要通过编辑 /etc/fstab 文 件来实现。当系统启动的时候,系统会自动地从这个文件读取信息,并且会自动将此文件中指定的 文件系统挂载到指定的目录。
#查看格式化后的磁盘分区信息
[root@localhost ~]# blkid
/dev/sda1: UUID="350148b0-7975-4114-b91d-35ac1b211fd3" BLOCK_SIZE="512"
TYPE="xfs" PARTLABEL="primary" PARTUUID="e272d830-5778-48cd-aba6-230271397716"
/dev/sda2: UUID="acf864ef-1778-4bc4-8e5d-260adfe38b69" BLOCK_SIZE="4096"
TYPE="ext4" PARTUUID="71ca21ff-cd76-a946-9dc6-4a8dd1022e99"
[root@kongd ~]# tail -3 /etc/fstab
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=806badd7-5a3b-4549-bcc1-3a43b07b6bac /boot xfs
defaults 0 0
/dev/mapper/rhel-swap swap swap defaults 0 0
用于挂载信息的指定填写格式中,各字段所表示的意义
字段 | 意义 |
---|---|
设备文件 | 一般为设备的路径+设备名称,也可以写唯一识别码(UUID,Universally Unique Identifier) |
挂载目录 | 指定要挂载到的目录,需在挂载前创建好 |
格式类型 | 指定文件系统的格式,比如Ext3、Ext4、XFS、SWAP、iso9660(此为光盘设备)等 |
权限选项 | 若设置为defaults,则默认权限为:rw, suid, dev, exec, auto, nouser, async |
是否备份 | 若为1则开机后使用dump进行磁盘备份,为0则不备份 |
是否自检 | 若为1则开机后自动进行磁盘自检,为0则不自检 |
1.8、查看磁盘空间使用量:df,du
- df:列出文件系统的磁盘空间占用情况
df,disk free,通过文件系统来快速获取空间大小的信息,当我们删除一个文件的时候,这 个文件不是马上就在文件系统当中消失了,而是暂时消失了,当所有程序都不用时,才会根 据OS的规则释放掉已经删除的文件, df记录的是通过文件系统获取到的文件的大小,他比 du强的地方就是能够看到已经删除的文件,而且计算大小的时候,把这一部分的空间也加上 了,更精确了。
用法:df [-ahikHTm] [目录或文件名]
-a:列出所有的文件系统,包括系统特有的/proc等文件系统
-k:以KB的容量显示各文件系统
-m:以MB的容量显示各文件系统
-h:以人们较易阅读的GB,MB,KB等格式自行显示
-H:以M=1000K替代M=1024K的进位方式
-T:连同该分区的文件系统名称(例如ext3)也列出
-i:不用硬盘容量,而以inode的数量来显示
由于df主要读取的数据几乎都是针对整个文件系统,因此读取的范围主要是在Super block内的信息, 所以这个命令显示结果的速度非常快速。
- du:显示磁盘空间使用量(统计目录或文件所占磁盘空间大小),在默认情况下,文件大小的单位 是KB。
du,disk usage,是通过搜索文件来计算每个文件的大小然后累加,du能看到的文件只是一 些当前存在的,没有被删除的。他计算的大小就是当前他认为存在的所有文件大小的累加 和。
当文件系统也确定删除了该文件后,这时候du与df就一致了。
du [-ahskm] 文件或目录名称
-a : 列出所有的文件与目录容量,因为默认仅统计目录下面的文件量而已;
-h : 以人们较易读的容量格式(G/M)显示;
-s : 列出总量,而不列出每个个别的目录占用了容量;
-S : 不包括子目录下的总计,与-s有点差别;
-k : 以KB列出容量显示;
-m : 以MB列出容量显示。