简体   繁体   English

从Linux上的C程序直接访问没有FS的硬盘

[英]Direct access to hard disk with no FS from C program on Linux

I want to access the whole hard disk directly from a C program. 我想直接从C程序访问整个硬盘。 There's no FS on it and never's gonna be one. 它上面没有FS,永远不会是一个。

I just want to open /dev/sda (for example) and do I/O at the block/sector level of the disk. 我只想打开/ dev / sda(例如)并在磁盘的块/扇区级别执行I / O.

I'm planning to write some programs for learning C programming in the Linux environment (I know C language, Python, Perl and Java) but lack confidence with the Linux environment. 我打算在Linux环境中编写一些用于学习C编程的程序(我知道C语言,Python,Perl和Java),但对Linux环境缺乏信心。

For my learning purposes I'm thinking about playing with kyoto-cabinet and saving the value corresponding to the computed hash directly into a "block/sector" of the hard disk, recording the pair: "hash, block/sector reference" into a kyoto-cabinet hash database file. 为了我的学习目的,我正在考虑使用kyoto-cabinet并将与计算的哈希相对应的值直接保存到硬盘的“块/扇区”中,记录该对:“哈希,块/扇区引用”到一个京都柜哈希数据库文件。

I don't know if this is feasible using standard CI/O functions or otherwise I'd have to write a "device driver" or something like... 我不知道使用标准CI / O功能是否可行,否则我不得不写一个“设备驱动程序”或类似的东西......

As mentioned elsewhere, under *NIX systems, block devices like /dev/sda can be accessed as plain files. 如其他地方所述,在* NIX系统下,像/dev/sda这样的块设备可以作为普通文件访问。 Note that if file system is mounted from the device, opening it as file for writing would fail. 请注意,如果从设备挂载文件系统,则将其作为写入文件打开将失败。

If you want to play with block devices, I would advise to first use the loop device , which presents a plain file as a block device. 如果你想玩块设备,我建议首先使用loop device ,它将普通文件作为块设备。 For example: 例如:

dd if=/dev/zero of=./loop_file_10MB bs=1024 count=10K
losetup /dev/loop0 $PWD/loop_file_10MB

After that, /dev/loop0 would behave as if it was a block device, but all information written would be stored in the file. 之后, /dev/loop0行为就好像它是一个块设备,但所有写入的信息都将存储在文件中。

由于驱动器的设备文件(例如/dev/sda )是块设备,这意味着您可以像普通文件一样打开,查找和使用该文件。

Yes, as others have noted, you can simply open the block device. 是的,正如其他人所说,你可以简单地打开块设备。

However, it's a really good idea to do IO (writes anyway) on block boundaries and whole blocks. 但是,在块边界和整个块上执行IO(无论如何写入)是一个非常好的主意。 You can use something like pread() and pwrite() to do these IO, or mmap some or all of the device. 您可以使用pread()和pwrite()之类的东西来执行这些IO,或者mmap部分或全部设备。

There are a bunch of ioctls which can be used, see "man sd" for some more info. 有一堆可以使用的ioctl,请参阅“man sd”获取更多信息。 They don't seem to all be documented in the same place. 它们似乎并未在同一个地方记录。

In linux/fs.h BLKROSET and a bunch of other ioctls are defined, you have to look around to find out how to use them. 在linux / fs.h中定义了BLKROSET和一堆其他ioctl,你必须四处寻找如何使用它们。 You can do useful things like find out how big the device is, and what the block size is. 您可以执行有用的操作,例如查看设备的大小以及块大小。

The source code of the util-linux-ng package is your friend, it contains examples. util-linux-ng软件包的源代码是你的朋友,它包含了一些例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM