The question is quite simple. I have search for an answer for a while but not sure I got the correct answer. Since mistakes could easily destroy my drive and data, I really want things to be clear.
I am running Ubuntu on a 250GB ssd (/dev/sda) which has two partitions, each is about 125GB big. The sda1 is mounted to /
and sda2 is mounted to /home
. Now I want to move everything into a new 1TB ssd (let's say it is /dev/sdc) and expand each drive into 500GB. After some research, I proposed two approaches:
1)do dd if=/dev/sda of=/dev/sdc
to clone the whole disk. After this I should get a bootable disk with two 125 GB partions and some unallocated space. Right? Then I need resize the sdc1 and sdc1 into 500GB with something like gparted. The resizing might take long, right?
2) The second approach is format the new drive first, make two 500GB partitions. do dd if=/dev/sda1 of=/dev/sdc1
and dd if=/dev/sda2 of=/dev/sdc2
to clone both partitions. At this point, this disk should be unbootable, right? In order to make it bootable, I need copy the bootloader into the new one. All I need is the first 446 bytes. So I can
dd if=/dev/sda of=/tmp/mbrsda.bak bs=512 count=1
then dd if=/tmp/mbrsda.bak of=/dev/sdc bs=446 count=1
. By doing this I made it bootable and also preserved the partition table for 500GB+500GB. This approach will save me from resizing the disk.
And for both approaches, I need change the UUID in the /etc/fstab file. This can be done by first mount the new drive after cloning: sudo mount /dev/sdc1 /mnt/
, then edit the /mnt/etc/fstab
file.
Am I right about all of this? Any potential risk of messing up my old drive here? Thanks a lot!
Any time you use
dd
(a.k.a.d
iskd
estroyer) there is a potential for messing up your data.dd
has no safeguards for accidentally writing your new drive over the old drive. The only thing keeping it working right is if you're VERY careful with typing the commands correctly and not mixing up the source and destinations.if=
andof=
are only one small mistyped keystroke away from each other!The other problem is that
dd
is slow and puts unnecessary wear on your target SSD. Most partitions are typically no where near 100% utilization. Usingdd
to copy a partition with lots of free space ends up copying EVERY BYTE of said partition, even the bytes that aren't actually containing any allocated data!Here's what I would do if it were my system:
gparted
to set up your new SSD with the desired partition table and format the filesystems. Try to keep everything the same as your old SSD's partition table (aside from partition sizes and UUID's). Don't forget to mark the/
root filesystem as active / bootable, just like your old SSD.Once booted, open a terminal and start working through these commands to copy the data from your old SSD to your new SSD. Unlike using
dd
, thecp
command is far more error-proof because you can't destroy anything copying an empty file system over top of your old file system -- there is nothing copied in that case! Plus, we can mount the old SSD in "read-only" mode to avoid any possibility of modifying the old SSD.Verify that you can see your old data. If all you see is an empty file system, perhaps you've got the wrong "/dev/sdXX" device...
Verify that all you see is an empty file system as your destination. If everything is good, issue the following commands to copy all of your files while preserving all the permissions/ownership/SELinux context settings:
Once that's done, unmount everything and sync cached writes to persistent storage:
Now you can safely shutdown and disconnect the old SSD so that we can't possibly destroy anything on it while we work through making the new SSD bootable.
Boot the computer using the USB thumb drive in Linux live mode with only the new SSD connected. Open a terminal and start working through these commands:
Now you need to modify the
fstab
file to match the UUID numbers to whatever they are on your new SSD. You can opengparted
to look at your partition table and figure out what the new UUID's are. When done, hit CTRL-X and save the fstab changes to disk.If everything is working right, this should configure grub and install it into your new SSD's MBR. After grub is installed, you have to unmount all the stuff we previously mounted from within the chroot environment before exiting. Otherwise, Ubuntu will have a hard time unmounting the Linux partition cleanly when you shutdown.
And there you have it! Hopefully your new SSD is bootable and everything is good now.
I just used gparted to migrate my ubuntu 18.04 from one SATA to another. Gparted provides an easy to use GUI with copy and paste options, and even the UUIDs are the same so I didn't have to update
/etc/fstab
or install grub on the new disk. It was very smooth, so I haven't check out what happened underneath yet, but during the copy-paste process, e2image(8) was actually invoked. Again, this was all based on my own experience, please correct me and thank you guys.I would use partimage to copy the partitions one at a time. After you copy the first partition, increase its size. The resize will be very fast because you won't have to move any adjacent partitions. Then copy the second partition and resize it to fit the rest of the space.
The easiest way to do this is to boot from SystemRescueCD and do everything from that environment. You can make a bootable USB drive so you don't need an actual CD.