打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Linux Online - Raw devices

Chapter 11. Raw devices

A raw device can be bound to an existing block device (e.g. a disk) and beused to perform "raw" IO with that existing block device. Such "raw" IObypasses the caching that is normally associated with block devices.Hence a raw device offers a more "direct" route to the physical deviceand allows an application more control over the timing of IO to thatphysical device. This makes raw devices suitable for complex applicationslike Database Management Systems that typically do their own caching.

Raw devices are character devices (major number 162). The firstminor number (i.e. 0) is reserved as a control interface and is usuallyfound at/dev/rawctl. A utility called raw (see man raw) can be used to bind a rawdevice to an existing block device. These "existing block devices" may bedisks or cdroms/dvds whose underlying interface can be anything supportedby Linux (e.g. IDE/ATA or SCSI).

A sequence of commands listing the raw devices and then bindinga SCSI disk partition followed by binding the whole disk lookslike this on my system:

$ ls -lR /dev/raw*
crw-r--r-- 1 root root 162, 0 Dec 6 06:54 /dev/rawctl

/dev/raw:
total 0
crw-r--r-- 1 root root 162, 1 Dec 6 06:54 raw1
crw-r--r-- 1 root root 162, 2 Dec 6 06:54 raw2
crw-r--r-- 1 root root 162, 3 Dec 6 06:54 raw3
crw-r--r-- 1 root root 162, 4 Dec 6 06:54 raw4
$
$ raw -qa
$
$ raw /dev/raw/raw1 /dev/sda3
/dev/raw/raw1: bound to major 8, minor 3
$ raw /dev/raw/raw2 /dev/sda
/dev/raw/raw2: bound to major 8, minor 0
$ raw -qa
/dev/raw/raw1: bound to major 8, minor 3
/dev/raw/raw2: bound to major 8, minor 0

The normal array of system calls for character devices are available onraw devices. The size of the transfer for read(2) and write(2) must bean integral multiple of the physical device's block size. For a diskthis will be its sector size which is normally 512 bytes. The data buffergiven to read() and write() system calls must be aligned to the blocksize. The lseek(2) call needs to align its file read/write offset to a blockboundary as well. The pread(3) call (see man pread)combines a read() and an lseek() and can be useful with raw devices (ditto withpwrite() ). Care should be taken with offsets greater than 2 GB (or perhaps4 GB) on 32 bit architectures where the "off_t" type is 32 bits long.One solution is to use the _llseek() call (see man llseek).

Unix utilities such as recent versions of dd andlmdd (from the lmbench suite of programs) can be usedto move data to and from "raw" devices as they meet the above-mentionedblock alignment requirements. Recent versions of the sg_ddcommand in the sg_utils package can access both raw and sg devices.

Also note that if the physical device has an odd number of sectors (asshown by blockdev --getsize /dev/raw/raw*), the lastsector will not be accessible using raw IO.

If a block device is being accessed via a bound raw device and also via its normal block interface then there is no cache coherency between the two access mechanisms. For example if /dev/sda1 was both mounted and being accessed via a bound raw device then there could be data inconsistencies.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
linux下创建裸设备及Oracle表空间
Linux音频驱动-ALSA概述(转)
Linux系统中/dev/mtd与/dev/mtdblock的区别,即MTD字符设备和块设备的区别
如何挂载qcow2格式的镜像文件(how to mount a qcow2 image) – 笑遍世界
Linux系统设备(device)的major和minor number
Input event驱动
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服