[Lazarus] How can I write to arbitrary position on device

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[Lazarus] How can I write to arbitrary position on device

Free Pascal - Lazarus mailing list

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)


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] How can I write to arbitrary position on device

Free Pascal - Lazarus mailing list
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?

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] How can I write to arbitrary position on device

Free Pascal - Lazarus mailing list
On 2017-04-13 09:15, Mark Morgan Lloyd via Lazarus wrote:

> 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)


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] How can I write to arbitrary position on device

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
On 13/04/17 07:30, Mark Morgan Lloyd via Lazarus wrote:

> 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]
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus