Linux下使用fdisk命令和partprobe命令,在不重啟系統的情況下建立分區並格式化

來源:互聯網
上載者:User

標籤:des   使用   os   strong   io   檔案   資料   for   

      由於工作的需要,最近一段時間一直在學習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重載分區表,避免重啟系統

[[email protected] /]# 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 命令查看分區資訊

[[email protected] /]# 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

[[email protected] /]# 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

[[email protected] 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的虛擬磁碟空間。再開機進入系統重新對新加的磁碟空間進行分區、格式化等操作。

步驟還是和上面一樣,分區、重載分區表、查看分區、格式化,這裡不再按步驟,直接給出完成的操作記錄。

[[email protected] ~]# 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

[[email protected] ~]# 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.
[[email protected] ~]# partprobe /dev/sdb
[[email protected] ~]# 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.

     這回不管是建立分區,還是重載分區表都沒有警告或錯誤提示了。成功實現在不重啟系統的情況下添加硬碟並對新增硬碟進行分區、格式化等操作。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.