参数 | 作用 |
m | 查看全部可用的参数 |
n | 添加新的分区 |
l | 列出所有可用的分区类型 |
t | 改变某个分区的类型 |
p | 查看分区表信息 |
w | 保存并退出 |
q | 不保存并退出 |
d | 删除某个分区信息 |
2. du命令:用于查看分区或目录所占用的磁盘容量的大小,格式“du -sh 目录名称”
df命令: 查看已挂载的磁盘空间使用情况,格式“df -h”
3. mkfs命令:用于格式化操作,格式为“mkfs.文件类型 分区”。如“mkfs.xfs /dev/sdb1”
4.交换(swap)分区:是一种通过在硬盘中预先划分一定的空间,然后把内存中暂时不用的数据临时存放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术,目的是为了解决真是物理内存不足的问题。
实验一:使用fdisk命令给/dev/sdb进行分区,并进行格式化以及挂载等操作。
[root@linuxprobe ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
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.
Created a new DOS disklabel with disk identifier 0x0e608123.
Command (m for help): n //输入N创建新的分区
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p //输入p来创建主分区
Partition number (1-4, default 1): 1 //主分区的编号范围是1~4,这里默认是1就可以了
First sector (2048-41943039, default 2048): //此处敲击回车键即可
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +2G //创建一个容量为2G的分区
Created a new partition 1 of type 'Linux' and of size 2 GiB.
Command (m for help): p //查看分区表信息
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x0e608123
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
Command (m for help): w //保存并退出
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
实验二:实验一中创建的分区/dev/sdb1因为还没有进行格式化,Linux系统无法在此分区写数据,因此需要使用mkfs命令格式化分区/dev/sdb1,并创建目录挂载此分区。
[root@linuxprobe ~]# mkfs.xfs /dev/sdb1 //格式化/dev/sdb1分区为xfs文件类型
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=524288, 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@linuxprobe ~]# mkdir /newFile //创建新目录
[root@linuxprobe ~]# mount /dev/sdb1 /newFile //挂载此分区
[root@linuxprobe ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 969M 0 969M 0% /dev
tmpfs 984M 0 984M 0% /dev/shm
tmpfs 984M 9.7M 974M 1% /run
tmpfs 984M 0 984M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 17G 3.9G 14G 23% /
/dev/sda1 1014M 152M 863M 15% /boot
tmpfs 197M 16K 197M 1% /run/user/42
tmpfs 197M 3.5M 194M 2% /run/user/0
/dev/sr0 6.7G 6.7G 0 100% /run/media/root/RHEL-8-0-0-BaseOS-x86_64
/dev/sdb1 2.0G 47M 2.0G 3% /newFile
实验三:交换分区的创建过程与挂载并使用存储设备的过程非常类似,同时在生产环境中,交换分区的大小一般为真是物理内存的1.5-2倍。这里我们尝试取出一块大小为5G的主分区作为交换分区资源:
step1:先创建一个容量为5GB的新分区
[root@linuxprobe ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): //敲击回车即可
First sector (4196352-41943039, default 4196352): //敲击回车即可
Last sector, +sectors or +size{K,M,G,T,P} (4196352-41943039, default 41943039): +5G
Created a new partition 2 of type 'Linux' and of size 5 GiB.
step2:修改硬盘的标识码,将其改为82(Linux swap)
Command (m for help): t //改变某个分区的类型
Partition number (1,2, default 2): 2 //选择分区2
Hex code (type L to list all codes): 82 //82为swap文件类型
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): p
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x0e608123
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
/dev/sdb2 4196352 14682111 10485760 5G 82 Linux swap / Solaris
Command (m for help): w
The partition table has been altered.
Syncing disks.
step3:使用mkswap命令对新设备进行交换分区格式化,格式“mkswap 设备名称”
[root@linuxprobe ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 5 GiB (5368705024 bytes)
no label, UUID=6f81d258-816b-4815-9db8-0356cb1daae2
step4:使用swapon激活新的交换分区设备,格式“swapon 设备名称”,同时可以使用free -m命令查看交换分区的大小变化。
[root@linuxprobe ~]# free -m //一卡式交换分区大小是2047MB
total used free shared buff/cache available
Mem: 1966 1227 91 14 646 540
Swap: 2047 0 2047
[root@linuxprobe ~]# swapon /dev/sdb2
[root@linuxprobe ~]# free -m //激活新设备后,交换分区大小变为7167MB了
total used free shared buff/cache available
Mem: 1966 1232 87 14 646 535
Swap: 7167 0 7167