Easily replace your hard drive
Tags: linux, harddrive, bash
Published: 2024-06-04 06:06:18

Recently, I encountered the issue that my 512GB SSD was gradually filling up. Naturally, the first step is to manually clean up. Under Linux, there’s a useful tool called **du** that allows you to determine how much disk space each directory occupies. This helps identify space-consuming culprits quickly:

```bash
# The `max-depth` parameter specifies the depth of the output breakdown.
du -h --max-depth=1 /
```

In my case, there was no obvious space hog that I could easily eliminate, so I decided to upgrade to a new SSD.

Now, how can you switch to a new disk without the hassle of a complete laptop reinstallation? Well, I took the lazy route and used the **dd** tool to create a byte-wise copy of my old hard drive onto the new one. This saved me the time-consuming process of setting up my laptop from scratch, and I was done in about an hour (including installing the new drive).

##Here’s a step-by-step guide:

###Create a Bootable Ubuntu USB Stick
Make sure you have a bootable USB stick with Ubuntu on it. You’ll need it to create a copy of your hard drive. Ensure that the old hard drive is not mounted; otherwise, the process won’t work.

###Install the New Hard Drive in an External Enclosure
I ordered the Samsung EVO 990 2TB hard drive. For compatibility, I paired it with [this external enclosure](
https://www.amazon.de/Geh%C3%A4use-NVMe-Geh%C3%A4use-kompatibel-Thunderbolt-Aluminiumlegierung/dp/B08X9YTWJC/ref=cm_cr_arp_d_product_top?ie=UTF8). I opted for a more expensive enclosure because the cheaper ones had poor reviews regarding speed and support for the Samsung drive.

###Connect the New Hard Drive via USB
The new disk doesn’t need to be formatted, simply install it and connect it via USB.

###Create a Byte-Wise Copy
Use the following command to create the copy:
```bash
# List all installed HDDs
lshw -class disk

# Replace `<old disk>` with something like `/dev/nvme0n1`, and `<new disk>` with `/dev/sdb` or similar (you'll recognize it by the disk size). Use `status=progress` to track the progress.
dd if=<old disk> of=<new disk> bs=64m status=progress
```
The copying process took less than 20 minutes for me, with the enclosure achieving around 700MB/s write speed to the new drive.

###Installing the New Hard Drive

Open the laptop case, remove the old drive, insert the new drive, and close the case.

With my Lenovo T14s, the screw securing the SSD was quite tight. I had to apply significant force to loosen it and ended up using pliers to make it easier. I suspect that too much screw-locking adhesive was used. Ideally, the screw shouldn’t be tighter than the case screws.

###Boot the Laptop and Resize Partitions if Necessary

The laptop should boot normally with the new hard drive.

If the new drive is larger than the old one, you’ll need to adjust your partitions to utilize the entire space. The steps depend on your existing partition setup. In my Ubuntu installation, I chose the default partitioning. Here’s what I did to resize the partition:

Open GParted and right-click on the largest partition to resize it to the maximum size.

Since my partition is a LVM partition (Logical Volume Manager), I needed the following command to expand it:

```bash
# First, check which parameter to append to `lvextend`:
df -h

# Then, execute this command to ultimately resize the partition:
lvextend -l +100%FREE -r /dev/mapper/vgubuntu-root
```

And that’s it—the hard drive replacement is complete!