Linux命令(二十五) 磁盘管理命令(三) fdisk
一、fdisk命令介绍
fdisk 为Linux系统下的分区管理工具,类型windows下的 分区助手等工具软件。分过区装过操作系统的人都知道硬盘分区是必要和重要的。fdisk 的帮助如下所示:
[root@localhost hk]# fdisk /dev/sdb
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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xb7af7b64.
Command (m for help): h
h: unknown command
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)
以上参数中常用参数说明:
d 删除存在的硬盘分区
n 添加分区
p 查看分区信息
w 保存分区变更信息
二、硬盘详细分区和挂载过程完整演示
# 先查看分区信息
[root@localhost hk]# fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000c44fb
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM
# 这里 /dev/sdb 为未使用的分区
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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 /dev/mapper/cl-root: 39.7 GB, 39720058880 bytes, 77578240 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 /dev/mapper/cl-swap: 2147 MB, 2147483648 bytes, 4194304 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
# 开始进行分区
[root@localhost hk]# fdisk /dev/sdb
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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xca00a0b9.
# m查看帮助
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): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
# p 表示创建主分区,e 表示扩展分区
Select (default p): p
Partition number (1-4, default 1): 1 # 分区号选择1,也可以使用默认
First sector (2048-2097151, default 2048): # 起始节使用默认即可
Using default value 2048
# +400M 表示给分区分配400M 空间,默认分配磁盘所有剩余空间
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +400M
Partition 1 of type Linux and of size 400 MiB is set
# 使用 w 保存
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
#查看分区情况
[root@localhost hk]# fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000c44fb
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0xca00a0b9
# 这是我们刚刚分配的分区
Device Boot Start End Blocks Id System
/dev/sdb1 2048 821247 409600 83 Linux
Disk /dev/mapper/cl-root: 39.7 GB, 39720058880 bytes, 77578240 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 /dev/mapper/cl-swap: 2147 MB, 2147483648 bytes, 4194304 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
# 将新建的分区进行格式化,格式化为 ext3 格式
[root@localhost hk]# mkfs -t ext3 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
102400 inodes, 409600 blocks
20480 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
50 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
#创建挂载目录
[root@localhost hk]# mkdir /mnt/disk2
#将 /dev/sdb1 挂载到 /mnt/disk2
[root@localhost hk]# mount /dev/sdb1 /mnt/disk2/
# 查看分区已经正常挂载
[root@localhost hk]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 463M 0 463M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 13M 475M 3% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/mapper/cl-root 37G 3.6G 34G 10% /
/dev/sda1 1014M 246M 769M 25% /boot
tmpfs 98M 0 98M 0% /run/user/1000
/dev/sdb1 380M 2.3M 358M 1% /mnt/disk2 #这里可以看见分区已经正常挂载
# 编辑系统分区表 /etc/fstab 实现开机自动挂载
[root@localhost hk]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Nov 14 20:59:37 2017
#
# 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
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=03cf4f74-827e-40a1-a67f-2227be43dee7 /boot xfs defaults 0 0
/dev/mapper/cl-swap swap swap defaults 0 0
/dev/sdb1 /mnt/disk2 ext3 defaults 0 0 # 将分区信息加到 /etc/fstab 文件末尾,保存并退出
# 切换到分区挂载的目录中,创建文件测试分区是否可用
[root@localhost hk]# cd /mnt/disk2/
[root@localhost disk2]# touch test.txt
[root@localhost disk2]# ls
lost+found test.txt