Method to test if disks in system are formatted
Solution 1
Depending on how you access the drives, you could use blkid -o list
(deprecated) on them and then parse the output.
The command outputs, among other things, a fs_type label column, that shows the filesystem.
blkid -o list
has been superseded be lsblk -f
.
Solution 2
The logic I would use may appear to be a little convoluted, but I think it should catch the failure mode.
Basically the steps are
- mke2fs
- mount the filesystem
- Create a file called "format.complete" in the filesystem
- unmount the filesystem
So we need to put some tests before this. The logic would be:
- Attempt to mount the filesystem at
$tmpmount
forcingext2
- If
mount
returned error code ==> Goto NOT FORMATTED - If
$tmpmount/lost+found
does not exist then an odd filesystem mounted; should not happen but...umount
. Goto NOT FORMATTED - If
$tmpmount/format.complete
does not exist then format was interrupted;umount
. Goto NOT FORMATTED umount
==> FORMATTED, skip to next disk.
The "NOT FORMATTED" would be the original 4 steps.
We can add those structures together. The result would be that disks would only be formatted if they don't have a format.complete
file on them.
Once all disks have been formatted you can optionally then remount each one and delete the format.complete
file.
Essentially we maintain a small amount of state on each disk and use that to determine if formatting was successful.
Related videos on Youtube
Comments
-
Jim almost 2 years
Currently working on a project where I'm dealing with an arbitrary group of disks in multiple systems. I've written a suite of software to burn-in these disks. Part of that process was to format the disks. While testing my software, I realized that if at some point during formatting the disks, the process stops/dies, and I want to restart the process, I really don't want to reformat all of the disks in the set, which have already successfully formatted.
I'm running this software from a ramfs with no disks mounted and none of the disks I am working on ever get mounted and they not be used by my software for anything other than testing, so anything goes on these bad boys. There's no data about which to be concerned.
EDIT:
No, I'm not partitioning.
Yes, ext2 fs.
This is the command I'm using to format:
(/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 &)
SOLUTION:
Thanks to Jan's suggestion below:
# lsblk -f /dev/<drv>
I concocted the following shell function, which works as expected.SOURCE
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 }
DATA
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
Which yielded:
[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.
Do note that the magic potion was extending Jan's suggestion to simply output what I cared about:
lsblk -no KNAME,FSTYPE $drive
-
Admin almost 8 yearsAre you partitioning these disks or formatting the whole disk?
-
Admin almost 8 yearsAre you always formatting with the same FS?
-
Admin almost 8 yearsWouldn't it make more sense to keep track of your job progress and simply restart after the last successful operation? Note that I seriously wonder what type of error would be so minor that you'd want to continue where you left off considering that a formatting error of your RAM disks would indicate a serious misconfiguration for your project or a hardware error.
-
Admin almost 8 years
lsblk /dev/sdd1 -no fstype
will return the file system (if any) for e.g.sdd1
- it's then just a matter of doing a string comparison... -
Admin almost 8 years@JuliePelletier it would be if the use-case was that simple. In this use case, it's very possible, though unlikely, that the machine be rebooted before the completion of the script. Since the OS is in ram, there is no state kept between boots. I needed to take this possibility into consideration.
-
Admin almost 8 yearsDoesn't your RAM FS get formatted in a matter of seconds? How many days will you use to save a few minutes?
-
Admin almost 8 yearsThe RAM FS is not getting formatted at all. Nevertheless, I don't expect to be able to go through the entire process we use for our use-case. This was a requirement. Thank you for your assistance.
-
-
Jim almost 8 yearsYeah, I was just thinking about this same method. Though I was looking for a specific command, this is probably the way I'm going to have to solve the problem.