Shifting Linux Filesystem From One SSD To Another
Tags: linux, archlinux, ubuntu, admin
Published: 2023-08-08 10:43:08


#### Storyline
> I use my gaming laptop as a daily driver for streaming and coding since I shifted to a new city. The base storage of the laptop is a 128G ssd in which I have a running archlinux installation with EFI boot partition and Linux filesystem partition. Now I wanted to play games on [Lutris](https://lutris.net/) but my root partition only being 128G I need more space for more games.
I bought a 500G ssd and an external M.2 to USB adapter so that I can move my installation there.

#### Execution
First we need to create a similar partition table in the new ssd. Lets say when we connected the ssd using the external M.2 USB adapter its being detected as **/dev/sdc**. Check the device from the following command.
```bash
sudo fdisk -l
```
#### Lets say our root file system is on device

/dev/nvme0n1, check the partition table against /dev/nvme0n1 by using the same command as above
```bash
sudo fdisk -l
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zowxh8vnzbw4culrn00v.png)
In my case I have two partitions against nvme0n1
> nvme0n1p1  300M   EFI System
> nvme0n1p2  118.9G Linux filesystem

#### Now I will try to replicate a similar file system to /dev/sdc
```bash
sudo fdisk /dev/sdc
# now press m from help
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4a1hqtfeks60em89ummc.png)
- press g to create a new gpt partition table
- press n to create the first partition 
- press enter to accept the default starting sector 
- then enter +300M to create a partition with ending sector allowing a 300MB partition

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g15n57wbq6of8dn9p0dh.png)
- press t to change the partition type 
- press L to list  all existing partition numbers and choose the number listed against the EFI System otherwise the default would be linux filesystem

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrbe3pwfjf4hj8rnefh1.png)
- In my case EFI System is listed against 1 so I enter 1 as input and press enter 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ig87i0nvx8ri41irdgru.png)

#### After the partitions has been created we can replicate data from one partition to another using dd command 
```bash
# copy the EFI System data
sudo dd if=/dev/nvme0n1p1 of=/dev/sdc1

# copy the linux filesystem data
sudo dd if=/dev/nvme0n1p2 of=/dev/sdc2
```
- Now use we need to resize the dd filesystem of sdc2 to allow the whole ssd space
```bash
sudo resize2fs /dev/sdc2
```
#### We are not done yet we still need to refresh the boot partition grub settings against the new storage in which we replicated the file system, 
- First create a linux bootable live usb, in my case I am using ubuntu.
- Replace the ssd with the new ssd and bootup the system using the live usb created.
- Now we need to manually mount the partitions before we can update grub 
- First we mount the linux file system partition
```bash
# first we mount the linux file system
sudo mount /dev/nvme0n1p2 /mnt

# then we mount the EFI partition
sudo mount /dev/nvme0n1p1 /mnt/boot/efi

# then we bind mount before performing chroot
sudo mount --bind /dev /mnt/dev
sudo mount --bind /proc /mnt/proc
sudo mount --bind /sys /mnt/sys

# now we perform the chroot command
sudo chroot /mnt

# now we perform the grub install and grub update commands
grub-install
```

- Now we are done. Reboot the system and boot from the storage device itself the operating system should boot up fine