Linux下用fdisk命令和partprobe命令不重启系统新建分区并格式化

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

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

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

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

具体操作如下所示:

1、使用fdisk新建分区

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

  1. Disk /dev/sda: 21.5 GB, 21474836480 bytes  
  2. 255 heads, 63 sectors/track, 2610 cylinders  
  3. Units = cylinders of 16065 * 512 = 8225280 bytes  
  4. Sector size (logical/physical): 512 bytes / 512 bytes  
  5. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  6. Disk identifier: 0x000b604c Device Boot Start End Blocks Id System  
  7. /dev/sda1 * 1 523 4194304 83 Linux  
  8. Partition 1 does not end on cylinder boundary.  
  9. /dev/sda2 523 784 2097152 82 Linux swap / Solaris  
  10. Partition 2 does not end on cylinder boundary.  
  11. /dev/sda3 784 2610 14672345 5 Extended  
  12. /dev/sda5 784 1421 5121671 83 LinuxCommand (m for help): w  
  13. The partition table has been altered!Calling ioctl() to re-read partition table.WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.  
  14. The kernel still uses the old table. The new table will be used at  
  15. the next reboot or after you run partprobe(8) or kpartx(8)  
  16. Syncing disks.  
  17.  

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

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

  1. [root@stduy /]# partprobe   
  2. 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.  
  3. Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。  
  4. Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。  
  5. Error: 无效的分区表 - /dev/sr0 出现递归分区。 

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

#p#

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

  1. [root@stduy /]# fdisk -l  
  2. Disk /dev/sda: 21.5 GB, 21474836480 bytes  
  3. 255 heads, 63 sectors/track, 2610 cylinders  
  4. Units = cylinders of 16065 * 512 = 8225280 bytes  
  5. Sector size (logical/physical): 512 bytes / 512 bytes  
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  7. Disk identifier: 0x000b604c Device Boot Start End Blocks Id System  
  8. /dev/sda1 * 1 523 4194304 83 Linux  
  9. Partition 1 does not end on cylinder boundary.  
  10. /dev/sda2 523 784 2097152 82 Linux swap / Solaris  
  11. Partition 2 does not end on cylinder boundary.  
  12. /dev/sda3 784 2610 14672345 5 Extended  
  13. /dev/sda5 784 1421 5121671 83 Linux 

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

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

  1. [root@stduy /]# mkfs -t ext3 /dev/sda5  
  2. 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

  1. [root@stduy dev]# ls -lh | grep sr0  
  2. lrwxrwxrwx. 1 root root 3 8月 5 00:12 cdrom -> sr0  
  3. lrwxrwxrwx. 1 root root 3 8月 5 00:12 cdrw -> sr0  
  4. lrwxrwxrwx. 1 root root 3 8月 5 00:12 dvd -> sr0  
  5. lrwxrwxrwx. 1 root root 3 8月 5 00:12 dvdrw -> sr0  
  6. lrwxrwxrwx. 1 root root 3 8月 5 00:12 scd0 -> sr0  
  7. 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的虚拟磁盘空间。再开机进入系统重新对新加的磁盘空间进行分区、格式化等操作。

#p#

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

  1. [root@stduy ~]# fdisk -l  
  2.  
  3. Disk /dev/sda: 21.5 GB, 21474836480 bytes  
  4. 255 heads, 63 sectors/track, 2610 cylinders  
  5. Units = cylinders of 16065 * 512 = 8225280 bytes  
  6. Sector size (logical/physical): 512 bytes / 512 bytes  
  7. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  8. Disk identifier: 0x000b604c  
  9.  
  10. Device Boot Start End Blocks Id System  
  11. /dev/sda1 * 1 523 4194304 83 Linux  
  12. Partition 1 does not end on cylinder boundary.  
  13. /dev/sda2 523 784 2097152 82 Linux swap / Solaris  
  14. Partition 2 does not end on cylinder boundary.  
  15. /dev/sda3 784 2610 14672345 5 Extended  
  16. /dev/sda5 784 2610 14672313+ 83 Linux  
  17.  
  18. Disk /dev/sdb: 5368 MB, 5368709120 bytes  
  19. 255 heads, 63 sectors/track, 652 cylinders  
  20. Units = cylinders of 16065 * 512 = 8225280 bytes  
  21. Sector size (logical/physical): 512 bytes / 512 bytes  
  22. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  23. Disk identifier: 0x00000000  
  24.  
  25. [root@stduy ~]# fdisk /dev/sdb  
  26. Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel  
  27. Building a new DOS disklabel with disk identifier 0x851e79cc.  
  28. Changes will remain in memory only, until you decide to write them.  
  29. After that, of course, the previous content won't be recoverable.  
  30.  
  31. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)  
  32.  
  33. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to  
  34. switch off the mode (command 'c') and change display units to  
  35. sectors (command 'u').  
  36.  
  37. Command (m for help): p  
  38.  
  39. Disk /dev/sdb: 5368 MB, 5368709120 bytes  
  40. 255 heads, 63 sectors/track, 652 cylinders  
  41. Units = cylinders of 16065 * 512 = 8225280 bytes  
  42. Sector size (logical/physical): 512 bytes / 512 bytes  
  43. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  44. Disk identifier: 0x851e79cc  
  45.  
  46. Device Boot Start End Blocks Id System  
  47.  
  48. Command (m for help): n  
  49. Command action  
  50. e extended  
  51. p primary partition (1-4)  
  52. p  
  53. Partition number (1-4): 1  
  54. First cylinder (1-652, default 1):   
  55. Using default value 1  
  56. Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652):   
  57. Using default value 652  
  58.  
  59. Command (m for help): p  
  60.  
  61. Disk /dev/sdb: 5368 MB, 5368709120 bytes  
  62. 255 heads, 63 sectors/track, 652 cylinders  
  63. Units = cylinders of 16065 * 512 = 8225280 bytes  
  64. Sector size (logical/physical): 512 bytes / 512 bytes  
  65. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  66. Disk identifier: 0x851e79cc  
  67.  
  68. Device Boot Start End Blocks Id System  
  69. /dev/sdb1 1 652 5237158+ 83 Linux  
  70.  
  71. Command (m for help): w  
  72. The partition table has been altered!  
  73.  
  74. Calling ioctl() to re-read partition table.  
  75. Syncing disks.  
  76. [root@stduy ~]# partprobe /dev/sdb  
  77. [root@stduy ~]# mkfs -t ext4 /dev/sdb1  
  78. 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表: 完成

  1. Creating journal (32768 blocks): 完成  
  2. Writing superblocks and filesystem accounting information: 完成  
  3. This filesystem will be automatically checked every 36 mounts or180 days, whichever comes first.   
  4. Use tune2fs -c or -i to override. 

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

原文链接:http://www.cnblogs.com/tschengbin/p/3891368.html

责任编辑:牛小雨 来源: 他们叫我兔子的博客
相关推荐

2022-01-11 15:10:18

Linuxfdiskparted

2012-05-10 10:10:19

Linuxfdisk

2018-01-15 14:59:35

Linux命令交换分区

2010-02-03 15:07:15

2023-01-12 09:30:31

Linux命令行xml

2019-08-08 14:47:07

Linux命令行JSON

2022-12-30 08:30:28

Linux命令行json

2022-08-01 21:38:25

Linux fmt命令

2023-04-11 10:37:40

bash命令printf

2018-12-03 09:10:07

Linux驱动器命令

2013-07-02 10:25:03

LinuxUSB设备

2018-11-01 09:45:40

2012-03-27 09:42:57

JavaScriptCSS

2012-05-10 10:19:42

Linuxmke2fsk格式化

2021-09-22 15:00:24

Linuxwatch 命令

2010-08-03 09:53:50

Linux NFS

2011-08-22 16:23:29

2010-02-22 09:13:29

Ubuntu硬盘

2010-12-24 10:25:20

SCP 命令

2009-11-18 09:26:36

Ubuntulinux关机
点赞
收藏

51CTO技术栈公众号