从Linux上的C程序直接访问硬盘而无需FS [英] Direct access to hard disk with no FS from C program on Linux
问题描述
我想直接从C程序访问整个硬盘.上面没有FS,永远也不会成为一个.
I want to access the whole hard disk directly from a C program. There's no FS on it and never's gonna be one.
我只想打开/dev/sda(例如)并在磁盘的块/扇区级别上执行I/O.
I just want to open /dev/sda (for example) and do I/O at the block/sector level of the disk.
我打算编写一些程序来学习Linux环境中的C编程(我知道C语言,Python,Perl和Java),但是对Linux环境缺乏信心.
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.
出于学习目的,我正在考虑使用kyoto-cabinet,并将与计算得出的哈希对应的值直接保存到硬盘的块/扇区"中,记录该对:哈希,块/扇区参考" "放入京都橱柜哈希数据库文件.
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.
我不知道使用标准C I/O函数是否可行,否则我不得不编写设备驱动程序"或类似的内容...
I don't know if this is feasible using standard C I/O functions or otherwise I'd have to write a "device driver" or something like...
推荐答案
如在其他地方提到的,在* NIX系统下,像/dev/sda
这样的块设备可以作为纯文件访问.请注意,如果从设备挂载了文件系统,则将其作为用于写入的文件打开将失败.
As mentioned elsewhere, under *NIX systems, block devices like /dev/sda
can be accessed as plain files. Note that if file system is mounted from the device, opening it as file for writing would fail.
如果您想使用块设备,我建议您首先使用 loop device
,将普通文件显示为块设备.例如:
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. For example:
dd if=/dev/zero of=./loop_file_10MB bs=1024 count=10K
losetup /dev/loop0 $PWD/loop_file_10MB
此后,/dev/loop0
的行为就好像它是一个块设备一样,但是所有写入的信息都将存储在文件中.
After that, /dev/loop0
would behave as if it was a block device, but all information written would be stored in the file.
这篇关于从Linux上的C程序直接访问硬盘而无需FS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!