partprobe
不重启的情况下重读分区
补充说明 partprobe命令 用于重读分区表,当出现删除文件后,出现仍然占用空间。可以partprobe在不重启的情况下重读分区。
语法
partprobe(选项)(参数)
选项
-d:不更新内核;
-s:显示摘要和分区;
-h:显示帮助信息;
-v:显示版本信息。
参数
设备:指定需要确认分区表改变的硬盘对应的设备文件。
实例
使用partprobe不重启系统添加新的磁盘分区,主机自带硬盘超过300GB,目前只划分使用了3个主分区,不到70GB,如下:
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 29G 3.7G 24G 14% /
/dev/sda2 29G 22G 5.2G 81% /oracle
tmpfs 2.0G 0 2.0G 0% /dev/shm
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
8 0 311427072 sda
8 1 30716248 sda1
8 2 30716280 sda2
8 3 8193150 sda3
8 16 976896 sdb
8 32 976896 sdc
…省略其他
现在需要给系统添加1个100GB的空间存放数据文件,而又不影响现有系统上业务的运行,使用fdisk结合partprobe命令不重启系统添加一块新的磁盘分区。操作步骤如下:
第1步 添加新的磁盘分区 :
[root@localhost ~]# fdisk /dev/sda
The number of cylinders for this disk is set to 38770.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of lilo)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
command (m for help): p
Disk /dev/sda: 318.9 GB, 318901321728 bytes
255 heads, 63 sectors/track, 38770 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks id System
/dev/sda1 * 1 3824 30716248+ 83 Linux
/dev/sda2 3825 7648 30716280 83 Linux
/dev/sda3 7649 8668 8193150 82 Linux swap / Solaris
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Selected partition 4
First cylinder (8669-38770, default 8669):
Using default value 8669
last cylinder or +size or +sizeM or +sizeK (8669-38770, default 38770): +100G
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.
Syncing disks.
第2步 使用工具partprobe让kernel读取分区信息:
[root@localhost ~]# partprobe
使用fdisk工具只是将分区信息写到磁盘,如果需要mkfs磁盘分区则需要重启系统,而使用partprobe则可以使kernel重新读取分区信息,从而避免重启系统。
第3步 格式化文件系统:
[root@localhost ~]# mkfs.ext3 /dev/sda4
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
12222464 inodes, 24416791 blocks
1220839 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
746 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632,
2654208, 4096000, 7962624, 11239424, 20480000, 23887872
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information:
done
This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#
第4步 mount新的分区/dev/sda4:
[root@localhost ~]# e2label /dev/sda4 /data
[root@localhost ~]# mkdir /data
[root@localhost ~]# mount /dev/sda4 /data
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 29753556 3810844 24406900 14% /
/dev/sda2 29753588 11304616 16913160 41% /oracle
tmpfs 2023936 0 2023936 0% /dev/shm
/dev/sda4 96132968 192312 91057300 1% /data
使用partprobe可以不用重启系统即可配合fdisk工具创建新的分区。
后续只做备注用:
磁盘挂载
https://wangchujiang.com/linux-command/c/partprobe.html
#查看服务器上未挂载的磁盘(磁盘有普通磁盘,高效磁盘,SSD磁盘);命令如下:fdisk -l
#磁盘分区(分区有主分区,扩展 分区,逻辑分区);
命令如下:fdisk /dev/vdb n->p->1->回车->回车->w ->partprobe /dev/vdb
n:添加一个分区
P:主分区
两个回车指是开始和结束的磁盘扇区大小;
w:写入磁盘
此时磁盘虽然已经分区,但是还没有文件系统,磁盘依然不能用;
#格式化磁盘并写入文件系统:命令如下:mkfs.ext4 /dev/vdb1
// 这里可以用ext3,也可以用ext4;
#挂载新磁盘到操作系统的某个节点上,命令如下:mkdir /mnt/disk mount /dev/vdb1 /mnt/disk1
#设置开机启动自动挂载;新创建的分区不能开机自动挂载,每次重启机器都要手动挂载。设置开机自动挂载需要修改/etc/fstab文件 ;命令如下:
vim /etc/fstab
//打开后,在最后一行加入以下代码:
/dev/vdb1 /huichengff ext4 defaults 0 1 //如果上面用的是ext3,这里也要用ext3;
#取消挂载:
unmount /mnt/disk1 -l
建立主分区
首先,我们要建立一个主分区,看看过程是什么样子的。命令如下:
[root@localhost ~]# fdisk /dev/sdb
…省略部分输出…
Command (m for help): p
#显示当前硬盘的分区列表
Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 *512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb4b0720c
Device Boot Start End Blocks id System
#目前一个分区都没有
Command (m for help): n
#那么我们新建一个分区
Command action
#指定分区类型
e extended
#扩展分区
p primary partition (1-4)
#主分区
p
#这里选择p,建立一个主分区
Partition number (1-4): 1
#选择分区号,范围为1~4,这里选择1
First cylinder (1 -2610, default 1):
#分区的起始柱面,默认从1开始。因为要从硬盘头开始分区,所以直接回车
Using default value 1
#提示使用的是默认值1
Last cylinder, +cylinders or +size{K, M, G}(1-2610, default 2610): +5G
#指定硬盘大小。可以按照柱面指定(1-2610)。我们对柱面不熟悉,那么可以使用size{K, M, G}的方式指定硬盘大小。这里指定+5G,建立一个5GB大小的分区
Command (m for help):
#主分区就建立了,又回到了fdisk交互界面的提示符
Command (m for help): p
#查询一下新建立的分区
Disk /dev/sdb: 21.5GB, 21474836480 bytes
255 heads,63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes 1512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb4b0720c
Device Boot Start End Blocks id System
/dev/sdb1 1 654 5253223+ 83 Linux
#dev/sdb1已经建立了吧
可以看到,建立主分区的过程是:"fdisk 硬盘名 -> n(新建)->p(建立主分区) -> 1(指定分区号) -> 回车(默认从 1 柱面开始建立分区)-> +5G(指定分区大小)"。当然,我们的分区还没有格式化和挂载,所以还不能使用```
建立扩展分区
```c
首先,我们要建立一个主分区,看看过程是什么样子的。命令如下:
[root@localhost ~]# fdisk /dev/sdb
…省略部分输出…
Command (m for help): p
#显示当前硬盘的分区列表
Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 *512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb4b0720c
Device Boot Start End Blocks id System
#目前一个分区都没有
Command (m for help): n
#那么我们新建一个分区
Command action
#指定分区类型
e extended
#扩展分区
p primary partition (1-4)
#主分区
p
#这里选择p,建立一个主分区
Partition number (1-4): 1
#选择分区号,范围为1~4,这里选择1
First cylinder (1 -2610, default 1):
#分区的起始柱面,默认从1开始。因为要从硬盘头开始分区,所以直接回车
Using default value 1
#提示使用的是默认值1
Last cylinder, +cylinders or +size{K, M, G}(1-2610, default 2610): +5G
#指定硬盘大小。可以按照柱面指定(1-2610)。我们对柱面不熟悉,那么可以使用size{K, M, G}的方式指定硬盘大小。这里指定+5G,建立一个5GB大小的分区
Command (m for help):
#主分区就建立了,又回到了fdisk交互界面的提示符
Command (m for help): p
#查询一下新建立的分区
Disk /dev/sdb: 21.5GB, 21474836480 bytes
255 heads,63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes 1512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb4b0720c
Device Boot Start End Blocks id System
/dev/sdb1 1 654 5253223+ 83 Linux
#dev/sdb1已经建立了吧
可以看到,建立主分区的过程是:"fdisk 硬盘名 -> n(新建)->p(建立主分区) -> 1(指定分区号) -> 回车(默认从 1 柱面开始建立分区)-> +5G(指定分区大小)"。当然,我们的分区还没有格式化和挂载,所以还不能使用。
fdisk命令创建扩展分区
接下来给大家展示如何建立一个扩展分区。还记得吗?主分区和扩展分区加起来最多只能建立 4 个,而扩展分区最多只能建立 1 个。扩展分区的建立命令如下:
[root@localhost ~]# fdisk /dev/sdb
…省略部分输出…
Command (m for help): n
#新建立分区
Command action
e extended
p primary partition (1-4)
e
#这次建立扩展分区
Partition number (1-4): 2
#给扩展分区指定分区号2
First cylinder (655-2610, default 655):
#扩展分区的起始柱面。上节建立的主分区1已经占用了1~654个柱面,所以我们从655开始建立,注意:如果没有特殊要求,则不要跳开柱面建立分区,应该紧挨着建立分区
Using default value 655
提示使用的是默认值655
Last cylinder, +cylinders or +size{K, M, G} (655-2610, default 2610):
#这里把整块硬盘的剩余空间都建立为扩展分区
Using default value 2610
#提示使用的是默认值2610
这里把 /dev/sdb 硬盘的所有剩余空间都建立为扩展分区,也就是建立一个主分区,剩余空间都建立成扩展分区。要知道,扩展分区是不能被格式化和直接使用的,所以还要在扩展分区内部再建立逻辑分区。
建立逻辑分区
最后,我们来看看逻辑分区的建立过程,命令如下:
[root@localhost ~]# fdisk /dev/sdb
…省略部分输出…
Command (m for help): n
#建立新分区
Command action
l logical (5 or over)
#由于在前面章节中,扩展分区已经建立,所以这里变成了l(logic)
p primary partition (1-4)
l
#建立逻辑分区
First cylinder (655-2610, default 655):
#不用指定分区号,默认会从5开始分配,所以直接选择起始柱面
#注意:逻辑分区是在扩展分区内部再划分的,所以柱面是和扩展分区重叠的
Using default value 655
Last cylinder, +cylinders or +size{K, M, G} (655-2610, default 2610):+2G
#分配2GB大小
Command (m for help): n
#再建立一个逻辑分区
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (917-2610, default 917):
Using default value 917
Last cylinder, +cylinders or +size{K, M, G} (917-2610, default 2610):+2G
Command (m for help): p
#查看一下已经建立的分区
Disk /dev/sdb: 21.5GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 *512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xb4b0720c
Device Boot Start End Blocks id System
/dev/sdb1 1 654
5253223+ 83 Linux
#主分区
/dev/sdb2 655 2610 15711570
5 Extended
#扩展分区
/dev/sdb5 655 916
2104483+ 83 Linux
#逻辑分区 1
/dev/sdb6 917 1178
2104483+ 83 Linux
#逻辑分区2
Command (m for help): w
#保存并退出
The partition table has been altered!
Calling ioctl。to re-read partition table.
Syncing disks.
[root@localhost -]#
#退回到提示符界面
注意,所有的分区立过程中如果不保存并退出是不会生效的,所以建立错了也没有关系,使用 q 命令不保存退出即可。如果使用了 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.
#要求重新启动,才能格式化
Syncing disks.
看到了吗?必须重新启动!可是重新启动很浪费时间。如果不想重新启动,则可以使用 partprobe 命令。这个命令的作用是让系统内核重新读取分区表信息,这样就可以不用重新启动了。命令如下:
[root@localhost ~]# partprobe
如果这个命令不存在,则请安装 parted-2.1-18.el6.i686 这个软件包。partprobe 命令不是必需的,如果没有提示重启系统,则直接格式化即可。