L49.linux命令每日一练 -- 第八章 Linux磁盘与文件系统管理命令 -- fdisk和partprobe

8.1 fdisk:磁盘分区工具

8.1.1 命令详解

【命令星级】 ★★★★★

【功能说明】

​ fdisk是Linux下常用的磁盘分区工具。受mbr分区表的限制,fdisk工具只能给小于2T的磁盘划分分区。如果使用fdisk对大于2TB的磁盘进行分区,虽然可以分区,但其仅识别2TB的空间,所以磁盘容量若超过2TB,就要使用parted分区工具(后面会讲)进行分区。

【语法格式】

fdisk [option] [device]
fdisk [选项] [设备名]

​ **说明:**在fdisk命令及后面的选项和设备名里,每个元素之间都至少要有一个空格。

【选项说明】

​ 表8-1针对该命令的参数选项进行了说明。

​ 表8-1 fdisk命令的参数选项及说明
在这里插入图片描述

8.1.2 使用范例

8.1.2.1 基础范例

​ **范例8-1:**显示磁盘分区列表(-l参数)例子。

[root@centos6 ~]# fdisk -l	#查看当前系统所有磁盘的分区信息。

Disk /dev/sda: 21.5 GB, 21474836480 bytes	#磁盘/dev/sda的大小。
255 heads, 63 sectors/track, 2610 cylinders	#255个虚拟磁头,63个扇区、磁道,2610个柱面。
Units = cylinders of 16065 * 512 = 8225280 bytes	#柱面大小8225280 bytes。
Sector size (logical/physical): 512 bytes / 512 bytes	#每个扇区的字节数。
I/O size (minimum/optimal): 512 bytes / 512 bytes	#每次读写的字节数。
Disk identifier: 0x000b962d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         131     1048576   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             131         392     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             392         523     1048576   83  Linux
Partition 3 does not end on cylinder boundary.
/dev/sda4             523        2611    16776192    5  Extended
/dev/sda5             523        2611    16775168   83  Linux

​ 上述信息每列功能说明具体如下:

Device:分区,这里有三个分区;
Boot:启动分区,用*表示的启动分区;
Start:表示开始的柱面;
End:表示结束的柱面;
Blocks:block块数量;
Id:分区类型Id;
System:分区类型。
[root@centos7 ~]# fdisk -l	#查看当前系统所有磁盘的分区信息。

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors	#磁盘/dev/sda大小21.5 GB,21474836480 bytes,41943040个扇区。
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: 0x000613a7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200     6293503     2097152   82  Linux swap / Solaris
/dev/sda3         6293504    41943039    17824768   83  Linux

8.1.2.2 技巧性范例

​ **范例8-2:**在虚拟机(VMware Workstation Pro)模拟磁盘分区实战。

​ 步骤1:先在虚拟机关机状态下添加一块1GB磁盘(如图8-1所示),若是在开机状态下添加,则需要重启系统。
在这里插入图片描述
​ 图8-1 添加一块1GB的硬盘

​ 步骤2:重启系统后查看添加的磁盘。

[root@centos7 ~]# fdisk -l	#显示分区信息。

Disk /dev/sda: 21.5 GB, 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
Disk label type: dos
Disk identifier: 0x000613a7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200     6293503     2097152   82  Linux swap / Solaris
/dev/sda3         6293504    41943039    17824768   83  Linux

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors	#刚刚新添加的磁盘名为/dev/sdb,是第二块盘。
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@centos7 ~]# fdisk -l /dev/sdb	#-l参数接设备码可以显示指定设备信息。

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

​ 步骤3:交互式分区实践。

[root@centos7 ~]# ls /dev/sd*	#查看分区当前设备的状态。
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb

[root@centos7 ~]# 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 0x1f0c4dd0.

Command (m for help): m		#m是帮助,需要人工输入m后回车,下同。
Command action
   a   toggle a bootable flag	#设置引导扇区。
   b   edit bsd disklabel	#编辑bsd卷标。
   c   toggle the dos compatibility flag	#设置dos兼容扇区。
   d   delete a partition	#删除一个分区。
   g   create a new empty GPT partition table	 # 创建一个空的GPT分区表。
   G   create an IRIX (SGI) partition table	  # 创建一个IRIX分区表。
   l   list known partition types	#查看分区类型对应编号列表。
   m   print this menu	#打印帮助菜单。
   n   add a new partition	#新建一个分区。
   o   create a new empty DOS partition table	#创建一个新的空DOS分区表。
   p   print the partition table	#打印分区表。
   q   quit without saving changes	#退出不保存更改。
   s   create a new empty Sun disklabel	#创建新的空Sun卷标。
   t   change a partition's system id	#更改分区系统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		#新建一个分区,需要人工输入n后回车。
Partition type:
   p   primary (0 primary, 0 extended, 4 free)	#创建主分区(编号1-4)。
   e   extended		#创建扩展分区。
Select (default p): p	#创建一个主分区,需要人工输入p后回车。
Partition number (1-4, default 1): 1	#设置分区编号为1,需要扔输入1后回车。
First sector (2048-2097151, default 2048): 	#设置起始扇区,直接敲回车键会使用默认值2048。
Using default value 2048
Last sector, +sectors or +size{
    
    K,M,G} (2048-2097151, default 2097151): +100M	#设置结束扇区(2097151)或分区大小(+100M),因为要划分出指定大小的分区,所以常用+100M这种方法,如果分区时使用fdisk -cu /dev/sdb,则这里就会使用扇区为单位来进行分区。
Partition 1 of type Linux and of size 100 MiB is set

Command (m for help): p		#打印分过的分区表。

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: 0x1f0c4dd0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   83  Linux

Command (m for help): n		#再新建一个分区。
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e	#创建一个扩展分区。
Partition number (2-4, default 2): 2	#设置分区编号为2.
First sector (206848-2097151, default 206848):	#按回车默认开始扇区号为206848。 
Using default value 206848
Last sector, +sectors or +size{
    
    K,M,G} (206848-2097151, default 2097151): 	#按回车,默认设置结束扇区号2097151。
Using default value 2097151
Partition 2 of type Extended and of size 923 MiB is set

Command (m for help): p		#打印分区表。

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: 0x1f0c4dd0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   83  Linux
/dev/sdb2          206848     2097151      945152    5  Extended

Command (m for help): n		#再新建一个分区。
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)	#分了扩展分区,这里自动变为逻辑分区。
Select (default p): p	#创建一个主分区。
Partition number (3,4, default 3): 3
No free sectors available	#不能再创建主分区,没有磁盘空间了。

Command (m for help): n		#再新建一个分区。
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l	#再新建一个逻辑分区。
Adding logical partition 5
First sector (208896-2097151, default 208896): #按回车开始扇区号208896.
Using default value 208896
Last sector, +sectors or +size{
    
    K,M,G} (208896-2097151, default 2097151): +400M	#设置分区大小为400M。
Partition 5 of type Linux and of size 400 MiB is set

Command (m for help): p		#打印分区表。

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: 0x1f0c4dd0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   83  Linux
/dev/sdb2          206848     2097151      945152    5  Extended
/dev/sdb5          208896     1028095      409600   83  Linux

Command (m for help): n		#再新建一个分区。
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l	#新建一个逻辑分区。
Adding logical partition 6
First sector (1030144-2097151, default 1030144): #开始扇区默认。
Using default value 1030144
Last sector, +sectors or +size{
    
    K,M,G} (1030144-2097151, default 2097151): 	#结束扇区默认。
Using default value 2097151
Partition 6 of type Linux and of size 521 MiB is set

Command (m for help): p		#打印分区表。

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: 0x1f0c4dd0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   83  Linux
/dev/sdb2          206848     2097151      945152    5  Extended
/dev/sdb5          208896     1028095      409600   83  Linux
/dev/sdb6         1030144     2097151      533504   83  Linux

Command (m for help): w		#将操作写入分区表生效并退出程序。
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos7 ~]# ls /dev/sd*	#查看分区后设备的状态。
/dev/sda   /dev/sda2  /dev/sdb   /dev/sdb2  /dev/sdb6
/dev/sda1  /dev/sda3  /dev/sdb1  /dev/sdb5
[root@centos7 ~]# partprobe /dev/sdb	#执行该命令通知内核分区表已更改,此步是不重启让分区表生效的命令。

​ 步骤4:格式化磁盘。

[root@centos7 ~]# mkfs.ext4 /dev/sdb1	#只有格式化后的磁盘才能挂载到系统中使用,后面将会详细讲解mdfs.ext4命令。
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
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@centos7 ~]# tune2fs -c 1 /dev/sdb1	#执行这个命令可以避免磁盘挂载自动检查磁盘,后面将会讲解tune2fs命令。
tune2fs 1.42.9 (28-Dec-2013)
Setting maximal mount count to 1

​ 步骤5:挂载磁盘分区。

[root@centos7 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        476M     0  476M   0% /dev
tmpfs           487M     0  487M   0% /dev/shm
tmpfs           487M  7.7M  479M   2% /run
tmpfs           487M     0  487M   0% /sys/fs/cgroup
/dev/sda3        17G  2.4G   15G  14% /
/dev/sda1      1014M  160M  855M  16% /boot
tmpfs            98M     0   98M   0% /run/user/0
[root@centos7 ~]# mount /dev/sdb1 /mnt
[root@centos7 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        476M     0  476M   0% /dev
tmpfs           487M     0  487M   0% /dev/shm
tmpfs           487M  7.7M  479M   2% /run
tmpfs           487M     0  487M   0% /sys/fs/cgroup
/dev/sda3        17G  2.4G   15G  14% /
/dev/sda1      1014M  160M  855M  16% /boot
tmpfs            98M     0   98M   0% /run/user/0
/dev/sdb1        93M  1.6M   85M   2% /mnt
[root@centos7 ~]# vim /etc/fstab	#最后一行加入,要开机自动挂载磁盘就要加入/etc/fstab或将上面的mount命令放入/etc/rc.local中。
/dev/sdb1                                  /mnt                   ex
t4    defaults        0 0
:wq

​ **提示:**有关fstab文件的知识,读者可以查看man fstab。

[root@centos7 ~]# vim /etc/rc.local		#或者编辑/etc/rc.local,最后一行加入,两种方法二选一。
mount /dev/sdb1 /mnt

​ 步骤6:其他事项。

​ 用交互指令d删除分区时要小心,看好分区的序号,如果删除了扩展分区,那么扩展分区之下的逻辑分区都会删除,所以操作时一定要小心。如果不小心操作错了,直接使用交互指令q不保存退出,这样先前的操作就会无效。如果输入w(保存指令)则会保存所有修改。

[root@centos7 ~]# 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.


Command (m for help): p	#打印分区表。

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: 0x1f0c4dd0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   83  Linux
/dev/sdb2          206848     2097151      945152    5  Extended
/dev/sdb5          208896     1028095      409600   83  Linux
/dev/sdb6         1030144     2097151      533504   83  Linux

Command (m for help): d	#扇区分区。
Partition number (1,2,5,6, default 6): 2	#输入要删除的分区编号,2是扩展分区。
Partition 2 is deleted

Command (m for help): p

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: 0x1f0c4dd0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   83  Linux
#可以看到逻辑分区都不见了。
Command (m for help): q	#不保存退出。

​ **范例8-3:**fdisk非交互式分区(批量分区案例)。

​ 表8-2展示的就是上面范例8-2步骤3中交互分区输入的字符串,其中enter为回车键。

​ 表8-2 范例8-2步骤3交互分区输入的字符串

在这里插入图片描述
​ 以下是实现非交互式分区的代码。

fdisk /dev/sdb<<EOF	#也可以将下面的内容写入文本文件,然后读文本执行。
n
p
1

+100M
n
e
2


n
1

+400M
n
l


p
w
EOF

​ 执行命令及结果如下:

[root@centos7 ~]# fdisk /dev/sdb<<EOF	#也可以将下面的内容写入文本。
> n
> p
> 1
> 
> +100M
> n
> e
> 2
> 
> 
> n
> l
> 
> +400M
> n
> l
> 
> 
> p
> w
> EOF	#输入EOF结束输入。

如果输入无误,回车后系统输出的结果如下:
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): Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): Partition number (1-4, default 1): First sector (2048-2097151, default 2048): Using default value 2048
Last sector, +sectors or +size{
    
    K,M,G} (2048-2097151, default 2097151): Partition 1 of type Linux and of size 100 MiB is set

Command (m for help): Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): Partition number (2-4, default 2): First sector (206848-2097151, default 206848): Using default value 206848
Last sector, +sectors or +size{
    
    K,M,G} (206848-2097151, default 2097151): Using default value 2097151
Partition 2 of type Extended and of size 923 MiB is set

Command (m for help): Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): Adding logical partition 5
First sector (208896-2097151, default 208896): Using default value 208896
Last sector, +sectors or +size{
    
    K,M,G} (208896-2097151, default 2097151): Partition 5 of type Linux and of size 400 MiB is set

Command (m for help): Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): Adding logical partition 6
First sector (1030144-2097151, default 1030144): Using default value 1030144
Last sector, +sectors or +size{
    
    K,M,G} (1030144-2097151, default 2097151): Using default value 2097151
Partition 6 of type Linux and of size 521 MiB is set

Command (m for help): 
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: 0x1f0c4dd0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   83  Linux
/dev/sdb2          206848     2097151      945152    5  Extended
/dev/sdb5          208896     1028095      409600   83  Linux
/dev/sdb6         1030144     2097151      533504   83  Linux

Command (m for help): 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.

​ 从结果看所有的分区已经分好了。

8.2 partprobe:更新内核的硬盘分区表信息

8.2.1 命令详解

【命令星级】 ★★★★☆

【功能说明】

​ partprobe命令用于在硬盘分区发生改变时,更新Linux内核中的磁盘分区表数据。有时在使用fdisk、part命令对硬盘进行分区后,会发现找不到新分区,此时需要重启系统才能使修改生效,但使用partprobe可以不重启系统就让修改的分区表生效。

【语法格式】

partprobe [option]
partprobe [选项]

​ **说明:**在partprobe命令及后面的选项里,每个元素之间都至少要有一个空格。

【选项说明】

【选项说明】

​ 表8-3针对该命令的参数选项进行了说明。

​ 表8-3 partprobe命令的参数选项及说明
在这里插入图片描述

8.2.2 使用范例

​ 范例8-4:更新分区表信息。

[root@centos7 ~]# partprobe /dev/sdb	#最好加上具体的磁盘,否则可能会报错,很多人这块直接执行最后导致报错,只好重启系统。

猜你喜欢

转载自blog.csdn.net/qq_25599925/article/details/125687386