LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-27-2024, 04:25 PM   #1
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Rep: Reputation: 78
Copy Block Device with CP command?


Can you copy a block device (eg; /dev/sda1) using the standard cp command?
 
Old 02-27-2024, 04:50 PM   #2
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,985

Rep: Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626
Well, yes and no I'd think.

cp copies files. Usually one may have a file structure and that device might be mounted to a point. So the question kind of confuses.
 
Old 02-27-2024, 05:12 PM   #3
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
a block device, /dev/sda, will be the raw hw device.
a mount point (entrance to a filesystem, etc) will be on a block device

I thought cp command only worked on regular files (-) and dirs (d), but not special files like block devices, character files, named pipes, etc.

Don't files and dirs have start and end to them, and cp works on that. A block device has no "start" and "end" within a block range that spans 0 to X (however big the device is). How would cp command know where the start and end is of a block device?

Last edited by Linux_Kidd; 02-27-2024 at 05:15 PM.
 
Old 02-27-2024, 05:29 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,708

Rep: Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898
"In linux everything is a file"

The debian installation guide uses cp to copy an ISO file to a USB device i.e. /dev/sdx so there should be no reason you can not use it to copy in reverse and it does work.
 
Old 02-27-2024, 05:54 PM   #5
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by michaelk View Post
"In linux everything is a file"

The debian installation guide uses cp to copy an ISO file to a USB device i.e. /dev/sdx so there should be no reason you can not use it to copy in reverse and it does work.
# cp debian.iso /dev/sdX
# sync

cp there though is reading a file from a filesystem, the iso file has a start and end to it.

When you make block device the source (cp /dev/sdx ./iso.img), how does cp know where the start and end of the block device is?
 
Old 02-27-2024, 06:46 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,708

Rep: Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898
Doesn't the destination also need a start/end. I am not a kernel person but a block device is basically an abstraction layer between the user and the I/O system calls. I'm guessing the maybe the inode contains that info.
 
Old 02-27-2024, 07:42 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,796

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
cp does an open() on a file or device, and then read() the data until it gets an EOF. In the case of a device the device driver gives the data and the EOF.

Last edited by MadeInGermany; 02-27-2024 at 07:44 PM.
 
Old 02-27-2024, 11:19 PM   #8
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by MadeInGermany View Post
cp does an open() on a file or device, and then read() the data until it gets an EOF. In the case of a device the device driver gives the data and the EOF.
That's interesting, special file devices have just 1 inode, and the inode always says "size:0", so what then does cp read() actually read if the inode info says "no size" and "no blocks"?



Code:
$ stat /dev/sda
      File: /dev/sda
      Size: 0               Blocks: 0          IO Block: 4096   block special file
Device: 6h/6d   Inode: 14628       Links: 1     Device type: 8,0
Code:
[root@localhost dev]# file urandom
urandom: character special (1/9)
[root@localhost dev]# stat urandom
  File: urandom
  Size: 0               Blocks: 0          IO Block: 4096   character special fi
le
Device: 6h/6d   Inode: 16          Links: 1     Device type: 1,9
Access: (0666/crw-rw-rw-)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 1969-12-31 17:00:00.060000000 -0700
Modify: 1969-12-31 17:00:00.060000000 -0700
Change: 1969-12-31 17:00:00.060000000 -0700
 Birth: -
Code:
[root@localhost ~]# stat bench.py
  File: bench.py
  Size: 114             Blocks: 8          IO Block: 4096   regular file
Device: ch/12d  Inode: 83          Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-12-26 03:19:19.234181000 -0700
Modify: 2020-12-26 03:19:19.234181000 -0700
Change: 2024-02-27 22:11:30.788000000 -0700
 Birth: -

Last edited by Linux_Kidd; 02-27-2024 at 11:26 PM.
 
Old 02-28-2024, 12:21 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,796

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
In the device inode the major/minor number pair points to a device driver.
The file access calls like open() read() close() ioctl() are redirected to the device driver.
 
Old 02-28-2024, 03:39 AM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
What's wrong with trying it yourself and see what happens ?. Great way to learn.
 
Old 02-28-2024, 11:28 AM   #11
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
I can try, yes.

I was just trying to get a better understanding of 'cp' command.
I thought it only worked on inode meta info filetype of reg file or directory.

Is there any actual file with data for items in /dev ? No should be the answer, but for those items to exist there needs to be an inode.

I may just be confused about 'cp' and special "files" (block char pipe, etc).

cp increments read() by bytes, dd increments read() by blocks ?

I actually don't know the correct answer, hence why I am asking.

Last edited by Linux_Kidd; 02-28-2024 at 11:30 AM.
 
Old 02-28-2024, 11:48 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
cp itself does not care about inodes. It has 2 file descriptors, one for reading and one for writing.
Anything which can be read can be copied, including a partition or a full raw disk.
It is not important if the fd is a real file on a filesystem or a virtual file on a virtual filesystem or a remote whatever on a remote real or virtual host, the kernel itself will manage the read and write operation (using the appropriate driver/interface).
From this point of view dd and cp are quite similar, just copy is mainly used for real filesystems and dd is mainly used for other kind of data.
 
Old 02-28-2024, 06:42 PM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Indeed, cp and dd do much the same thing. It's just that cp has options like recursing into directories, handling file attributes, making links instead of copying, not overwriting existing files, etc., while dd has options suited for copying devices, such as setting I/O block sizes, handling errors, bypassing the kernel's buffer cache, and (especially useful for large transfers) showing the progress of the operation.

Now dd does have the reputation of being dangerous, but that's just because it is typically used to do dangerous things, like overwriting devices. You can say that dd is dangerous because you could easily zero out the wrong device, but really you could do exactly the same thing with cp, it's just that usually you wouldn't be doing that.
 
1 members found this post helpful.
Old 02-28-2024, 07:43 PM   #14
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
I will ask a new way.

When a util opens a file and does a read(), the abtraction layer between kernel and device (a driver) has to handle feeding the read() with data, but the kernel holds a mapping of that file, the file is all over the disk (fragmented), but the process just receives the file data, has no clue the where it came from, or how fragmented it was. At some point in the read() there will be an eof, and data will stop flowing back to the process.

read(file) <-->kernel<-->major,minor<-->mapping<-->disk
so start of file, pointers to next block, ... , last block(eof)
That's fairly straghtforward.

When a device block is opened, does the kernel handle read() differently? It must if we are expecting the read() to start at block 0, and end is last block of the actual device, AND, it must read() the device blocks sequentially.

How does the kernel know how to handle read() of a reg-file file differently than read() block-device file ?
I know it was mentioned that inode makes no diff, but is that true? How would kernel know the diff between reg-file or block-device if kernel does not read the info in the inode? open() must invoke kernel to read the inode data, no?

I need to craft a test to copy a storage block device to a file on another storage device, will use 'cp' and 'ddresecue'. If they both do exactly the same thing then each file should have same MD5 SHA hashes.
 
Old 02-28-2024, 08:25 PM   #15
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,985

Rep: Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626
Tar used to be a way. Why don't you use file by file like rsync or partition copy or such?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Does copying/pasting an image create an identical copy, block-by-block? duupunisher2x Linux - Newbie 22 08-24-2020 08:03 AM
Want to change how pasted files are named "(copy)," "(another copy)," "(3rd copy)" L a r r y Linux - Desktop 3 08-24-2013 03:39 PM
Is it possible to get into shell when you're in Recovery-mode to copy a device-block? Kristoffer G Linux - Software 0 12-20-2011 07:05 PM
Block device /dev/sda3 is not a valid root device.. rincewind Linux - Hardware 7 06-09-2006 04:47 AM
UL 1.0 GRUB: could not find device for /boot: not found or not a block device cma Linux - General 4 12-12-2005 03:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration