Linux下使用fdisk命令和partprobe命令,在不重启系统的情况下新建分区并格式化

      由于工作的需要,最近一段时间一直在学习Linux。学习一门新的知识,我是喜欢根据谋一本书或者某一个学习视频系统的学习,这样可以对学习的新知识有一个系统全面的认识和了解。所以学习之前,没了一本鸟哥的私房菜做为参考书,开始系统的学习Linux。

     根据鸟哥的建议,安装虚拟机时,预留了一块空的容量用来练习分区使用。所以在虚拟机上安装Linux系统分区时,只划分了一块4G的容量挂载到根目录“/”下,还有就是只划分了一个2G的swap分区。还有14G的容量没有分配。

     在学习磁盘管理时,使用fdisk将剩余的容量继续分区。却发现新建完分区后,无法通过partprobe重载分区。

具体操作如下所示:

1、使用fdisk新建分区

(。。。。)分区操作记录没有保存下来,执行w后,出现下列信息

Disk /dev/sda: 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: 0x000b604c Device Boot Start End Blocks Id System
/dev/sda1 * 1 523 4194304 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 523 784 2097152 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 784 2610 14672345 5 Extended
/dev/sda5 784 1421 5121671 83 LinuxCommand (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: 设备或资源忙.
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.

       保存新建分区,提示“设备或资源忙”,当时就在/dev/sda硬盘上做的操作,以为该提示为正常现象,所以忽略了,继续下一步操作。

2、通过partprobe重载分区表,避免重启系统

[root@stduy /]# partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (设备或资源忙). As a result, it may not reflect all of your changes until after reboot.
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
Error: 无效的分区表 - /dev/sr0 出现递归分区。

       重载分区出现的上述信息中,当时对于警告提示“设备或资源忙,需要重启系统才能使更改生效”直接忽略,以为是正常现象。但是对于错误信息“Error: 无效的分区表 - /dev/sr0 出现递归分区。”就有点茫然了,不过,既然提示“无效的分区表”,那我查下分区信息。

3、使用fdisk -l 命令查看分区信息

[root@stduy /]# fdisk -l
Disk /dev/sda: 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: 0x000b604c Device Boot Start End Blocks Id System
/dev/sda1 * 1 523 4194304 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 523 784 2097152 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 784 2610 14672345 5 Extended
/dev/sda5 784 1421 5121671 83 Linux

       查看分区信息,正常啊,并且刚才新建的扩展分区/dev/sda3和逻辑分区/dev/sda5都在。于是直接对新建的分区/dev/sda5进行格式化。

4、格式化新建的逻辑分区/dev/sda5

[root@stduy /]# mkfs -t ext3 /dev/sda5
mke2fs 1.41.12 (17-May-2010)
无法对 /dev/sda5 进行 stat 调用 --- 没有那个文件或目录
The device apparently does not exist; did you specify it correctly?

       格式化时,提示“没有那个文件或目录”。这就奇怪了,操作是根据参考书上一步一步来的,怎么就不行了呢?于是百度Linux下新建分区的一些信息,希望可以的找到类似的错误,借鉴解决办法。找了一圈下来,倒是有个类似的错误,但是没有给出解决办法。

      重新分析新建分区并重载分区表的过程,对于“设备或资源忙”的提示,因为当时就是在/dev/sda这块硬盘上操作的,提示“设备或资源忙”应该是正常的,重启下应该就可以了。但是重启后,还是找不到/dev/sda5,但是fdisk -l 查看是有的。于是怀疑是不是重载分区表时的“Error: 无效的分区表 - /dev/sr0 出现递归分区。”错误信息导致的呢?

     查看了下/dev下的sr0

[root@stduy dev]# ls -lh | grep sr0
lrwxrwxrwx. 1 root root           3 8月   5 00:12 cdrom -> sr0
lrwxrwxrwx. 1 root root           3 8月   5 00:12 cdrw -> sr0
lrwxrwxrwx. 1 root root           3 8月   5 00:12 dvd -> sr0
lrwxrwxrwx. 1 root root           3 8月   5 00:12 dvdrw -> sr0
lrwxrwxrwx. 1 root root           3 8月   5 00:12 scd0 -> sr0
brw-rw----. 1 root cdrom    11,   0 8月   5 00:12 sr0

      从上面的结果看,sr0是光驱设备,百度了下/dev/sr0这个设备,也所是光驱。于是管它重载分区表失败是不是由于“Error: 无效的分区表 - /dev/sr0 出现递归分区。”引起的,先把光驱移除了再说,反正是虚拟机上。

      把虚拟机上的光驱设备移除后,重新启动系统,再重新新建分区,并重新执行partprobe命令重载分区表,错误信息“Error: 无效的分区表 - /dev/sr0 出现递归分区。”没有了,只是警告提示“设备或资源忙”还在。这时直接格式化新建的逻辑分区/dev/sda5还是提示“没有那个文件或目录”。于是重启系统,再格式化/dev/sda5,这回成功了。

      也就是,之前新建分区格式化失败,是由于虚拟机的光驱/dev/sr0设备导致了分区表递归。另外由于是在同一块硬盘上操作,所以必须要重启系统才能使新建的分区写入分区表生效。

     那鸟哥的私房菜中,说的预留一块容量用于分区练习,鸟哥是怎么成功的呢?

     于是又找了一些Linux磁盘管理相关的视频和资料,发现,他们使用fdisk新建分区都是使用的多块硬盘实现的。再重新回去看鸟哥的私房菜,发现鸟哥预留的硬盘是/dev/hdc,那么鸟哥的环境中肯定还有/dev/hda和/dev/hdb,那么鸟哥试验也是在多快硬盘的基础上完成的。

     那我再给虚拟机分配一块硬盘试试。于是关闭系统,在虚拟机上又给系统分配了一块5G的虚拟磁盘空间。再开机进入系统重新对新加的磁盘空间进行分区、格式化等操作。

步骤还是和上面一样,分区、重载分区表、查看分区、格式化,这里不再按步骤,直接给出完成的操作记录。

[root@stduy ~]# fdisk -l

Disk /dev/sda: 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: 0x000b604c

Device Boot Start End Blocks Id System
/dev/sda1 * 1 523 4194304 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 523 784 2097152 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 784 2610 14672345 5 Extended
/dev/sda5 784 2610 14672313+ 83 Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x00000000

[root@stduy ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x851e79cc.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x851e79cc

Device Boot Start End Blocks Id System

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652):
Using default value 652

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x851e79cc

Device Boot Start End Blocks Id System
/dev/sdb1 1 652 5237158+ 83 Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@stduy ~]# partprobe /dev/sdb
[root@stduy ~]# mkfs -t ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=操作系统:Linux块大小=4096 (log=2)分块大小=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks327680 inodes, 1309289 blocks65464 blocks (5.00%) reserved for the super user
第一个数据块=0Maximum filesystem blocks=134217728040 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 36 mounts or180 days, whichever comes first.
Use tune2fs -c or -i to override.

     这回不管是新建分区,还是重载分区表都没有警告或错误提示了。成功实现在不重启系统的情况下添加硬盘并对新增硬盘进行分区、格式化等操作。

posted @ 2014-08-05 00:55  他们叫我兔子  阅读(25375)  评论(3编辑  收藏  举报