2020-10-12 02:13:08

by Mikhail Gavrilov

[permalink] [raw]
Subject: [question] What happens when dd writes data to a missing device?

Hi folks!
I have a question.
What happens when dd writes data to a missing device?

For example:
# dd if=/home/mikhail/Downloads/Fedora-Workstation-Live-x86_64-Rawhide-20201010.n.0.iso
of=/dev/adb

Today I and wrongly entered /dev/adb instead of /dev/sdb,
and what my surprise was when the data began to be written to the
/dev/adb device without errors.

But my surprise was even greater when cat /dev/adb started to display
the written data.

I have a question:
Where the data was written and could it damage the stored data in
memory or on disk?


--
Best Regards,
Mike Gavrilov.


2020-10-12 02:15:09

by Sven-Haegar Koch

[permalink] [raw]
Subject: Re: [question] What happens when dd writes data to a missing device?

On Mon, 12 Oct 2020, Mikhail Gavrilov wrote:

> I have a question.
> What happens when dd writes data to a missing device?
>
> For example:
> # dd if=/home/mikhail/Downloads/Fedora-Workstation-Live-x86_64-Rawhide-20201010.n.0.iso
> of=/dev/adb
>
> Today I and wrongly entered /dev/adb instead of /dev/sdb,
> and what my surprise was when the data began to be written to the
> /dev/adb device without errors.
>
> But my surprise was even greater when cat /dev/adb started to display
> the written data.
>
> I have a question:
> Where the data was written and could it damage the stored data in
> memory or on disk?

If the device node /dev/adb does not exist (most likely udev case when
you don't have the device/no module loaded for it) then dd as root will
just create a normal file inside the /dev ramdisk.

Only if the device node exists but is not handled then something else
like an open error will happen.

c'ya
sven-haegar

--
Three may keep a secret, if two of them are dead.
- Ben F.

2020-10-12 02:17:07

by Al Viro

[permalink] [raw]
Subject: Re: [question] What happens when dd writes data to a missing device?

On Mon, Oct 12, 2020 at 12:46:03AM +0500, Mikhail Gavrilov wrote:
> Hi folks!
> I have a question.
> What happens when dd writes data to a missing device?
>
> For example:
> # dd if=/home/mikhail/Downloads/Fedora-Workstation-Live-x86_64-Rawhide-20201010.n.0.iso
> of=/dev/adb
>
> Today I and wrongly entered /dev/adb instead of /dev/sdb,
> and what my surprise was when the data began to be written to the
> /dev/adb device without errors.
>
> But my surprise was even greater when cat /dev/adb started to display
> the written data.
>
> I have a question:
> Where the data was written

Into a file called "/dev/adb", of course.

> and could it damage the stored data in
> memory or on disk?

Why would it? There's nothing magical about /dev - the same thing happened
as if you said
dd if=/home/mikhail/Downloads/whatever.iso of=/tmp/adb
or, for that matter, of=/home/mikhail/copy-of-that-damn-iso - it had been
asked to write into file with that name if it already existed or to create it
and write into it if it didn't exist... So it had created a file in /dev
with name adb and stored a copy into it. You might run out of space if the
file had been large enough, but that's about it...

Try ls -l /dev/adb /dev/sdb and compare these two - sdb will be something
like
brw-rw---- 1 root disk 8, 16 ....
and adb -
-rw-rw---- 1 root <some group> <size of that sucker> ...

Block device and regular file respectively... man mknod if you are
curious about device nodes and creating them manually - usually that's
done by scripts called by udev when it discovers devices, but that's
what they boil down to in the end.

Again, there's nothing magical about /dev or the names of specific
device nodes created in it - it's just the usual place to put that
stuff into, but that's it; you could call mknod(2) to create such
device nodes in any directory, using any names.

2020-10-12 02:48:56

by Douglas Gilbert

[permalink] [raw]
Subject: Re: [question] What happens when dd writes data to a missing device?

On 2020-10-11 3:46 p.m., Mikhail Gavrilov wrote:
> Hi folks!
> I have a question.
> What happens when dd writes data to a missing device?
>
> For example:
> # dd if=/home/mikhail/Downloads/Fedora-Workstation-Live-x86_64-Rawhide-20201010.n.0.iso
> of=/dev/adb
>
> Today I and wrongly entered /dev/adb instead of /dev/sdb,
> and what my surprise was when the data began to be written to the
> /dev/adb device without errors.
>
> But my surprise was even greater when cat /dev/adb started to display
> the written data.
>
> I have a question:
> Where the data was written and could it damage the stored data in
> memory or on disk?

Others have answered your direct question.

You may find 'oflag=nocreat' helpful if you (or others) do _not_ want
a regular file created in /dev ; for example: if you have misspelt a
device name.
That flag may also be helpful in unstable systems (e.g. where device
nodes are disappearing and re-appearing) as it can be a real pain
if you manage to create a regular file with a name like /dev/sdc when
the disk usually occupying that node is temporarily offline. When
that disk comes back online then regular file '/dev/sdc' will stop
device node '/dev/sdc' from being created.

The solution is to remove the regular file /dev/sdc and you probably
need to power cycle that disk. If this becomes a regular event then
'oflag=nocreat' is your friend [see 'man dd' for a little more
information, it really should be expanded].

Doug Gilbert