LinuxCommandLibrary

blockdev

Get or set block device properties

TLDR

Print a report for all devices

$ sudo blockdev --report
copy

Print a report for a specific device
$ sudo blockdev --report [/dev/sdXY]
copy

Get the size of a device in 512-byte sectors
$ sudo blockdev --getsz [/dev/sdXY]
copy

Set read-only
$ sudo blockdev --setro [/dev/sdXY]
copy

Set read-write
$ sudo blockdev --setrw [/dev/sdXY]
copy

Flush buffers
$ sudo blockdev --flushbufs [/dev/sdXY]
copy

Get the physical block size
$ sudo blockdev --getpbsz [/dev/sdXY]
copy

Set the read-ahead value to 128 sectors
$ sudo blockdev --setra 128 [/dev/sdXY]
copy

SYNOPSIS

blockdev [options] [device]

PARAMETERS

--getsz, --getsize
    print device size in 512-byte sectors

--getss
    print logical block (sector) size in bytes

--getpbsz
    print physical block (sector) size in bytes

--getbsz
    print kernel preferred I/O block size in bytes

--setbs size
    set kernel preferred I/O block size to size bytes

--rereadpt
    reread partition table from device

--flushbufs
    flush kernel buffers (and tables if --rereadpt)

--getro
    print read-only status (0=r/w, 1=ro)

--setro
    set device read-only

--getssz
    print minimum I/O size supported by device

--getiomin
    print optimal I/O size supported by device

--getdiscardzeroes
    print 1 if TRIM discards zeroes data (else 0)

--discard nr_sectors
    TRIM nr_sectors 512-byte sectors

--step granularity
    TRIM step size in 512-byte sectors

--zeromode mode
    set zeroing mode: none(0), zero+verify(1), verify(2)

--getzeromode
    print current zeroing mode

--verify[=sector_size]
    verify device integrity (optional sector size)

--weight weight
    set blk-mq I/O scheduler weight (1-1000)

--help
    display usage information

--version
    output version information

DESCRIPTION

blockdev is a command-line utility from the util-linux package that provides low-level control over block devices in Linux, such as hard disks, SSDs, and partitions. It enables administrators to query device geometry (e.g., sector sizes, block sizes, capacity), manipulate kernel buffers, reread partition tables, set read-only status, perform TRIM operations for SSD optimization, verify device integrity, and configure advanced I/O parameters like scheduler weights or zeroing modes.

By issuing direct ioctl() system calls to the kernel, blockdev offers precise, efficient operations without filesystem dependencies, making it ideal for boot scripts, disk maintenance, and troubleshooting. For example, it can flush buffers before repartitioning or discard unused blocks to improve SSD performance. Most actions require root privileges due to their potential to affect system stability or data integrity. While powerful, improper use risks data corruption, especially on mounted devices.

Modern features support blk-mq I/O schedulers and secure zeroing verification, reflecting ongoing kernel evolution. It complements tools like hdparm for ATA drives or fstrim for filesystems.

CAVEATS

Requires root for most options.
Can cause data loss or crashes if used on mounted/in-use devices.
TRIM (--discard) ineffective on mounted filesystems; use fstrim instead.
Not all options supported on every device type.

EXAMPLES

blockdev --flushbufs --rereadpt /dev/sda
Flush buffers and reread partition table.

blockdev --getbsz /dev/nvme0n1
Query optimal I/O size.

blockdev --discard 1000000 /dev/sdb
TRIM 500MB on unmounted device.

HISTORY

Introduced in util-linux 2.10 (2003) by Andries E. Brouwer. Enhanced in later versions for SSD support (TRIM, 2010+), blk-mq (2015+), and verification. Actively maintained by Karel Zak in util-linux project.

SEE ALSO

hdparm(8), blkdiscard(8), fstrim(8), lsblk(1), partprobe(8)

Copied to clipboard