#Bash 的 blockdev 命令
blockdev [OPTION]... COMMAND... DEVICE...
功能
从命令行对块设备调用 ioctl。
类型
可执行文件(/usr/sbin/blockdev),属于 util-linux。
参数
OPTIONS选项:-q- 不打印任何信息-v- 打印详细信息--report- 打印指定设备的报告-h,--help- 显示帮助-V,--version- 显示版本
COMMAND- 要执行的命令--flushbufs- 冲洗缓冲区--getalignoff- 获取设备对齐偏移量(alignment offset),用于确定读写是否对齐--getbsz- 获取设备的块大小;单位是字节--getdiscardzeroes- 检查设备在执行 discard 操作时是否自动清零--getdiskseq- 获取设备的序列号(disk sequence number)--getfra- 获取前置读请求数量(front read ahead)--getiomin- 获取最小 I/O 单位(最小 I/O size)--getioopt- 获取设备的最优 I/O 大小--getmaxsect- 获取设备最大扇区数(每次 I/O 可处理的最大扇区数)--getpbsz- 获取物理块大小(physical block size)--getra- 获取读提前(read ahead)扇区数--getro- 检查设备是否只读(read-only)--getsize64- 获取设备大小(以字节为单位,64 位)--getsize- 获取设备大小(以 512 字节扇区为单位)--getss- 获取设备的扇区大小(sector size)--getsz- 获取设备扇区数量(与 --getsize 类似)--rereadpt- 重新读取分区表(re-read partition table)--setbsz bytes- 设置设备块大小(block size)--setfra sectors- 设置前置读(front read ahead)扇区数;以 512 字节的扇区为单位--setra sectors- 设置预读(read ahead)扇区数;以 512 字节的扇区为单位--setro- 将设备设置为只读--setrw- 将设备设置为可读写
DEVICE- 要操作的设备
#示例
查看设备大小(扇区数和字节数)
# 查看 /dev/sda 的扇区数
sudo blockdev --getsz /dev/sda
# 查看 /dev/sda 的大小(字节)
sudo blockdev --getsize64 /dev/sda
查看设备的扇区大小和块大小
# 扇区大小
sudo blockdev --getss /dev/sda
# 块大小
sudo blockdev --getbsz /dev/sda
检查设备是否只读
sudo blockdev --getro /dev/sda
# 输出 1 表示只读,0 表示可读写
设置设备为只读或可读写
# 设置只读
sudo blockdev --setro /dev/sda
# 设置可读写
sudo blockdev --setrw /dev/sda
查看和设置读提前(read-ahead)
# 查看 read-ahead 扇区数
sudo blockdev --getra /dev/sda
# 设置 read-ahead 为 256 扇区
sudo blockdev --setra 256 /dev/sda
冲洗缓冲区(flush buffers)
sudo blockdev --flushbufs /dev/sda
- 这个命令会把所有缓存的写操作刷新到磁盘上,类似
sync,确保数据安全。
重新读取分区表
sudo blockdev --rereadpt /dev/sda
- 当你修改了分区表(如用
fdisk或parted)后,可以用这个命令让内核重新识别分区。
综合示例:设置 read-ahead、查看大小并刷新缓冲区
sudo blockdev --setra 128 /dev/sda
sudo blockdev --getsize64 /dev/sda
sudo blockdev --flushbufs /dev/sda
#推荐阅读
#手册
BLOCKDEV(8) System Administration BLOCKDEV(8)
NAME
blockdev - call block device ioctls from the command line
SYNOPSIS
blockdev [-q] [-v] command [command...] device [device...]
blockdev --report [device...]
blockdev -h|-V
DESCRIPTION
The utility blockdev allows one to call block device ioctls from the
command line.
OPTIONS
-q
Be quiet.
-v
Be verbose.
--report
Print a report for the specified device. It is possible to give
multiple devices. If none is given, all devices which appear in
/proc/partitions are shown. Note that the partition StartSec is in
512-byte sectors.
-h, --help
Display help text and exit.
-V, --version
Print version and exit.
COMMANDS
It is possible to give multiple devices and multiple commands.
--flushbufs
Flush buffers.
--getalignoff
Get alignment offset.
--getbsz
Print the blocksize in bytes. This size does not describe device
topology. It’s the size used internally by the kernel and it may be
modified (for example) by filesystem driver on mount.
--getdiscardzeroes
Get discard zeroes support status.
--getdiskseq
Get disk sequence number.
--getfra
Get filesystem readahead in 512-byte sectors.
--getiomin
Get minimum I/O size.
--getioopt
Get optimal I/O size.
--getmaxsect
Get max sectors per request.
--getpbsz
Get physical block (sector) size.
--getra
Print readahead (in 512-byte sectors).
--getro
Get read-only. Print 1 if the device is read-only, 0 otherwise.
--getsize64
Print device size in bytes.
--getsize
Print device size (32-bit!) in sectors. Deprecated in favor of the
--getsz option.
--getss
Print logical sector size in bytes - usually 512.
--getsz
Get size in 512-byte sectors.
--rereadpt
Reread partition table
--setbsz bytes
Set blocksize. Note that the block size is specific to the current
file descriptor opening the block device, so the change of block
size only persists for as long as blockdev has the device open, and
is lost once blockdev exits.
--setfra sectors
Set filesystem readahead (same as --setra on 2.6 kernels).
--setra sectors
Set readahead (in 512-byte sectors).
--setro
Set read-only. The currently active access to the device may not be
affected by the change. For example, a filesystem already mounted
in read-write mode will not be affected. The change applies after
remount.
--setrw
Set read-write.
AUTHORS
blockdev was written by Andries E. Brouwer and rewritten by Karel Zak.
REPORTING BUGS
For bug reports, use the issue tracker at
https://github.com/util-linux/util-linux/issues.
AVAILABILITY
The blockdev command is part of the util-linux package which can be
downloaded from Linux Kernel Archive
<https://www.kernel.org/pub/linux/utils/util-linux/>.
util-linux 2.39.3 2023-12-01 BLOCKDEV(8)