Linux Online - Raw devices

来源:百度文库 编辑:神马文学网 时间:2024/04/30 03:31:02

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.