测试系统磁盘是否格式化的方法

测试系统磁盘是否格式化的方法

目前正在开发一个项目,我正在处理多个系统中的任意磁盘组。我编写了一套软件来烧录这些磁盘。该过程的一部分是格式化磁盘。在测试我的软件时,我意识到,如果在格式化磁盘期间的某个时刻,该过程停止/终止,并且我想重新启动该过程,我真的不想重新格式化该组中的所有磁盘,这些磁盘已经格式化成功。

我从没有安装任何磁盘的 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

答案1

根据您访问驱动器的方式,您可以使用blkid -o list(不推荐)它们,然后解析输出。
除其他内容外,该命令还输出一个 fs_type 标签列,用于显示文件系统。

blkid -o list已被取代lsblk -f

答案2

我将使用的逻辑可能看起来有点复杂,但我认为它应该捕获故障模式。

基本上步骤是

  1. mke2fs
  2. 挂载文件系统
  3. 在文件系统中创建一个名为“format.complete”的文件
  4. 卸载文件系统

所以在此之前我们需要进行一些测试。逻辑是:

  1. 尝试$tmpmount强制挂载文件系统ext2
  2. 如果mount返回错误代码==>转到NOT FORMATTED
  3. 如果$tmpmount/lost+found不存在,则安装了一个奇怪的文件系统;不应该发生但是umount......转到未格式化
  4. 如果$tmpmount/format.complete不存在则格式化被中断;umount。转到未格式化
  5. umount==> 已格式化,跳到下一个磁盘。

“未格式化”是原来的 4 个步骤。

我们可以将这些结构添加在一起。结果是,只有当磁盘format.complete上没有文件时才会对其进行格式化。

格式化所有磁盘后,您可以选择重新安装每个磁盘并删除文件format.complete

本质上,我们在每个磁盘上维护少量状态,并使用它来确定格式化是否成功。

相关内容