Using the fdisk -l
command I got the following answer:
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 28266495 14132224 27 Hidden NTFS WinRE
/dev/sda2 28268544 28473343 102400 7 HPFS/NTFS/exFAT
/dev/sda3 28473344 132552703 52039680 7 HPFS/NTFS/exFAT
/dev/sda4 * 132556798 625141759 246292481 5 Extended
/dev/sda5 193996800 198092799 2048000 82 Linux swap / Solaris
/dev/sda6 234960896 625141759 195090432 7 HPFS/NTFS/exFAT
/dev/sda7 198094848 234950316 18427734+ 83 Linux
/dev/sda8 132556800 183756018 25599609+ 83 Linux
I'd like to copy the three first partitions of my disk in an image using the dd
command. So I mounted an external hard drive, entered in its folder and typed:
# dd count=$((132552703-2048)) if=/dev/sda of=./newImage.image
But this command copied all the sda disk to my external hard drive instead of just copying until the end of the sda3 partition.
How can I use the dd
to create an image that starts at the beginning of sda1 and finishes at the end of sda3?
Best Answer
first of all, here's how:
First do almost as you did before, but no subtraction - and add one to the count.
Next print the partition table at a
sed
process which can screen out the ones which you're removing.sed
will write ad
elete command to a secondfdisk
which has opened yourimg
file for every partition from sda4 and on.There is no 3. You're done.
secondly, here's why:
A Partial Success...
I'm pretty sure your command almost worked, but I'm willing to bet that it worked better than you think.
I expect that when you say it copied all of sda you believe that because an
fdisk -l
of that image indicated all of the partitions were included within. Based on thedd
command in your question, though, provided/dev/sda
's sector size is the fairly standard 512 bytes (and therefore identical todd
's default blocksize) then you should have copied everything from byte 0 of/dev/sda
only through to all but the last 2k sectors of/dev/sda3
.About Sectors...
You can see below where the
fdisk
output reports on Units. That is the size of each sector thatfdisk
reports on. A disk sector might be 4096-bytes - if it is a very recently manufactured disk and handles the Advanced Format sector-size - otherwise it is very rare to find a disk not partitioned on a standard logical 512-byte sector-size.This is how
fdisk
'sman
page puts it:There's more on this here.
And something about
dd
, too...dd
cannot silently lose data. In fact, if a short read occurs,dd
is specified to be very vocal about it:Block i/o...
lseek()
for files existing on block-devices - it's a very basic principle of blocked i/o.To sum up...
/dev/sda
will contain its entire partition table, and as such you would have copied said partition table to your image, and so anfdisk -l
of your image would report for all partitions of/dev/sda
, whether or not the data for those partitions actually resides within that image-file. You can, instead, of course, justcat
the separate data partitions separately into separate image files if you like - but in that case you lose the partition table entirely. All you really have to do is delete the partitions which you did not copy, and make sure you copy all of those you do.third, here's how I know:
This will create an 4G
./img
file full of NULs.This will partition
./img
to match your disk up to the first three partitions but on a 1/16th scale:And so now we can look at it.
I'll also put some actual filesystems and files on the three partitions.
Here are the byte offsets for where it all wound up...
But did you notice that
fdisk
was perfectly happy to report on the partitions' sizes before ever we formatted them with filesystems? This is because the partition table lies at the very head of the disk - it's only a layout and nothing more. None of the partitions need actually exist to be reported. They're only logically mapped out within the first 1M of./img
. Watch:Let's try getting only the first two partitions off of
./img
...We'll
grep
it again...And get an
fdisk
report...Now that is curious.
fdisk
still seems to believe there's a third partition extending as far out as 4G for a disk which it also seems to believe is only 869M in size!Probably we should remove that third partition from the partition table.
And now lets see if we can mount the partitions we copied and if our files remain in tact...
Apparently it's not impossible.