3

I had an Ubuntu 14.04 running on a 1024 GB disk (disk A), which only used up to 130 GB spaces. I want to clone it to a 256 GB SSD disk ( disk B ). But failed.

I used gparted to shrink the partitions on disk A to be only about 180 GB for the preparation for disk B successfully.

Can you tell me where I was wrong?

1) restore the partition table

I did backup the partition table of disk A.

└──╼ $ sudo sfdisk -d /dev/sda
# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=     2048, size=   997376, Id=83, bootable
/dev/sda2 : start=   999424, size= 15624192, Id=82
/dev/sda3 : start= 16623616, size=337020928, Id=83
/dev/sda4 : start=        0, size=        0, Id= 0

Tips

/dev/sda1 for /boot, /dev/sda2 for swap, /dev/sda3 for /.

Save partition table

sudo sfdisk -d /dev/sda > partition.table

Then I tried to restore the partition table to disk B ( /dev/sdc in this scenario ).

I replaced sda with sdc in file partition.table. Then it looks like:

# partition table of /dev/sdc
unit: sectors

/dev/sdc1 : start=     2048, size=   997376, Id=83, bootable
/dev/sdc2 : start=   999424, size= 15624192, Id=82
/dev/sdc3 : start= 16623616, size=337020928, Id=83
/dev/sdc4 : start=        0, size=        0, Id= 0

Then do the restore successfully.

sudo sfdisk /dev/sdc < partition.table

2) migrating disk partition content

sudo dd if=/dev/sda1 of=/dev/sdc1

sudo dd if=/dev/sda2 of=/dev/sdc2

sudo dd if=/dev/sda3 of=/dev/sdc3

After migration, those partitions on /dev/sdc can be mounted and viewed.

Failure

But if I plugged the SSD disk (disk B) into my laptop, it would not boot up after some Thinkpad BIOS output. No error came out but a blinking cursor...

I bet the BIOS even did not detect the /boot on disk B when doing booting.

Can you help me? Many thanks!


update

Some one suggested me to use grub-install /dev/sdc to do the trick.

I searched what grub-install is capable -- link Let me try. And I am pretty sure disk A ( had MBR installed ).


Update

After doing dd if=/dev/sda of=/dev/sdc bs=512 count=1 , insert disk B only, it's still the same blinking cursor. Nothing really after BIOS.

After doing grub-install --boot-directory=/mnt/mypartition/boot /dev/sdc I went to boot it up, only disk B.

But grub console came out . And reported

enter image description here


Update

Now it is working!!!

Here's how I did it, on the PC running disk A as OS and the disk B (/dev/sdc) as a USB hard drive.

sudo mount /dev/sdc3 /mnt
sudo mount /dev/sdc1 /mnt/boot

sudo grub-install --boot-directory=/mnt/boot /dev/sdc3

Then went to the /mnt/boot/grub/grub.cfg, I did replaced 2 things in file grub.cfg. (remember to give write permission to the file grub.cfg)

replace hd1 with hd0

replace /dev/sdc3 with /dev/sda3

Then save the file. -> Power off computer -> Insert disk B via SATA and take out disk A forever. -> Boot -> See grub error but still boot up

If you met error Error: invalid environment block. Press any key to continue, please check this to solve it. Press any key will boot your system. https://askubuntu.com/questions/191852/error-invalid-environment-block-press-any-key-to-continue

sudo -i

Then, run each command, one-by-one.

cd /boot/grub
rm grubenv
grub-editenv grubenv create
grub-editenv grubenv set default=0
grub-editenv grubenv list
update-grub

Now go rebooting, it will work!

This is how I shrank my 1024GB disk hard drive and migrated the entire system to a new 256GB SSD disk.

  • If you do dd if=/dev/sda of=/dev/sdc then the partition table and the MBR will also be migrated. You need one or both (GPT for UEFI boot and MBR for BIOS boot) to have the disk boot properly. One issue is that you're copying disk UUIDs, which make the kernel confused if both disks (copied and copy) are connected at the same time. – grochmal Jul 21 '16 at 17:58
  • But the thing is that /dev/sda is bigger than /dev/sdc physically. I guess the disk sdc will be full. – dotslash Jul 21 '16 at 18:03
  • Then you do need to reinstall the bootloader (grub) code alright, the MBR is squeezed between the partition table and the first partition. I assume your motherboard is not UEFI (i.e. does not have a UEFI menu when it boots), right? UEFIs are very different (almost all new laptops have UEFI boot). – grochmal Jul 21 '16 at 18:08
  • Do not forget to create the grub.cfg file for your new OS. grub-install will install the bootloader on the disk and grub-mkconfig will create a config file (grub boot menu) for the new disk. – jc__ Jul 21 '16 at 19:12
  • im guessing that disk 1 ATA HDD0 Sandisk... Is the disk you want to boot from, and the bios is set to boot from it? – jc__ Jul 21 '16 at 19:34
2

I am not familiar with sfdisk, but you could accomplish the same thing, partition table AND MBR back up using dd.

This was in my notes and I am not the author...

Backing up the MBR

The MBR is stored in the the first 512 bytes of the disk. It consist of 3 parts:

  • The first 446 bytes contain the boot loader.
  • The next 64 bytes contain the partition table (4 entries of 16 bytes each, one entry for each primary partition).
  • The last 2 bytes contain an identifier

Clone the MBR as mbr.img:

dd if=/dev/sdX of=/path/mbr_file.img bs=512 count=1

Clone partition as pX.img

dd if=/dev/sdX of=/path/pX.img bs=1024

Restore the MBR to new disk

dd if=/path/mbr_file.img of=/dev/sdY bs=512

Restore Partition to new disk

dd if=/path/pX.img of=/dev/sdX bs=1024

OR

You could use clonezilla to make an image of the OS and restore it on a disk that already has the partitions created.

This way you might need to reinstall grub on the new disk

grub-install

grub-mkconfig

and set your swap partition in the 'new' OS.

mkswap

swapon
  • Do you mean grub-install /dev/sdc rather than grub-install /dev/sdc1 for this instance? – dotslash Jul 21 '16 at 17:58
  • yes. mount the new disk, cloned OS partition. grub-install --boot-directory=/mnt/newDisk_OsPartition/boot/grub /dev/sdc – jc__ Jul 21 '16 at 18:09
  • Did you see the question updated mins ago? Grub install failed. No valid system. I didn't see the grub OS choosing interface but the error of this photo – dotslash Jul 21 '16 at 19:12

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.