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

后端 未结 3 2189
天涯浪人
天涯浪人 2020-11-30 06:18

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.

I just want to open /dev/sda (for example) and do I

3条回答
  •  情书的邮戳
    2020-11-30 07:07

    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.

    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
    

    After that, /dev/loop0 would behave as if it was a block device, but all information written would be stored in the file.

提交回复
热议问题