システムディスクがフォーマットされているかどうかをテストする方法

システムディスクがフォーマットされているかどうかをテストする方法

現在、複数のシステムにわたってランダムなディスクグループを扱うプロジェクトを進めています。私はこのディスクを焼くためのソフトウェアを書いた。プロセスの一部はディスクをフォーマットすることです。私のソフトウェアのテスト中に、ディスクフォーマットのある時点でプロセスが停止/終了し、プロセスを再起動したい場合は、グループ内のすべてのディスクを再フォーマットしたくないことに気づきました。これらのディスクはフォーマットされています。正常に。

私はディスクのramfがマウントされた状態でこのソフトウェアを実行したことがなく、作業中のディスクはマウントされていません。いいえ私のソフトウェアはテスト以外の目的で使用することができます。参考になる資料はありません。

編集する:

いいえ、分割しません。

はい、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>次のシェル関数を作成しましたが、期待どおりに動作します。

源泉

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.

Magic Potionは、私が興味のあるものを簡単に出力するようにJanの提案を拡張しています。lsblk -no KNAME,FSTYPE $drive

ベストアンサー1

ドライブにアクセスする方法に応じて、blkid -o listドライブを使用して(推奨されていない)出力を解析できます。
特に、このコマンドはファイルシステムを表示するfs_typeラベル列を出力します。

blkid -o listが置き換えられましたlsblk -f

おすすめ記事