brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: udisksctl power-off makes a USB disk safe to unplug

Disk
~/tips/tip-disk-udisksctl-power-off

udisksctl power-off -b /dev/sdX

Flushes, detaches and powers down a USB disk from the terminal, like ejecting it in a file manager.

umount detaches the filesystem, but the disk itself stays powered and may still have data in its own cache. The desktop’s eject button does more, and from a terminal udisksctl does the same.

udisksctl unmount -b /dev/sdX1
udisksctl power-off -b /dev/sdX

The first line unmounts the partition, and works without root for disks that your desktop session mounted. The second one tells the drive to flush its cache and powers off its USB port, so the disk spins down and the device disappears from the system. Then you can pull the cable.

Note the difference in the names: unmount gets the partition (sdX1), power-off gets the whole disk (sdX). If a disk has several partitions, unmount each one first; power-off refuses while something is still mounted.

udisksctl is part of udisks2, which is installed on most desktop distributions and often missing on servers. There, umount followed by sync and waiting until the activity light stops is the low-tech version, or echo 1 > /sys/block/sdX/device/delete, which removes the device from the system. That last one is easy to aim at the wrong disk, so check the name with lsblk first.

Some USB-to-SATA adapters ignore the power-off command. The disk then disappears from the system but keeps spinning, so I’d wait a few seconds anyway before unplugging.

Further reading: udisksctl(1) manual page

More Disk tips sudo e2freefrag /dev/sdb1 sudo smartctl -a --json /dev/sda | jq '.smart_status.passed' lsscsi -s du -sh — .[!.]* * 2>/dev/null | sort -h ls -l /dev/disk/by-id/ All Disk tips (44) All topics

Link