測試系統中的磁碟是否已格式化的方法
目前正在從事一個項目,我正在處理多個系統中的任意一組磁碟。我編寫了一套軟體來燒錄這些磁碟。該過程的一部分是格式化磁碟。在測試我的軟體時,我意識到如果在格式化磁碟過程中的某個時刻,程序停止/終止,並且我想重新啟動程序,我真的不想重新格式化集合中的所有磁碟,這些磁碟已經格式化成功。
我從一個沒有安裝磁碟的 ramfs 執行這個軟體,而且我正在處理的任何磁碟都沒有被安裝,我的軟體除了測試之外沒有使用它們,所以這些壞男孩會發生任何事情。沒有值得關注的數據。
編輯:
不,我沒有分區。
是的,ext2 fs。
這是我用來格式化的命令:
(/sbin/mke2fs -q -O sparse_super,large_file -m 0 -T largefile -T xfs -FF $drive >> /tmp/mke2fs_drive.log 2>&1 & echo $? > $status_file &)
解決方案:
感謝 Jan 的以下建議:
# lsblk -f /dev/<drv>
我編寫了以下 shell 函式,它按預期工作。來源
is_formatted() { drive=$1 fs_type=$2 if [[ ! -z $drive ]] then if [[ ! -z $fs_type ]] then current_fs=$(lsblk -no KNAME,FSTYPE $drive) if [[ $(echo $current_fs | wc -w) == 1 ]] then echo "[INFO] '$drive' is not formatted. Formatting." return 0 else current_fs=$(echo $current_fs | awk '{print $2}') if [[ $current_fs == $fs_type ]] then echo "[INFO] '$drive' is formatted with correct fs type. Moving on." return 1 else echo "[WARN] '$drive' is formatted, but with wrong fs type '$current_fs'. Formatting." return 0 fi fi else echo "[WARN] is_formatted() was called without specifying fs_type. Formatting." return 0 fi else echo "[FATAL] is_formatted() was called without specifying a drive. Quitting." return -1 fi }
數據
sdca ext2 46b669fa-0c78-4b37-8fc5-a26368924b8c sdce ext2 1a375f80-a08c-4889-b759-363841b615b1 sdck ext2 f4f43e8c-a5c6-495f-a731-2fcd6eb6683f sdcn sdby ext2 cf276cce-56b1-4027-a795-62ef62d761fa sdcd ext2 42fdccb8-e9bc-441e-a43a-0b0f8d409c71 sdci ext2 d6e7dc60-286d-41e2-9e1b-a64d42072253 sdbw ext2 c3986491-b83f-4001-a3bd-439feb769d6a sdch ext2 3e7dba24-e3ec-471a-9fae-3fee91f988bd sdcq sdcf ext2 8fd2a6fd-d1ae-449b-ad48-b2f9df997e5f sdcs sdco sdcw ext2 27bf220e-6cb3-4953-bee4-aff27c491721 sdcp ext2 133d9474-e696-49a7-9deb-78d79c246844 sdcx sdct sdcu sdcy sdcr sdcv sdde sddc ext2 0b22bcf1-97ea-4d97-9ab5-c14a33c71e5c sddi ext2 3d95fbcb-c669-4eda-8b57-387518ca0b81 sddj sddb sdda ext2 204bd088-7c48-4d61-8297-256e94feb264 sdcz sddk ext2 ed5c8bd8-5168-487f-8fee-4b7c671ef2cb sddl sddn sdds ext2 647d2dea-f71d-4e87-bbe5-30f6424b36c9 sddf ext2 47128162-bcb7-4eab-802d-221e8eb36074 sddo sddh ext2 b7f41e1a-216d-4580-97e6-f2df917754a8 sddg ext2 39b838e0-f0ae-447c-8876-2d36f9099568
這產生了:
[INFO] '/dev/sdca' is formatted with correct fs type. Moving on. [INFO] '/dev/sdce' is formatted with correct fs type. Moving on. [INFO] '/dev/sdck' is formatted with correct fs type. Moving on. [INFO] '/dev/sdcn' is not formatted. Formatting. [INFO] '/dev/sdby' is formatted with correct fs type. Moving on. [INFO] '/dev/sdcd' is formatted with correct fs type. Moving on. [INFO] '/dev/sdci' is formatted with correct fs type. Moving on. [INFO] '/dev/sdbw' is formatted with correct fs type. Moving on. [INFO] '/dev/sdch' is formatted with correct fs type. Moving on. [INFO] '/dev/sdcq' is not formatted. Formatting. [INFO] '/dev/sdcf' is formatted with correct fs type. Moving on. [INFO] '/dev/sdcs' is not formatted. Formatting. [INFO] '/dev/sdco' is not formatted. Formatting. [INFO] '/dev/sdcw' is formatted with correct fs type. Moving on. [INFO] '/dev/sdcp' is formatted with correct fs type. Moving on. [INFO] '/dev/sdcx' is not formatted. Formatting. [INFO] '/dev/sdct' is not formatted. Formatting. [INFO] '/dev/sdcu' is not formatted. Formatting. [INFO] '/dev/sdcy' is not formatted. Formatting. [INFO] '/dev/sdcr' is not formatted. Formatting. [INFO] '/dev/sdcv' is not formatted. Formatting. [INFO] '/dev/sdde' is not formatted. Formatting. [INFO] '/dev/sddc' is formatted with correct fs type. Moving on. [INFO] '/dev/sddi' is formatted with correct fs type. Moving on. [INFO] '/dev/sddj' is not formatted. Formatting. [INFO] '/dev/sddb' is not formatted. Formatting. [INFO] '/dev/sdda' is formatted with correct fs type. Moving on. [INFO] '/dev/sdcz' is not formatted. Formatting. [INFO] '/dev/sddk' is formatted with correct fs type. Moving on. [INFO] '/dev/sddl' is not formatted. Formatting. [INFO] '/dev/sddn' is not formatted. Formatting. [INFO] '/dev/sdds' is formatted with correct fs type. Moving on. [INFO] '/dev/sddf' is formatted with correct fs type. Moving on. [INFO] '/dev/sddo' is not formatted. Formatting. [INFO] '/dev/sddh' is formatted with correct fs type. Moving on. [INFO] '/dev/sddg' is formatted with correct fs type. Moving on.
請注意,魔法藥水正在擴展 Jan 的建議,以簡單地輸出我關心的內容:
lsblk -no KNAME,FSTYPE $drive
根據您訪問驅動器的方式,您可以
blkid -o list
在它們上使用(不推薦使用)然後解析輸出。該命令會輸出顯示文件系統的 fs_type 標籤列等。
blkid -o list
已被取代lsblk -f
。
我將使用的邏輯可能看起來有點複雜,但我認為它應該擷取故障模式。
基本上步驟是
- 妻子2fs
- 掛載文件系統
- 在文件系統中創建一個名為“format.complete”的文件
- 解除安裝文件系統
所以我們需要在此之前進行一些測試。邏輯是:
- 嘗試
$tmpmount
強制掛載文件系統ext2
- 如果
mount
返回錯誤程式碼 ==> Goto NOT FORMATTED- 如果
$tmpmount/lost+found
不存在,則安裝一個奇怪的文件系統;不應該發生,但是umount
…… 轉到未格式化- 如果
$tmpmount/format.complete
不存在則格式被中斷;umount
. 轉到未格式化umount
==> 格式化,跳到下一個磁碟。“未格式化”將是原始的 4 個步驟。
我們可以將這些結構加在一起。結果是磁碟只有在沒有
format.complete
文件的情況下才會被格式化。格式化所有磁碟後,您可以選擇重新安裝每個磁碟並刪除
format.complete
文件。本質上,我們在每個磁碟上維護少量狀態,並使用它來確定格式化是否成功。