
blockdev Command in Linux
The blockdev command in Unix/Linux is a robust utility that allows you to manage, query, and manipulate block devices.
The blockdev command lets you invoke block device input/output control (IOCTL) commands directly from the command line. You can use this command line utility to interact with block devices such as hard disks, SSDs, and USB drives.
Table of Contents
Here is a comprehensive guide to the options available with the blockdev command −
- Syntax of blockdev Command
- blockdev Command Options
- Useful Commands for Managing Block Devices
- Examples of blockdev Command in Linux
- Common Issues with the blockdev Command
Syntax of blockdev Command
The following is the general for the blockdev command −
blockdev [options] <device> <command> [arguments]
Where −
- <device> is the path to the block device you want to interact with (e.g., /dev/sda).
- <command> represents the specific operation you want to perform (e.g., --getsize64, --setro, etc.).
- [arguments] are any additional parameters or values required by the command.
blockdev Command Options
The following are different options you can use with the blockdev command −
Options | Description |
---|---|
-q | Be quiet. |
-v | Be verbose. |
-V | Print version and exit. |
--report |
Print a report for the specified device. You can provide multiple devices. If none are given, all devices listed in /proc/partitions are shown. |
Useful Commands for Managing Block Devices
The blockdev command in Unix/Linux offers various useful commands for managing block devices −
Options | Description |
---|---|
--getsz | Retrieves the size of the block device in 512-byte sectors. |
--setro | Sets the block device to read-only mode. |
--setrw | Sets the block device back to read-write mode |
--getro | Checks if the block device is currently in read-only mode. |
--getdiscardzeroe | Determines if the device supports discarding zeroed blocks (part of TRIM support for SSDs). |
--getss | Prints the logical block (sector) size of the device (usually 512 bytes). |
--getpbsz | Retrieves the physical block (sector) size of the device. |
--getiomin | Provides the minimum I/O size for the block device. |
--getioopt | Gives the optimal I/O size for the device. |
--getalignoff | Retrieves the alignment offset in bytes. |
--getmaxsect | Determines the maximum sectors per I/O request. |
--getbsz | Prints the block size in bytes (used internally by the kernel). |
--setbsz <bytes> | Sets the block size on the file descriptor when opening the block device. |
--setra <sectors> | Sets the readahead (buffering) for the device (in 512-byte sectors) |
--getfra | Retrieves the filesystem readahead setting. |
--getdiskseq | Gets the disk sequence numberFlushes buffers associated with the device. |
--flushbufs | Flushes buffers associated with the device. |
--rereadpt | Rereads the partition table for the device. |
Examples of blockdev Command in Linux
In this section, we'll look at some common blockdev commands and their usage.
- Get Block Size
- Change the Block Size of a Device
- Set Readahead
- Retrieve Filesystem Readahead
- Get Disk Sequence Number
- Reread Partition Table
- Mount a Disk Image
Get Block Size
To retrieve the block size of a specific device, use the following syntax −
sudo blockdev --getbsz /dev/sda2

Change the Block Size of a Device
Before changing the block size, unmount the filesystem associated with the device −
sudo umount /dev/sdb1

Next, create a new file system with the desired block size. For instance, to set a block size of 4096 bytes (4 KB) for a vfat file system, use the following command −
sudo mkfs -t ext4 -b 4096 /dev/sdb1

Now, verify the changed block size with the following command −
sudo blockdev --getbsz /dev/sdb1

Set Readahead
To adjust the readahead (buffering) for a device (in 512-byte sectors), you can use the following command:
sudo blockdev --setra 64 /dev/sdb1

Retrieve Filesystem Readahead
To retrieve the filesystem readahead setting for a device, use the following command −
sudo blockdev --getfra /dev/sdb1

Get Disk Sequence Number
To obtain the disk sequence number for a device, you can simply use the following command −
sudo blockdev --getdiskseq /dev/sdb1

Reread Partition Table
To reread the partition table for a device, use the following command −
sudo blockdev --rereadpt /dev/sdb

Mount a Disk Image
To mount a disk image using the blockdev command, first set the Loop Device to Read-Only Mode −
sudo blockdev --setro /dev/loop2

Replace /dev/loop2 with your appropriate loop device name.
Next, mount the disk image (e.g., an ISO file) (e.g., /mnt) using the mount command −
sudo mount /dev/loop2 /mnt

Common Issues with the blockdev Command
Following are some of the common issues attached with the blockdev command −
- Device Not Found − This issue occurs when the specified device (e.g., /dev/sda2) is not connected to the system or is not correctly identified. To resolve this, check the device's physical connection and ensure the system properly recognizes it.
- Permission Denied − If you encounter a "permission denied" error, it means you dont have sufficient permissions to operate. Make sure youre running the command with appropriate privileges (e.g., as root or using sudo).
- Invalid Command − Verify that the command youre using is valid and supported by the blockdev command.
Conclusion
The blockdev command in Linux allows you to interact with block devices using various ioctls from the command line. With the examples given in this tutorial, you can now start exploring the blockdev command.