Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2017-04

How can I write to arbitrary position on device

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Carlos E. R.

Hi,

I'm new on this list, so if I should post this to a different place, just tell me.

I want to find a function that I can use on Linux to write a memory block to an arbitrary position on a device, say, /dev/sda or /dev/sda5 .

I can do that on an opened file with seek() and write(), but apparently only on files. I need accessing the raw disk device. So currently I call 'dd' instead.

I also had problems with blockread/write: it failed reading more than one megabyte.

Cheers Carlos E. R. (from 42.2 x86_64 "Malachite" at Telcontar)

Reply by email

Mark Morgan Lloyd
to Carlos E. R.
Carlos E. R. wrote 10 lines
Hi, I'm new on this list, so if I should post this to a different place, just tell me. I want to find a function that I can use on Linux to write a memory block to an arbitrary position on a device, say, /dev/sda or /dev/sda5 . I can do that on an opened file with seek() and write(), but apparently only on files. I need accessing the raw disk device. So currently I call 'dd' instead. I also had problems with blockread/write: it failed reading more than one megabyte.

That's interesting, I need to look at some medium testing stuff over the next few days and was assuming I'd need to check the dd sources. Are you running as root to get direct access to the device?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Carlos E. R.
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 15 lines
On 12/04/17 21:30, Carlos E. R. via Lazarus wrote: > Hi, > I'm new on this list, so if I should post this to a different place, > just tell me. > I want to find a function that I can use on Linux to write a memory > block to an arbitrary position on a device, say, /dev/sda or /dev/sda5 . > I can do that on an opened file with seek() and write(), but apparently > only on files. I need accessing the raw disk device. So currently I call > 'dd' instead. > I also had problems with blockread/write: it failed reading more than > one megabyte. That's interesting, I need to look at some medium testing stuff over the next few days and was assuming I'd need to check the dd sources. Are you running as root to get direct access to the device?

Thanks for responding. :-)

I had and idea, tried it, and it worked. I simply assign:

   assign(Fout, '/dev/sdc');
   Reset (Fout);

The file can be a file of bytes, or in my case, a typed file (of an array). Works perfectly, I can write chunks at 180 MiB/s. :-)

For writing to some position, I assume I could use Seek(). I haven't tried.

Yes, of course, even for testing you need being root to run the code. So the IDE runs as my user, the code in another terminal as root. Scary, because I write directly to /dev/sdc (I'm filling the disk with random data fast, prior to encrypting it. 7 hours for a 4TB disk).

Perhaps I could instead chmod or chown the device [...] Yes, it works, better! It is undone on reboot, but that doesn't matter.

Cheers / Saludos, Carlos E. R. (from 42.2 x86_64 "Malachite" at Telcontar) -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 181 bytes Desc: OpenPGP digital signature URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20170413/9901abb2/attachment.sig>

Reply by email

Mark Morgan Lloyd
to Mark Morgan Lloyd: That's interesting, I need to look at some medium testing stuff over the next few days…
Mark Morgan Lloyd wrote 11 lines
On 12/04/17 21:30, Carlos E. R. via Lazarus wrote:> Hi,> I'm new on this list, so if I should post this to a different place,> just tell me.> I want to find a function that I can use on Linux to write a memory> block to an arbitrary position on a device, say, /dev/sda or /dev/sda5 .> I can do that on an opened file with seek() and write(), but apparently> only on files. I need accessing the raw disk device. So currently I call> 'dd' instead.> I also had problems with blockread/write: it failed reading more than> one megabyte. That's interesting, I need to look at some medium testing stuff over the next few days and was assuming I'd need to check the dd sources. Are you running as root to get direct access to the device?

In case this is useful to anybody at any point: I was filling a device with a pattern and then reading it back, and found that the read time depended crucially on the amount of RAM available (relative to the size of device I was testing). This turned out to be because I was relying on Reset() with FileMode = 2 to open the file R/W, but this was causing the Linux kernel to buffer as much as possible in RAM. Switching to using Rewrite(), then closing and reopening with Reset() only if needed, resulted in predictable performance.

So I think that the lesson is that Reset() should only be used for R/W files when the write and read operations are expected to be comparatively close to each other in time and position.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email