I want to clone a disk

I needed to clone my laptop disk, but I didn’t do it right.

Last week, by chance, I run a SMART long test on my laptop disk and it
found two errors in one partition. I did a backup image of that
partition the same day using dd, and then I overwrote the partition: 285
new errors appeared.

I bought a new disk which arrived today.

Meanwhile, I cloned the entire disk, but I did a mistake: instead of
cloning the disk whole I cloned the partitions one by one, plus the mbr
(and the 63 next sectors) and also the extended partition.

Today I used dd to restore the partition table. My mistake is that the
procedure does not restore the logical partitions. Not knowing how to do
it, and not having access to write here and ask (I only have this laptop
here), I resorted to dd the entire original disk to the new disk.

The procedure worked, to the point that the first read error appeared
and dd stopped at partition 8. I then restored that partition from the
good copy on a backup disk.

Good enough for the moment, as that means I can use the laptop again.
But the logical partition 9 is not there. Look at the partition table of
the new disk:


> minas-tirith:~ # fdisk /dev/sda
> omitting empty partition (9)
>
> Command (m for help): p
>
> Disk /dev/sda: 500.1 GB, 500107862016 bytes
> 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
> Units = sectors of 1 * 512 = 512 bytes
> Sector size (logical/physical): 512 bytes / 512 bytes
> I/O size (minimum/optimal): 512 bytes / 512 bytes
> Disk identifier: 0xb4e583dd
>
>    Device Boot      Start         End      Blocks   Id  System
> /dev/sda1            2048      409599      203776    7  HPFS/NTFS/exFAT
> /dev/sda2          409600   333279231   166434816    7  HPFS/NTFS/exFAT
> /dev/sda3       950562816   976771071    13104128    7  HPFS/NTFS/exFAT
> /dev/sda4   *   333284490   950549984   308632747+   f  W95 Ext'd (LBA)
> /dev/sda5       333284553   345863384     6289416   82  Linux swap / Solaris
> /dev/sda6       345863448   346265009      200781   83  Linux
> /dev/sda7       346265073   409175549    31455238+  83  Linux
> /dev/sda8       409175613   935850509   263337448+  83  Linux
>
> Partition table entries are not in disk order
>
> Command (m for help):

and now at the original disk:


> minas-tirith:~ # fdisk -l /dev/sdd
>
> Disk /dev/sdd: 500.1 GB, 500107862016 bytes
> 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
> Units = sectors of 1 * 512 = 512 bytes
> Sector size (logical/physical): 512 bytes / 512 bytes
> I/O size (minimum/optimal): 512 bytes / 512 bytes
> Disk identifier: 0xb4e583dd
>
>    Device Boot      Start         End      Blocks   Id  System
> /dev/sdd1            2048      409599      203776    7  HPFS/NTFS/exFAT
> /dev/sdd2          409600   333279231   166434816    7  HPFS/NTFS/exFAT
> /dev/sdd3       950562816   976771071    13104128    7  HPFS/NTFS/exFAT
> /dev/sdd4   *   333284490   950549984   308632747+   f  W95 Ext'd (LBA)
> /dev/sdd5       333284553   345863384     6289416   82  Linux swap / Solaris
> /dev/sdd6       345863448   346265009      200781   83  Linux
> /dev/sdd7       346265073   409175549    31455238+  83  Linux
> /dev/sdd8       409175613   935850509   263337448+  83  Linux
> /dev/sdd9       935850573   950549984     7349706   83  Linux
>
> Partition table entries are not in disk order
> minas-tirith:~ #


As you can see, both disks are exactly the same, except for sda9 which
is “partially” missing. If I try to create it again from fdisk, I can
not set the same sectors as the original:


> /dev/sda8       409175613   935850509   263337448+  83  Linux
>
> Partition table entries are not in disk order
>
> Command (m for help): n
> First sector (935852558-950549984, default 935852558): ^C
> minas-tirith:~ #

I want it to start on sector 935850573 and end on 950549984, so that I
can dd the backup image of the 9th partition. If it starts where fdisk
wants it will not fit.

So the first question is how can I recreate that 9th partition at
exactly the same size as the original (and why doesn’t fdisk let me do it).

The second question is for the next time: how should I recreate an
entire partition table so that both the primary partitions and the
logical partitions are recreated. As it is, my procedure does not
recreate the logical partitions.

I think that somewhere in sda8 it is stored a pointer to where sda9
starts, and sda9 stores somewhere its own size. As nothing of sda9 has
been written, the table is incomplete and fdisk complains of “omitting
empty partition (9)”. On the other hand, fdisk is probably using a
different alignment that it used when I created the disk some years ago,
but I do not see how to change it.


Cheers / Saludos,

Carlos E. R.
(from 11.4, with Evergreen, x86_64 “Celadon” (Minas Tirith))

Did you try expert mode? Also, you could try sfdisk -N9:

       -N number
              Change only the single partition indicated.  For example:
                  % sfdisk /dev/hdb -N5
                  ,,,*
              will  make  the  fifth partition on /dev/hdb bootable (`active')
              and change nothing  else.  (Probably  this  fifth  partition  is
              called  /dev/hdb5,  but  you are free to call it something else,
              like `/my_equipment/disks/2/5' or so).

No. It is stored somewhere before sda8 in EBR, just like primary partition table is stored outside of any partition in MBR. You can my script to see partition chain: https://raw.github.com/arvidjaar/scriptlets/master/walkparts.sh

and sda9 stores somewhere its own size.

EBR for sda8 normall contains size for sda8 and pointer to sda9. Although I have seen EBR with only pointer, without any partition, and in theory all 4 slots can be used (normally it is one partition per EBR).

how should I recreate an
entire partition table so that both the primary partitions and the
logical partitions are recreated

Use sfdisk to dump and re-create partitions.

On 2013-01-16 04:36, arvidjaar wrote:
>
> Did you try expert mode?

In eXtended mode there is a new menu, but ‘n’ to create partition does
not work, you have to return to the normal menu.

Also, you could try sfdisk -N9:

but sfdisk wants cylinders, I don’t see a method to display sectors
instead. Ah, got it, ‘-uS’. Original disk:


minas-tirith:~ # sfdisk -uS -l /dev/sdc

Disk /dev/sdc: 60801 cylinders, 255 heads, 63 sectors/track
Units = sectors of 512 bytes, counting from 0

Device Boot    Start       End   #sectors  Id  System
/dev/sdc1          2048    409599     407552   7  HPFS/NTFS/exFAT
/dev/sdc2        409600 333279231  332869632   7  HPFS/NTFS/exFAT
/dev/sdc3     950562816 976771071   26208256   7  HPFS/NTFS/exFAT
/dev/sdc4   * 333284490 950549984  617265495   f  W95 Ext'd (LBA)
/dev/sdc5     333284553 345863384   12578832  82  Linux swap / Solaris
/dev/sdc6     345863448 346265009     401562  83  Linux
/dev/sdc7     346265073 409175549   62910477  83  Linux
/dev/sdc8     409175613 935850509  526674897  83  Linux
/dev/sdc9     935850573 950549984   14699412  83  Linux
minas-tirith:~ #

Lets try.


> minas-tirith:~ # sfdisk -uS --no-reread /dev/sda
>
> Disk /dev/sda: 60801 cylinders, 255 heads, 63 sectors/track
>
> sfdisk: ERROR: sector 935850510 does not have an msdos signature
> Old situation:
> Units = sectors of 512 bytes, counting from 0
>
>    Device Boot    Start       End   #sectors  Id  System
> /dev/sda1          2048    409599     407552   7  HPFS/NTFS/exFAT
> /dev/sda2        409600 333279231  332869632   7  HPFS/NTFS/exFAT
> /dev/sda3     950562816 976771071   26208256   7  HPFS/NTFS/exFAT
> /dev/sda4   * 333284490 950549984  617265495   f  W95 Ext'd (LBA)
> /dev/sda5     333284553 345863384   12578832  82  Linux swap / Solaris
> /dev/sda6     345863448 346265009     401562  83  Linux
> /dev/sda7     346265073 409175549   62910477  83  Linux
> /dev/sda8     409175613 935850509  526674897  83  Linux
> Input in the following format; absent fields get a default value.
> <start> <size> <type [E,S,L,X,hex]> <bootable -,*]> <c,h,s> <c,h,s>
> Usually you only need to specify <start> and <size> (and perhaps <type>).
>
> /dev/sda1 :935850573 14699412
> /dev/sda1     935850573 950549984   14699412  83  Linux
> /dev/sda2 :^C
> minas-tirith:~ #

It wants to change partition 1! I need to change partition 9 only. The
format above doesn’t allow a partition number. If I give the parameter
-N9 I get


sfdisk: Partition 9 does not exist, cannot change it

Another attempt. Pressing enter accepts the current partition


minas-tirith:~ # sfdisk -uS --no-reread /dev/sda

Disk /dev/sda: 60801 cylinders, 255 heads, 63 sectors/track

sfdisk: ERROR: sector 935850510 does not have an msdos signature
Old situation:
Units = sectors of 512 bytes, counting from 0

Device Boot    Start       End   #sectors  Id  System
/dev/sda1          2048    409599     407552   7  HPFS/NTFS/exFAT
/dev/sda2        409600 333279231  332869632   7  HPFS/NTFS/exFAT
/dev/sda3     950562816 976771071   26208256   7  HPFS/NTFS/exFAT
/dev/sda4   * 333284490 950549984  617265495   f  W95 Ext'd (LBA)
/dev/sda5     333284553 345863384   12578832  82  Linux swap / Solaris
/dev/sda6     345863448 346265009     401562  83  Linux
/dev/sda7     346265073 409175549   62910477  83  Linux
/dev/sda8     409175613 935850509  526674897  83  Linux
Input in the following format; absent fields get a default value.
<start> <size> <type [E,S,L,X,hex]> <bootable -,*]> <c,h,s> <c,h,s>
Usually you only need to specify <start> and <size> (and perhaps <type>).

/dev/sda1 :
/dev/sda1             1 976773167  976773167  83  Linux
/dev/sda2 :
/dev/sda2             0         -          0   0  Empty
/dev/sda3 :
/dev/sda3             0         -          0   0  Empty
/dev/sda4 :
/dev/sda4             0         -          0   0  Empty
New situation:
Units = sectors of 512 bytes, counting from 0

Device Boot    Start       End   #sectors  Id  System
/dev/sda1             1 976773167  976773167  83  Linux
/dev/sda2             0         -          0   0  Empty
/dev/sda3             0         -          0   0  Empty
/dev/sda4             0         -          0   0  Empty
Warning: partition 1 does not end at a cylinder boundary
I don't like this - probably you should answer No
Do you want to write this to disk? [ynq]

I probably have to say “y” here

>
> Successfully wrote the new partition table
>
> Re-reading the partition table ...
> BLKRRPART: Device or resource busy
> The command to re-read the partition table failed.
> Run partprobe(8), kpartx(8) or reboot your system now,
> before using mkfs
>
> If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
> to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
> (See fdisk(8).)
>
>
> minas-tirith:~ #

Argh! It ignored the logical partitions! It has destroyed my partition
table, and the system is running!


minas-tirith:~ # fdisk -l /dev/sda

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb4e583dd

Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1   976773167   488386583+  83  Linux
minas-tirith:~ #

Ok…

>
> minas-tirith:~ # sfdisk -d /dev/sdc > partition.sfdisk
> minas-tirith:~ # sfdisk --no-reread /dev/sda < partition.sfdisk
> ...
> sfdisk: I don't like these partitions - nothing changed.
> (If you really want this, use the --force option.)
> minas-tirith:~ # sfdisk --no-reread --force /dev/sda < partition.sfdisk
>
> Disk /dev/sda: 60801 cylinders, 255 heads, 63 sectors/track
> Old situation:
> Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0
>
>    Device Boot Start     End   #cyls    #blocks   Id  System
> /dev/sda1          0+  60801-  60802- 488386583+  83  Linux
> /dev/sda2          0       -       0          0    0  Empty
> /dev/sda3          0       -       0          0    0  Empty
> /dev/sda4          0       -       0          0    0  Empty
> New situation:
> Units = sectors of 512 bytes, counting from 0
>
>    Device Boot    Start       End   #sectors  Id  System
> /dev/sda1          2048    409599     407552   7  HPFS/NTFS/exFAT
> /dev/sda2        409600 333279231  332869632   7  HPFS/NTFS/exFAT
> /dev/sda3     950562816 976771071   26208256   7  HPFS/NTFS/exFAT
> /dev/sda4   * 333284490 950549984  617265495   f  W95 Ext'd (LBA)
> /dev/sda5     333284553 345863384   12578832  82  Linux swap / Solaris
> /dev/sda6     345863448 346265009     401562  83  Linux
> /dev/sda7     346265073 409175549   62910477  83  Linux
> /dev/sda8     409175613 935850509  526674897  83  Linux
> /dev/sda9     935850573 950549984   14699412  83  Linux
> Warning: partition 1 does not end at a cylinder boundary
> Successfully wrote the new partition table
>
> Re-reading the partition table ...
> BLKRRPART: Device or resource busy
> The command to re-read the partition table failed.
> Run partprobe(8), kpartx(8) or reboot your system now,
> before using mkfs
>
> If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
> to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
> (See fdisk(8).)
> minas-tirith:~ #
> minas-tirith:~ # man partprobe
> minas-tirith:~ # partprobe /dev/sda
> minas-tirith:~

I had to force the issue, let’s hope it works. I’ll reboot and pray.

This software is obsolete, it wants to align on cylinders :-/

robin_listas Wrote:

I think that somewhere in sda8 it is stored a pointer to where sda9
starts,
No. It is stored somewhere before sda8 in EBR, just like primary
partition table is stored outside of any partition in MBR.

Ah, yes, of course.

You can my
script to see partition chain:
https://raw.github.com/arvidjaar/scriptlets/master/walkparts.sh

I’ll have a look.


minas-tirith:~/Downloads/forum_arvidjaar # bash walkparts.sh  /dev/sdc
MBR dump
Flag CHS/Start Type CHS/End    LBA/Start      LBA/Cnt
00    202100   07  7e2519         2048       407552
00    7e2619   07  feffff       409600    332869632
00    feffff   07  feffff    950562816     26208256
80    feffff   0f  feffff    333284490    617265495

EBR at offset 333284490
Flag CHS/Start Type CHS/End    LBA/Start      LBA/Cnt
00    feffff   82  feffff           63     12578832
00    feffff   05  feffff     12578895       401625
00    000000   00  000000            0            0
00    000000   00  000000            0            0

EBR at offset 345863385
Flag CHS/Start Type CHS/End    LBA/Start      LBA/Cnt
00    feffff   83  feffff           63       401562
00    feffff   05  feffff     12980520     62910540
00    000000   00  000000            0            0
00    000000   00  000000            0            0

EBR at offset 346265010
Flag CHS/Start Type CHS/End    LBA/Start      LBA/Cnt
00    feffff   83  feffff           63     62910477
00    feffff   05  feffff     75891060    526674960
00    000000   00  000000            0            0
00    000000   00  000000            0            0

EBR at offset 409175550
Flag CHS/Start Type CHS/End    LBA/Start      LBA/Cnt
00    feffff   83  feffff           63    526674897
00    feffff   05  feffff    602566020     14699475
00    000000   00  000000            0            0
00    000000   00  000000            0            0

EBR at offset 935850510
Flag CHS/Start Type CHS/End    LBA/Start      LBA/Cnt
00    feffff   83  feffff           63     14699412
00    000000   00  000000            0            0
00    000000   00  000000            0            0
00    000000   00  000000            0            0

minas-tirith:~/Downloads/forum_arvidjaar #

Both disks are exactly the same.

Mmm fdisk… lists the same identifier for both disks. Should I change
it? The extended menu can do it.


command (m for help): p

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb4e583dd

I changed it to “0xb4e583de”.
u

>> how should I recreate an
>> entire partition table so that both the primary partitions and the
>> logical partitions are recreated
> Use sfdisk to dump and re-create partitions.

It works… but it complains that partition do not use cylinders
boundaries, this is obsolete. I don’t like this program, there must be
another one that is better.

Ok… I have to reboot. If I disappear, you know what happened… :slight_smile:


Cheers / Saludos,

Carlos E. R.
(from 11.4, with Evergreen, x86_64 “Celadon” (Minas Tirith))

On 2013-01-16 11:44, Carlos E. R. wrote:
> Ok… I have to reboot. If I disappear, you know what happened… :slight_smile:

I’m back :slight_smile:


Cheers / Saludos,

Carlos E. R.
(from 11.4, with Evergreen, x86_64 “Celadon” (Minas Tirith))