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]

Examples:
blockdev --getsize64 /dev/sda
blockdev --setra 2048 /dev/sdb1

PARAMETERS

--getbsz
    Get the block device logical block size in bytes.

--setbsz <BYTES>
    Set the block device logical block size to BYTES.

--getpbsz
    Get the physical block size in bytes.

--getss
    Get the logical sector size in bytes (usually 512 or 4096).

--getsize
    Get the device size in 512-byte sectors.

--getsize64
    Get the device size in bytes (64-bit value).

--getra
    Get the current read-ahead value in 512-byte sectors.

--setra <SECTORS>
    Set the read-ahead value to SECTORS (in 512-byte units).

--getfra
    Get the current file system read-ahead value.

--setfra <SECTORS>
    Set the file system read-ahead value.

--getiocount
    Get the I/O operation count.

--setiocount <COUNT>
    Set the I/O operation count to COUNT.

--flushbufs
    Flush all dirty buffers to disk (write cached data).

--rereadpt
    Force the kernel to reread the partition table.

--partscan
    Scan device for new partitions (often used after creating or deleting partitions).

--getro
    Get the read-only status (1 if read-only, 0 if read-write).

--setro
    Set the device to read-only mode.

--setrw
    Set the device to read-write mode.

--getdiscard
    Get discard (TRIM/UNMAP) support status (1 if supported, 0 if not).

--getalignoff
    Get the alignment offset in bytes.

--getmaxsect
    Get the maximum sectors per request.

--getblkioopt
    Get the I/O scheduler's optimal I/O size.

--getblkiobug
    Get the I/O scheduler's granularity.

--getdiscardzeroes
    Get the discard_zeroes_data value.

--getzoned
    Get the zoned block device type.

--setmq <QUEUE_DEPTH>
    Set multi-queue depth.

--getmq
    Get multi-queue depth.

-q, --quiet
    Suppress output messages.

-V, --version
    Display version information and exit.

-h, --help
    Display a help message and exit.

DESCRIPTION

blockdev is a command-line utility used to manage and manipulate various attributes of block devices, such as hard drives, SSDs, and USB drives. It provides a way to query and set parameters related to a device's physical and logical characteristics, I/O performance, and behavior.

Common uses include retrieving device size, block sizes, and read-ahead settings, as well as operations like flushing write buffers, rereading partition tables, and changing read-only status. This tool is often employed by system administrators or scripts requiring low-level interaction with storage devices to optimize performance or ensure data integrity. It directly interacts with the kernel's block layer via ioctl system calls, making it a powerful but potentially risky tool if used incorrectly.

CAVEATS

Most operations that modify device parameters or perform privileged actions (e.g., flushing buffers, rereading partition tables) require root privileges.

Incorrect usage, especially when setting parameters like block sizes or read-ahead, can potentially lead to performance degradation or even data corruption.

Changes made by blockdev are often temporary and may not persist across reboots, especially for system-managed parameters, unless explicitly configured in system-wide configuration files (e.g., udev rules or init scripts).

<I>KERNEL INTERACTION</I>

blockdev primarily interacts with the Linux kernel through the ioctl() system call on the specified device file (e.g., /dev/sda). This allows it to directly query and modify parameters managed by the kernel's block layer and device drivers, offering fine-grained control over storage devices.

<I>DEVICE FILES</I>

The command operates on block device files, which are special files typically found in the /dev/ directory (e.g., /dev/sda, /dev/nvme0n1, /dev/dm-0). It is crucial to specify the correct device path to avoid unintended operations on other devices.

HISTORY

The blockdev command is part of the util-linux project, a collection of essential Linux utilities. Its core functionality, providing a user-space interface to various ioctl commands for interacting directly with block device drivers, has been a fundamental aspect of Linux kernel's block layer management for a long time. While not always used interactively by end-users, it remains a critical low-level tool for system administrators and in scripting for managing storage devices and their properties within the Linux ecosystem.

SEE ALSO

fdisk(8), parted(8), lsblk(8), hdparm(8)

Copied to clipboard