Safely Inspect, Resize and Format SCSI Media with sg_format

sg_format sends the SCSI FORMAT UNIT command that erases everything on the medium, so this guide is built around not sending it by accident. You will inspect the current block size and count, rehearse a change with no format command sent, resize a disk when appropriate, and verify a completed operation. The examples use an unmistakable placeholder device until you have identified the real one.

Allow 10 minutes for inspection and a dry run. A real format can take minutes, hours or longer depending on the medium and options. You need Linux, the sg3-utils package, access to the SCSI device, and a backup you have actually tested. The installed package here is sg3-utils 1.46-3ubuntu4; the binary reports sg_format version: 1.62 20201223. The installed manpage is headed sg3_utils-1.45, so check the binary's help when a local package differs from the documentation.

1. Confirm the binary and identify the device

Checking the command and package is ordinary, read-only work. No sudo needed:

$ command -v sg_format
/usr/bin/sg_format
$ sg_format --version
sg_format version: 1.62 20201223
$ dpkg-query -W -f='${Package} ${Version}\n' sg3-utils
sg3-utils 1.46-3ubuntu4

Now identify the device from your own inventory. /dev/sgX below is deliberately not something to paste unchanged. Use a device name you have matched to the enclosure, serial number and intended LUN, such as /dev/sg3 or a stable path under /dev/disk/by-id/. A SCSI generic node and a disk node can point at the same LUN, but two similar-looking names are not automatically interchangeable.

$ lsscsi -g
$ ls -l /dev/disk/by-id/

Checkpoint: write down the exact path and serial number. If you cannot explain which physical medium it selects, stop here. Elevated privileges may be needed for the later SCSI commands, but sudo cannot fix a mistaken device choice.

2. Inspect the current block size and count

Run sg_format with only the device argument. This is the tool's non-modifying inspection, pulling block information from both a MODE SENSE response and READ CAPACITY:

$ sudo sg_format /dev/sgX
Format unit. Device: /dev/sgX
block size: 512, number of blocks: 1953525168

Exact headings and numbers are device-specific, and some devices need the command run as root. What matters is the reported logical block size and block count, plus a clean exit status:

$ printf 'inspection status: %s\n' "$?"
inspection status: 0

If the two sources disagree, treat that as a warning about a possible format-corrupt state, not a cue to pick the larger value. Record the output and check the device documentation before making any change.

3. Rehearse a change with dry-run

--dry-run parses the options and prepares the operation but skips the actual FORMAT UNIT, FORMAT WITH PRESET or FORMAT MEDIUM command, and skips the MODE SELECT that would change the logical block size. That makes it a useful syntax and compatibility check, not proof the real operation will succeed.

Rehearse changing a disk to 4,096-byte blocks without sending the format:

$ sudo sg_format --dry-run --format --size=4096 /dev/sgX
$ printf 'dry-run status: %s\n' "$?"
dry-run status: 0

Keep --dry-run in the command while you review the device, count and block size. Only drop it once the backup and recovery plan have been checked. --format itself stays destructive even though the dry-run variant is not.

4. Resize without formatting

Use --resize with --count when you need to change the number of accessible blocks but keep the existing contents. A positive count makes READ CAPACITY report that many blocks:

$ sudo sg_format --resize --count=0x10000 /dev/sgX
$ sudo sg_format /dev/sgX

That hexadecimal value is 65,536 blocks. The remaining blocks are not erased. Resizing is reversible according to the manpage, so restore the normal maximum with:

$ sudo sg_format --resize --count=-1 /dev/sgX
$ sudo sg_format /dev/sgX

Do not confuse a smaller visible capacity with a smaller physical disk. Filesystems, partition tables and applications still need to understand the capacity change, and none of them like it happening under a mounted filesystem or an active database. Stop the relevant service and unmount first if the device is in use.

Checkpoint: run the read-only inspection again and compare the reported block count with the requested value. If it did not change, do not just repeat the command; inspect the error and the device's SCSI support instead.

5. Format a disk only after the final check

Warning: --format destroys all data on the medium, and can also change the logical block size with --size. A format cannot be undone by rerunning the command, so a backup that has never been restored in a test is not a recovery plan.

A normal format keeps the current block size and count unless you specify alternatives:

$ sudo sg_format --format /dev/sgX

Without --quick, sg_format gives you a 15-second pause and a chance to abort before it issues the format command. Leave that pause enabled while learning or working manually. --quick removes the last reconsideration point, which is a poor default for a command this destructive.

By default the format command uses IMMED mode and the program polls periodically. Add --wait when the caller must sit until the format operation actually completes:

$ sudo sg_format --format --wait /dev/sgX

Waiting can take many hours. Do not invent a short timeout: the tool already picks large internal timeouts based on the medium, and an operating-system timeout can leave the device in an incomplete or format-corrupt state. Choose --early instead and the program exits once formatting has started; monitor the still-busy device with tools such as sg_turs or sg_requests.

6. Verify the result and handle failures

After a format, wait for the device to become ready, then inspect capacity again:

$ sudo sg_format /dev/sgX
$ sudo sg_readcap -l /dev/sgX

Check that the logical block size and block count match what you intended. If protection information was part of the design, run sg_vpd -p ei -l before formatting to check supported protection types, then sg_readcap -l afterwards to inspect protection status. Do not add --fmtpinfo, --pfu or --pie just because they exist; they change the on-device format and need matching host support.

A non-zero exit status means the command did not report success, but an IMMED format can still be running when the initial command returns. Check readiness and capacity rather than retrying immediately. If a format was interrupted or timed out, stop using the medium and consult the device vendor or storage administrator: repeated resets tend to make recovery harder, not easier.

7. Treat tape formatting as a separate operation

For a tape drive, --tape=FM sends FORMAT MEDIUM instead of FORMAT UNIT. The format field can select the default format or partitioning, but the tape's own capabilities and mode page still matter:

$ sudo sg_format --tape=0 --wait /dev/sgX

This is equally destructive to the tape medium. Position the tape as the drive requires, stop software using it, and check the vendor procedure first. Use --verify only when the tape workflow calls for the FORMAT MEDIUM VERIFY bit. The disk examples above are no substitute for a tape recovery plan.

Done means