如何使用fdisk命令新建、修改或删除磁盘的分区表信息?

学习目标:

  • 学习如何使用fdisk命令变动磁盘分区表信息
  • 学会如何对分区进行格式化操作
  • 学会如何使用及辨别du、df命令查看磁盘信息
  • 理解并学会使用交换分区

内容前导:

  1. fdisk命令用于新建、修改及删除磁盘的分区表信息,格式为“fdisk 磁盘名称”,如下表格是fdisk命令中的参数及作用。
    参数作用
    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

学习心得:

  • fdisk命令可以给一块硬盘设备分区,同时不同的参数有着不同且有效的作用
  • 创建好分区后,一定记得格式化,不然无法写入数据
  • 创建好交换分区后,记得分别使用“mkswap”以及“swapon”格式化和激活交换分区设备
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值