4

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!

  • 1
    Is drive MBR or gpt. Best not to use dd with gpt. Generally I find a new install and copy of /home and maybe some settings in /etc. And export list of installed apps and reinstall that list. That is typically quicker as you avoid all the issues of resizing or resetting UUIDs and reinstalling grub. – oldfred Jun 19 '19 at 3:46
3

Any time you use dd (a.k.a. disk destroyer) 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= and of= 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. Using dd 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:

  1. Take a screenshot of your old SSD's partition table for reference later when we set up your new SSD's partition table.
  2. Connect the new SSD to the computer and disconnect the old SSD. Keeping the old SSD disconnected makes it impossible to destroy any of our precious data while we do potentially destructive work.
  3. Boot from the Ubuntu installation USB thumb drive in Linux live mode. Make sure your BIOS always boots the thumbdrive in legacy/MBR mode instead of UEFI mode, since it sounds like your old system is using MBR. If my assumption is invalid, make sure your BIOS boots the thumbdrive in UEFI mode instead. Mixing things up would make it a lot harder to get grub to install properly later on.
  4. Use 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.
  5. Now that all the dangerous partition editing stuff is done, you can safely shutdown and add the old SSD connected to the computer.
  6. Boot the Linux live USB thumb drive again with both drives connected this time. Do not boot from the old SSD (we need to keep that system inactive to make a good clean copy of it).
  7. 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, the cp 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.

    mkdir oldroot newroot oldhome newhome
    sudo mount -o ro /dev/sda1 oldroot
    sudo mount -o ro /dev/sda2 oldhome
    ls oldroot
    ls oldhome
    

    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...

    sudo mount -o noatime /dev/sdc1 newroot
    sudo mount -o noatime /dev/sdc2 newhome
    ls newroot
    ls newhome
    

    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:

    sudo cp -Rfax oldroot/* newroot/
    sudo cp -Rfax oldhome/* newhome/
    

    Once that's done, unmount everything and sync cached writes to persistent storage:

    sudo umount oldroot
    sudo umount oldhome
    sudo umount newroot
    sudo umount newhome
    sync
    
  8. 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.

  9. 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:

    mkdir newroot
    sudo mount -o noatime /dev/sda1 newroot
    sudo chroot newroot /bin/bash
    mount -t devtmpfs udev /dev
    mount -t proc proc /proc
    mount -t sysfs sysfs /sys
    nano -w /etc/fstab
    

    Now you need to modify the fstab file to match the UUID numbers to whatever they are on your new SSD. You can open gparted 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.

    update-grub
    grub-install /dev/sda
    

    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.

    umount /sys
    umount /proc
    umount /dev
    exit
    sudo umount newroot
    sync
    

And there you have it! Hopefully your new SSD is bootable and everything is good now.

  • 1
    an edit and an upvote! Please review my edits to see how I did that: 8 spaces indent code, 1 space in front of a paragraph indents the entire paragraph. ;-) – Fabby Jun 19 '19 at 12:46
0

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.

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.