Migrating a drive with Linux and Windows partitions to a larger drive - rcasero/doc GitHub Wiki

These notes describe replacing the solid-state drive (SSD) on my Dell XPS 15 with Linux and Windows by a bigger one, but they could apply to other systems with a Windows/Linux dual boot, whether with a hard drive (HD) or SSD.

Disclaimer: These are my personal notes that I used for my own laptop. Following them may damage your system. Use at your own risk!

What do we want to do?

I have a Dell XPS 15 with a 512 GB SSD, and would like to upgrade it to a 1TB Samsung SSD.

I have the original Windows 10 Pro install that came with the laptop. I shrunk the Windows partition to make space for Ubuntu. Now the laptop has grub2 dual boot with Windows or Linux. I sometimes launch the native Windows from Linux using a VirtualBox raw disk configuration, as described in "Ubuntu linux on Dell XPS 15 (9560)".

There are two main steps:

  1. Clone the old SSD to the new SSD
  2. Expand the Windows and Linux partitions without breaking the boot system

Dell warranty considerations

(This is only of interest for people who own a Dell XPS, the rest can skip this section).

I cannot give legal advice, so check with Dell for your particular machine. I had heard that opening the laptop would break some internal seals, and then the warranty would be void. But I asked Dell specifically about that, and a representative told me that replacing the SSD would not void the laptop's warranty. Only the new SSD would not be covered by Dell's warranty.

However, I got the laptop via the University, and work in Government research now, so maybe we have some special arrangement. I don't know.

Clone the old SSD to the new SSD

  1. You have a choice if your Ubuntu home directory is encrypted:

    • Decrypt your Ubuntu home directory, so that later you will be able to recover your home files just by copying them over.
    • Leave your home directory encrypted, but login (so that you can access the files), and make a copy of everything to a separate location. This wastes space, because you'll have two copies of your files, but it's the least trouble.
    • Leave your home directory encrypted as it is. In the next section, we show how to incorporate the encrypted files into your new install.
  2. Create an Ubuntu install USB stick or DVD.

    • I would definitely avoid 17.10, which has been released with serious bugs (e.g. BIOS corruption in Lenovo laptops, but also random broken programs in my experience). Ubuntu 17.04 is more stable, but gparted doesn't e.g. finish resizing or partition repair operations. To me, for repartitioning, what works is Ubuntu 16.10, so you may want two Live USBs, use 16.10 for repartitioning, and 17.04 to install.
  3. Boot the laptop with it (in the initial Dell boot screen, press F12 to go to the boot menu, where you can select booting from the USB stick or DVD).

  4. Plug in an external drive that is at least as big as your SSD.

  5. Connect the laptop to the internet, using Wifi or an ethernet cable.

  6. In Ubuntu, launch "Software & Updates". Enable "Ubuntu Software" -> "Community-maintained free and open-source software (universe)". Update the list of packages and close "Software & Updates".

  7. From the command line,

    sudo apt install pv
    
  8. Make a copy of your old SSD to an external drive with pv. For example, in the Dell XPS 15, Ubuntu names the drive /dev/nvme0n1. If the external drive is mounted as e.g. /dev/sdb, then run

    sudo bash -c 'pv </dev/nvme0n1  >/dev/sdb'
    

    This will give you a progress bar and ETA when it finishes. For me, cloning a 512 GB SSD to an external drive with USB-C gave an ETA of 1 h. Note that if instead you run pv directly, you'll get a bash: /dev/nvme0n1: Permission denied error.

  9. Power down the laptop.

  10. Open the laptop from the back

    • You need a T5 Torx driver to remove the 10 small screws around the edges of the laptop's base.
    • You need a small Phillips screwdriver to remove the two larger screws hidden under the small lid that covers the service tag
  11. Replace the old SSD by the new one, and screw the base cover back on.

  12. Boot again with the USB stick or DVD as before.

  13. Clone from the external drive to the new SSD

    sudo bash -c 'pv </dev/sdb  >/dev/nvme0n1'
    

    This took 2 h for me.

Resize partitions

  1. Boot the laptop with the Ubuntu USB stick or DVD.

  2. Launch gparted with

    sudo gparted
    
  3. Move and resize partitions around so that they absorb the newly available unallocated space.

    • For example, let the partition order be: Windows C:, Linux, New unallocated space. Select the linux partition and move it e.g. 100 GB to the right. This will leave an extra 100 GB for Windows. Enlarge the linux partition to the right to take over the unallocated space. Then enlarge the Windows partition to take over the 100 GB of unallocated space. Finally, apply the changes.
    • At this point, your boot system has broken, because the linux partition's starting point is no longer where the MBR/UEFI thinks it is. This is expected.

Reinstall Ubuntu

Quite frankly, the easiest way to make things work again is to simply reinstall Ubuntu rather than copy system files over. The Ubuntu Live USB gives you several options, such as trying to preserve data files, wipe out the Ubuntu partition and reinstall from scratch, or wipe out the whole drive.

You don't want to wipe out the whole drive if you are dual booting Windows, but you can wipe out the Ubuntu partition, and after a clean install, copy back:

  • your home directory files, as they will include both your data and configuration files
  • any extra software you put in /opt

See Installing Ubuntu for details.

Note: You want the same username and password you had before to avoid trouble with encrypted home directories.

Getting your personal files back

If you have a decrypted copy of your old $HOME

  1. Boot Ubuntu in the laptop from the new SSD.

  2. Plug in the external drive that has the copy of your old system.

  3. Change directory to the home directory in the external drive. This will be specific to the label in your particular external drive, e.g.

    cd /media/rcasero/b5bfc76a-d3f9-4a1d-969f-b7c62bb14c62/home/
    
  4. Copy your old home directory over to the laptop

    rsync -ax $USER /home/
    

If your home directory was encrypted in your old SSD

If you didn't decrypt your Ubuntu home directory before you cloned your SSD to the external hard drive, now your data files are inaccessible (that's the whole point of an encrypted home directory, so that's expected).

  1. Boot Ubuntu in the laptop from the new SSD.

  2. Install ecryptfs-utils.

    sudo apt install ecryptfs-utils
    
  3. Plug in the external drive that has the copy of your old system.

  4. Change directory to the / root directory of the external drive. This will be specific to the label in your particular external drive, e.g.

    cd /media/rcasero/b5bfc76a-d3f9-4a1d-969f-b7c62bb14c62
    
  5. Copy all the encryption-related files, preserving permissions and ownership

    sudo rsync -ax share/ecryptfs-utils /usr/share/
    sudo rsync -ax home/.ecryptfs ~/../
    sudo rsync -ax home/$USER/.ecryptfs ~/
    sudo rsync -ax home/$USER/.Private ~/
    

At this point, if you reboot the machine, the next time you log into Ubuntu with your username and password (we assume you chose the same you had in the old SSD), your encrypted partition will mount automatically, and you will see those files in your $HOME directory transparently.

  1. You may want to create in ~/Desktop the following symlinks. If the automatic mounting of the encrypted partition works, you won't see them, because ~/Desktop will get replaced by the encrypted version. But if for some reason the decryption doesn't work, they can give you pointers to where to look for errors.

    cd ~/Desktop
    ln -s /usr/share/ecryptfs-utils/ecryptfs-mount-private.desktop Access-Your-Private-Data.desktop
    ln -s /usr/share/ecryptfs-utils/ecryptfs-mount-private.txt README.txt
    ln -s /home/.ecryptfs/$USER/.ecryptfs .ecryptfs
    ln -s /home/.ecryptfs/rcasero/.Private .Private
    
⚠️ **GitHub.com Fallback** ⚠️