Correct method to clone a disk using dd

Discussion in 'Other Operating Systems' started by Oblong Cheese, Dec 17, 2013.

  1. Oblong Cheese

    Oblong Cheese Member

    Joined:
    Aug 31, 2001
    Messages:
    10,574
    Location:
    Brisbane
    Hi all,

    My scenario is as follows: I am upgrading my HTPC by installing an SSD for the operating system and moving all media files to my NAS.

    So, I currently have Mythbuntu installed on a 750Gb disk. I want to move that install onto an SSD of 60Gb in size.

    My approach is as follows:
    1. Move existing media files off filesystem and resize to <60Gb
    2. Connect new SSD and boot into LiveCD environment
    3. Copy partition: dd if=/dev/sda1 of=/dev/sdb1 bs=4096 conv=notrunc,noerror
    4. Copy boot sector: dd if=/dev/sda of=/dev/sdb bs=512 count=1
    5. Finished!

    My approach does not work. What I end up with (sometimes) is even though the partition sda1 is exactly 50Gb in size, the full 60Gb of the SSD is used, and I do not have an sdb1 partition at all - instead, I simply have /dev/sdb, which can be mounted and read as a filesystem, but gparted can't understand it (cannot see space used on it, and I assume also cannot safely resize it). Also it is not bootable.

    I have learnt since trying this morning that the Linux kernel caches the state of partition tables on disks, so I will need to force a re-read after step four by
    Code:
    partprobe /dev/sdb
    (which probably explains why subsequent dd commands result in an error about the disk being full)

    Is this all I need? Or am I approaching this completely incorrectly? I have previously dd'd whole disks, but that was when increasing in size, which allowed me to copy the entire drive sector by sector.
     
    Last edited: Dec 17, 2013
  2. shift

    shift Member

    Joined:
    Jul 28, 2001
    Messages:
    2,929
    Location:
    Hillcrest, Logan
    More likely to work if you swap the order of step 3 and step 4. You seem to be using the partition before you've created it.

    Whenever I've done it, I create new partitions and copy file-wise rather than use dd, then manually reinstall the bootloader.
     
  3. samarium

    samarium Member

    Joined:
    Jun 6, 2007
    Messages:
    476
    Copying the boot sector using dd also copies the primary partition table. Not a good idea given the disks are different.

    I usually use a method similar to above by shift.
     
  4. ^catalyst

    ^catalyst Member

    Joined:
    Jun 27, 2001
    Messages:
    11,660
    Location:
    melbourne
    Good time to export all your configs and do a nice clean install :)
     
  5. w0ng

    w0ng Member

    Joined:
    Dec 17, 2006
    Messages:
    118
    Location:
    Sydney
    1. Move existing media files off filesystem and resize to <60Gb
    2. Connect new SSD and boot into LiveCD environment
    3. Copy all partitions and bootloader:
      Code:
      dd if=/dev/sda of=/dev/sdb bs=4096 conv=notrunc,noerror
    4. Update fstab entries to point to SSD
    5. Update bootloader to point to SSD
    6. Finished!
     
  6. OP
    OP
    Oblong Cheese

    Oblong Cheese Member

    Joined:
    Aug 31, 2001
    Messages:
    10,574
    Location:
    Brisbane
    Thanks guys for your input, I finally got it sorted.

    What I ended up doing:

    1. Create new partition table on SSD
    2. Create new /dev/sdb1 partition of exact required size
    3. dd if=/dev/sda1 of=/dev/sdb1 bs=4096 conv=notrunc,noerror
    4. Install SSD into system, boot Ubuntu LiveCD and repair it with Boot Repair
    5. Finished!

    My problem when playing around before was that, as mentioned above, dd'ing the first 512 bytes was also dd'ing the MBR, which doesn't work.
     

Share This Page