I am working at an OS independent file manager, in C. I managed to copy files, links and directories, but I am not sure how to copy devices. I would appreciate any help.
6 Answers
To create a device file, use the mknod(2)
syscall. The struct stat
structure will give you the major and minor device numbers for an existing device in st_rdev
.
Having said that, there is little value in "copying" a device because a device doesn't contain anything useful. The major and minor numbers are specific to the OS on which they exist.
-
Thanks! I don't know a lot about Linux specific stuff, so maybe I am wrong, but I would think that the use for this is that if someone wants to back up a part of their system they would want the devices to be backed up too (in case they delete the original directory and then they want to restore it). Is there a flaw in this logic? Because if there is really no need at all to copy them, I would rather not.– RaduOct 1, 2011 at 1:26
-
1@Radu You never know how a system is set up, if someone wants to move/copy a whole directory somewhere else, they expect everything in that directory to be moved - even if its device files. And e.g. named pipes and unix domain sockets are created by the same mechanisms - those might be vital to the operation of whatever was in that directory. So better copy it.– nosOct 1, 2011 at 1:33
-
Where exactly do I find the minor and major device numbers in the
dirent
structure? Whichdirent
fields specify that?– RaduOct 1, 2011 at 2:25 -
-
Got everything working, thanks! It was actually much easier than the mknod(2) man page instructions (which were very ambiguous). Just had to copy both mode and dev from the source file stats structure as arguments to mknod().– RaduOct 1, 2011 at 9:03
It's not really a useful feature, IMHO. tar(1) needs to be able to do it as part of backing up a system, and setup programs need to be able to create them for you when setting up your system, but few people need to deal directly with device files these days.
Also, modern Linux systems are going to dynamic device files, created on the fly. You plug in a device and the device files appear; you unplug it and they disappear. There is really no use in being able to copy these dynamic files.
-
My reason for implementing this is backup purposes. Maybe someone wants to copy some dir structure and want to restore it later as it was.– RaduOct 1, 2011 at 1:27
-
steveha gave you the counter-reason for that. They're created on the fly. Oct 1, 2011 at 1:31
-
Well, I had some devices in /lib/udev/devices which I deleted by mistake during my tests. They did not reappear.– RaduOct 1, 2011 at 1:34
-
@Radu, what version of Linux are you using? And did you try removing the hardware and re-plugging it, or for non-removable hardware, did you try rebooting your system?– stevehaOct 1, 2011 at 2:45
-
I am using Fedora 14. I don't know what hardware that device belonged to. I rebooted my computer and they did not reappear.– RaduOct 1, 2011 at 2:50
dd is your friend (man dd)
dd if=/dev/sda1 of=/some_file_or_equally_sized_partition bs=8192
if you want to copy the device-file itself, do this:
cp -p device-filename new-filename
e.g.:
cp -p /dev/sda1 /tmp/sda1
those are both equivalent device files, and can be used to access the device.
If you're want to do this from C, use mknod() .. see "man 2 mknod"
-
1I am not looking to copy the content of the device, I am looking to copy the device itself.– RaduOct 1, 2011 at 3:21
-
1
-
-
-
2On Linux
cp -p
did not copy the device block special file for me, butcp -r
did provided that I was root. Jan 24, 2019 at 15:22
This might be useful
cp -dpR devices /destination_directory
cp -dpR console /mnt/dev
You don't. Just filter them out of the view such that it can't be done.
Use the stat function to determine the file type.
Check if you've the udev package, if you do, chances are that devices are generated on the fly, from the package description:
udev - rule-based device node and kernel event manager
udev is a collection of tools and a daemon to manage events received from the
kernel and deal with them in user-space. Primarily this involves creating and
removing device nodes in /dev when hardware is discovered or removed from the
system.
Events are received via kernel netlink messaged and processed according to
rules in /etc/udev/rules.d and /lib/udev/rules.d, altering the name of the
device node, creating additional symlinks or calling other tools and programs
including those to load kernel modules and initialise the device.
-
Well, I don't want to make assumptions about what users have installed and how their systems are set up. I just want someone moving a directory to move everything in it, just in case.– RaduOct 1, 2011 at 1:43