Safely Start, Stop and Eject SCSI Media with sg_start

sg_start sends a SCSI START STOP UNIT command: start a stopped disk, stop a spinning one, or load and eject removable media. The command can affect hardware immediately, so allow about 10 minutes for device identification and the first controlled test. You need the sg3-utils package and usually root access.

1. Before you touch the device

Identify the exact device. Replace /dev/sdX in the examples with the intended block device or pass-through device. Check it before every state-changing command.

lsblk -o NAME,MODEL,SERIAL,TYPE,SIZE,MOUNTPOINTS
readlink -f /dev/disk/by-id/REPLACE_WITH_THE_DEVICE_ID

Do not use a mounted system disk as a practice target. Stopping a disk can interrupt filesystems, services and virtual machines that still need it. If the device carries mounted filesystems, stop the dependent workload and unmount them according to your normal operating procedure first. sg_start does not make that work safe for you.

Checkpoint: Continue only when the model, serial number and device path match the hardware you intend to control.

2. Confirm the installed interface

Check the local utility. This guide was tested against Ubuntu's sg3-utils package version 1.46-3ubuntu4. The installed utility reports 0.67 20200930; the local manual is dated September 2020 and documents the newer option interface.

dpkg-query -W -f='${Package} ${Version}\n' sg3-utils
sg_start --version
sg_start --help

The useful default is easy to miss: with no action option, sg_start assumes --start. That means a bare invocation is not a harmless status query. Always name --start, --stop, --load or --eject in an operational script.

3. Start a stopped device

Send an explicit start command. Starting an already-running device is documented as harmless, but the command still needs access to the device.

sudo sg_start --start /dev/sdX
status=$?
printf 'sg_start exit=%s\n' "$status"

An exit value of 0 means that sg_start successfully issued the command. A non-zero value is a device or utility failure, not proof that the disk has become ready. The command normally waits for the requested action to complete. If you need to return as soon as the device receives the command, add --immed, but then readiness must be checked separately.

A stopped or still-starting rotating device may answer a readiness check with a SCSI "not ready" sense result. Use the installed sg_turs utility when your workflow needs to wait for readiness rather than treating a successful command submission as a ready disk.

4. Stop a disk, with a recovery path

Stop only an idle, correctly identified device. The stop action spins down the unit and limits access to the media. It also waits for the device by default.

# WARNING: this can disrupt anything using /dev/sdX.
sudo sg_start --stop --readonly /dev/sdX
status=$?
printf 'sg_start exit=%s\n' "$status"

--readonly opens the device read-only. The manual says it may be needed for ATA disks because opening them read-write can cause a nuisance spin-up after the command, defeating the stop. It is a useful first choice for that case, not a guarantee that other software will leave the disk stopped.

The normal stop path flushes media when the SCSI command variant requires it. --noflush suppresses that flush. Do not add it casually: it trades a shorter operation for a boundary where data still held by the device may not have reached stable media.

To undo the state change, use the same verified path and start the unit again:

sudo sg_start --start --readonly /dev/sdX
printf 'restart exit=%s\n' "$?"

On Linux, another mechanism can start an ATA disk again after --stop. The manual describes taking the device offline as an additional measure, but that is service-disrupting: while its state is offline, no SCSI commands can be sent. If you use that method, restore the state to running before attempting normal access, and treat it as an operations change rather than a routine verification step.

Checkpoint: After a stop, confirm that no workload needs the device and record the command exit status. Do not infer readiness or absence of I/O from silence at the terminal.

5. Load or eject removable media

Use the dedicated media action. These operations are for drives with removable media, such as an ATAPI CD or DVD drive.

# Load media and start the unit.
sudo sg_start --load /dev/srX

# Stop the unit and eject its medium.
sudo sg_start --eject /dev/srX

Ejection is a physical action and may interrupt a reader or writer. Check that no process is using the disc before running it. If a prior SCSI PREVENT ALLOW MEDIUM REMOVAL command locked the medium, the device can reject the eject with an illegal-request error. Release the lock using the procedure that created it, such as the matching sg_prevent operation, and only then retry.

--loej gives lower-level control: with --start it loads and starts, and with --stop it stops and ejects. If neither start nor stop is supplied, the current interface treats it as load and start. Use --load or --eject when that direct meaning is clearer. Do not combine multiple action selectors; the manual recommends using only one of 0, 1, --eject, --load, --start and --stop.

Power conditions and common traps

The default --pc=0 selects the ordinary start, stop, load and eject behaviour. The command also accepts device power conditions: --pc=1 for active, 2 for idle, 3 for standby, and 5 for sleep on MMC devices. When --pc is greater than zero, the manual says --loej is ignored. Choose one model for the operation rather than mixing a power condition with media handling and assuming both will apply.

If a script needs to distinguish submission from completion, record both the exit value and whether --immed was used. If it needs to inspect the SCSI command, add --verbose; repeated --verbose options increase the detail. Avoid the older interface unless maintaining an old script: --old must be the first option, and its historical --loej default differs when no start or stop action is given.

Done means