Linux新磁盘分区格式化及挂载-fdisk命令

Posted tssc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux新磁盘分区格式化及挂载-fdisk命令相关的知识,希望对你有一定的参考价值。

本文主解决linux系统的磁盘分区格式化及挂载问题
注意:
分区操作针对磁盘vda,sda等
vda1,sda1等是分区的名称

1.检查当前磁盘分区状态

1.1.查看分区挂载情况

df -h

-------------------------------------------------------------------------------

[email protected]:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 7.9G 0 7.9G 0% /dev
tmpfs 1.6G 3.2M 1.6G 1% /run
/dev/vda1 50G 5.7G 41G 13% /
tmpfs 7.9G 0 7.9G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 7.9G 0 7.9G 0% /sys/fs/cgroup
tmpfs 1.6G 0 1.6G 0% /run/user/0

1.2.查看服务器挂载的磁盘

fdisk -l
[email protected]:~# fdisk -l
Disk /dev/vda: 50 GiB, 53687091200 bytes, 104857600 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: 0xd6804155

Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 104855551 104853504 50G 83 Linux


Disk /dev/vdb: 50 GiB, 53687091200 bytes, 104857600 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

2.对磁盘进行分区格式化操作

2.1.对vdb磁盘进行分区

fdisk /dev/vdb

以下是常用的分区操作:

m打印菜单
n新建分区
d删除分区
p打印分区表
w写入分区表保存
q退出

p选择主分区
e选择扩展分区
1选择分区号
选择初始位置,默认为1
选择结束为止,默认为磁盘结尾

[email protected]:~# fdisk /dev/vdb

Welcome to fdisk (util-linux 2.27.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 0x63c3e6e0.

Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-104857599, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-104857599, default 104857599):

Created a new partition 1 of type Linux and of size 50 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

2.2.重新查看磁盘分区表

[email protected]:~# fdisk -l
Disk /dev/vda: 50 GiB, 53687091200 bytes, 104857600 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: 0xd6804155

Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 104855551 104853504 50G 83 Linux


Disk /dev/vdb: 50 GiB, 53687091200 bytes, 104857600 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: 0x63c3e6e0

Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 104857599 104855552 50G 83 Linux

2.3..重新载入磁盘分区信息

partprobe /dev/vdb1

使用fdisk工具只是将分区信息写到磁盘,
在格式化分区值前需要使用partprobe让kernel重新读取分区信息,如果不成功则需要重启系统
无返回就是读取成功

2.4.格式化磁盘分区

mkfs.ext4 /dev/vdb1

 

[email protected]:~# mkfs.ext4 /dev/vdb1
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 13106944 4k blocks and 3276800 inodes
Filesystem UUID: 047fb8b0-e6ed-42db-bf5c-2a6d388fc34d
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
4096000, 7962624, 11239424

Allocating group tables: done 
Writing inode tables: done 
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 
This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

2.5.格式化完成后续优化配置

tune2fs -c -1 /dev/vdb1 
[email protected]:~# tune2fs -c -1 /dev/vdb1 
tune2fs 1.42.13 (17-May-2015)
Setting maximal mount count to -1

3.挂载分区

3.1.挂载分区到指定目录

mkdir -p /data
mount /dev/vdb1 /data

3.2.配置开机自动挂载分区

vim /etc/fstab 

     增加以下内容

# made by zhaoshuai in 20180327
/dev/vdb1 /data ext4 defaults 0 0

完毕,呵呵呵呵 















以上是关于Linux新磁盘分区格式化及挂载-fdisk命令的主要内容,如果未能解决你的问题,请参考以下文章

linux系统磁盘分区及把磁盘挂载到系统中的方法

Linux下添加新硬盘,分区及挂载

Linux下添加新硬盘,分区及挂载

centOS 8 磁盘分区及挂载

Linux磁盘分区

LINUX04_分区的概述设备挂载情况lsblk磁盘分区fdisk格式化硬盘挂载mount