2

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 6

6

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.

5
  • 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.
    – Radu
    Oct 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.
    – nos
    Oct 1, 2011 at 1:33
  • Where exactly do I find the minor and major device numbers in the dirent structure? Which dirent fields specify that?
    – Radu
    Oct 1, 2011 at 2:25
  • Sorry, I guess it's in struct stat. I'll update the answer. Oct 1, 2011 at 3:49
  • 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().
    – Radu
    Oct 1, 2011 at 9:03
0

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.

7
  • 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.
    – Radu
    Oct 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.
    – Radu
    Oct 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?
    – steveha
    Oct 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.
    – Radu
    Oct 1, 2011 at 2:50
0

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"

5
  • 1
    I am not looking to copy the content of the device, I am looking to copy the device itself.
    – Radu
    Oct 1, 2011 at 3:21
  • 1
    cp -p device-filename new-filename will just do fine on UNIX
    – Tilo
    Oct 1, 2011 at 5:21
  • really a bad advice. Try to copy /dev/zero in this way
    – ayvango
    Jul 2, 2016 at 15:28
  • I did not give advice to copy /dev/zero
    – Tilo
    Jul 3, 2016 at 5:37
  • 2
    On Linux cp -p did not copy the device block special file for me, but cp -r did provided that I was root. Jan 24, 2019 at 15:22
0

This might be useful

cp -dpR devices /destination_directory

cp -dpR console /mnt/dev

2
  • cp: cannot create special file Is all i get
    – Achim
    Dec 6, 2017 at 0:02
  • @Achim you need root privs Jan 24, 2019 at 15:17
-1

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.

0
-1

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.   
1
  • 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.
    – Radu
    Oct 1, 2011 at 1:43

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.