Linux平台查看、格式化、(开机自动)挂载未使用的磁盘空间

1. 引言

下述命令与操作转自以下文章,在 Centos7 x64 上测试通过:
centos 硬盘中未使用空间进行逻辑分区 挂载使用
Centos7挂载新硬盘
CentOS7设置开机自动挂载硬盘

在此处收集整理并记录,方便日后查询,侵权立删!


2. fdisk 部分命令说明

命令:fdisk <disk>

[root@ecs-centos7 ~]# fdisk /dev/vda
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.


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):

说明:

命令动作
   a   设置可引导标记
   b   修改bsd的磁盘标签
   c   设置DOS操作系统兼容标记
   d   删除一个分区
   g   创建一个新的空白的GPT分区表
   G   创建一个新的空白的IRIX (SGI)分区表
   l   显示已知的分区类型,其中82为Linux swap分区,83为Linux分区
   m   显示帮助信息
   n   增加一个新的分区
   o   创建一个新的空白的DOS分区表
   p   打印磁盘分区表
   q   不保存修改并退出程序
   s   创建一个新的空白的Sun磁盘标签
   t   改变一个分区的系统号码(比如把Linux Swap分区改为Linux分区)
   u   改变显示/条目 单位
   v   验证磁盘分区表
   w   保存修改结果并退出
   x   额外功能(仅限专家)

3. 查看未使用的磁盘空间

命令:fdisk -l

[root@ecs-centos7 ~]# fdisk -l

Disk /dev/vda: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x000bd094

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    18616319     8258560   82  Linux swap / Solaris
/dev/vda3        18616320   209715199    95549440   83  Linux
[root@ecs-centos7 ~]#

解读:

  • 磁盘:/dev/vda
  • 容量:644.2GB(644245094400字节)
  • 扇区:512字节
  • 分区1:名称 - /dev/vda1,容量 - 1048576 / 1024 = 1024M = 1G
  • 分区2:名称 - /dev/vda2,容量 - 8258560 / 1024 = 8065M ≈ 7.9G
  • 分区3:名称 - /dev/vda3,容量 - 95549440 / 1024 = 93310M ≈ 91G

可见,磁盘/dev/vda仍有约(644 - 1 - 7.9 - 91)= 544.1G空间未使用


4. 为未使用的磁盘空间创建分区

命令:fdisk <disk>

[root@ecs-centos7 ~]# fdisk /dev/vda
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.


Command (m for help): 

下一步命令:n (创建新分区)

Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e):

下一步命令:p(选择分区类型为主分区,需要逻辑分区选择输入e)

Select (default e): p
Selected partition 4
First sector (209715200-1258291199, default 209715200):

下一步命令:回车(选择扇区起始地址,回车表示默认,需要自己指定输入<扇区起始地址>)

First sector (209715200-1258291199, default 209715200):
Using default value 209715200
Last sector, +sectors or +size{K,M,G} (209715200-1258291199, default 1258291199):

下一步命令:回车(选择扇区结束地址,回车表示默认,需要自己指定输入<扇区结束地址>)

Last sector, +sectors or +size{K,M,G} (209715200-1258291199, default 1258291199):
Using default value 1258291199
Partition 4 of type Linux and of size 500 GiB is set

分区大小 = (扇区结束地址 - 扇区起始地址) x 扇区大小
此处分区大小 = (1258291199 - 209715200) x 512 = 536870911488 (字节) = 500G

下一步命令:w (保存对磁盘的修改并退出)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@ecs-centos7 ~]#

下一步命令:partprobe <disk>(重新加载磁盘分区)

[root@ecs-centos7 ~]# partprobe /dev/vda
[root@ecs-centos7 ~]#

验证分区命令:fdisk -l <disk>

[root@ecs-centos7 ~]# fdisk -l /dev/vda

Disk /dev/vda: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x000bd094

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    18616319     8258560   82  Linux swap / Solaris
/dev/vda3        18616320   209715199    95549440   83  Linux
/dev/vda4       209715200  1258291199   524288000   83  Linux

/dev/vda4就是新创建的500G大小的分区

5. 格式化新的磁盘分区

命令:mkfs.xfs <disk> (将分区格式化位xfs文件系统格式,xfs是目前Centos7的主流文件系统)

[root@ecs-centos7 ~]# mkfs.xfs /dev/vda4
meta-data=/dev/vda4              isize=512    agcount=4, agsize=32768000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=131072000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=64000, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@ecs-centos7 ~]#

6. 挂载新的磁盘分区

命令:mount -t xfs <disk> <dirctory> (以xfs文件系统格式挂载磁盘分区到指定目录位置

[root@ecs-centos7 ~]# mount -t xfs /dev/vda4 /opt/
[root@ecs-centos7 ~]#

7. 开机自动挂载

命令:blkid <disk>(查看磁盘分区的UUID)

[root@ecs-centos7 ~]# blkid /dev/vda4
/dev/vda4: UUID="242681ca-49e3-42e8-9896-74f88d915654" TYPE="xfs"
[root@ecs-centos7 ~]#

命令:在文件 /etc/fstab 中新增一行 “UUID=242681ca-49e3-42e8-9896-74f88d915654 /opt xfs defaults 0 0”

#
# /etc/fstab
# Created by anaconda on Tue Jan 14 11:42:56 2020
#
# 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=63ea71f1-c4d0-481b-9f60-7a8361cb1d0b /                       xfs     defaults        0 0
UUID=d3c6e2a5-3259-40af-aa7f-206f5d504fc1 /boot                   xfs     defaults        0 0
UUID=4e19397a-3336-499d-a889-0803e453988c swap                    swap    defaults        0 0
UUID=242681ca-49e3-42e8-9896-74f88d915654 /opt                    xfs     defaults        0 0

重启后验证命令:df -h (查看已挂载的文件系统)

[root@ecs-centos7 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        7.9G     0  7.9G   0% /dev
tmpfs           7.9G     0  7.9G   0% /dev/shm
tmpfs           7.9G  117M  7.8G   2% /run
tmpfs           7.9G     0  7.9G   0% /sys/fs/cgroup
/dev/vda3        92G   19G   73G  21% /
/dev/vda1      1014M  177M  838M  18% /boot
tmpfs           1.6G     0  1.6G   0% /run/user/0
/dev/vda4       500G   33M  500G   1% /opt
[root@ecs-centos7 ~]#
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值