-3

I was reading about device files in FreeBSD, but my understanding is not great. To practice, I thought up the following scenario. I suspect that the problem can be solved using device files.

Imagine that I am a part time spy and I need to generate some one-time pad. I could go to my secure FreeBSD box and write some /dev/urandom to a pad file, knowing that the randomness coming from this source is Good Enough (tm). My flash drive with pad file will be delivered to my future contact.

However, seeing as this drive will only contain one file full of random bytes, it seems a waste to write a whole filesystem to the disk. Why not fill the USB to the brim with random data?

My question: How can I achieve my aim of reading and writing raw data from and to a disk? Should I point dd at the disk's device file?

  • Downvote => Bad question. What's wrong with it? – bimmo May 12 '16 at 7:30
  • Because the answer is in your question: dd (or whatever command to the device file). – Ouki May 12 '16 at 7:42
  • Surely there are other ways of interacting with device files? I have read about the idea of a device file, but nowhere can I find an actual protocol for writing to device files. – bimmo May 12 '16 at 7:45
  • If i have understood you question correctly then the below command can be used. dd if=/dev/urandom of=sample.txt bs=1G count=1 were 1G can be your whole disk size. – Mongrel May 12 '16 at 7:49
  • What happens if you echo some text to the device file? What data does dd actually send to the device file? – bimmo May 12 '16 at 7:54
1

Yes, you can treat the device file as a raw device file, and read/write data from/to it using the same APIs used to access normal files. In most cases, you can use dd, or simply cat data to/from the device file.

Keep in mind that there are several practical differences between raw devices and real files on a filesystem:

  • Devices have a fixed size. Writing less data will leave the remaining space intact. You cannot determine how much data you actually wrote unless you have some other way to convey that information.
  • Devices have no permissions. Device files representing devices have permissions, but those permissions do not travel with the device itself, they are simply assigned by the system to which the device is connected. If you chmod a device file representing a USB drive, unplug it, and connect it to another (or the same) system, the permissions changes will not persist.
  • Devices have no name, or at least no user-changeable name. Your OS will assign whatever device node name it feels like to your device. Things like volume names are stored in filesystems or partition tables, and there is no such thing when you access the entire device as raw storage.
  • Most OSes will not let random non-root users access device files, while some may allow non-root local users to mount things like USB drives.

(Note: I come from a Linux background, but these concepts are pretty much identical on any UNIX-like system)

  • I gave dd a go but it chokes on the device files. I looked at the manual pages and it seems that I have to wrangle GEOM. Any tips? – bimmo May 12 '16 at 21:48
  • Looks like FreeBSD has protection mechanisms in place to prevent you from writing to devices that are currently in use. Make sure you have unmounted any filesystems on the device. If you have no other option, it looks like setting the kern.geom.debugflags sysctl to 0x10 should allow you to bypass the safety. – marcan May 13 '16 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.