How to replace a hard drive on Linux

Need more (or faster) storage? Seamlessly replace your old hard disk drive with a new solid-state drive.
125 readers like this.
How Linux got to be Linux: Test driving 1993-2003 distros

Internet Archive Book Images. Modified by Opensource.com. CC BY-SA 4.0

I built my current desktop about three years ago and installed a solid-state drive (SSD). Later, I needed more storage space, so I installed a second drive—an older spindle and platter hard disk drive (HDD) that happened to be lying around. Recently, I decided to replace this HDD with an SSD.

This article walks through the steps for replacing the drive, including some commands used for identifying and configuring a drive and editing the configuration file Linux uses.

Identify drives and partitions

To begin, use the parted command with the argument -l to list the physical drives in your system.

root@workstation:~# parted -l
Model: ATA Samsung SSD 850 (scsi)
Disk /dev/sda: 512GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size    File system  Name                  Flags
 1      1049kB  538MB  537MB   fat32        EFI System Partition  boot, esp
 2      538MB   495GB  494GB   ext4


Model: ATA WDC WD1500HLFS-0 (scsi)
Disk /dev/sdb: 150GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system     Name              Flags
 1      1049kB  4296MB  4295MB  linux-swap(v1)  Linux swap
 2      4296MB  150GB   146GB   ext4            Linux filesystem

You can see that my system has two drives: sda, which is an SSD, and sdb, which is an HDD. The Number column lists the partitions that exist on each drive. Each partition is referred to by number; for example, the full name of the first partition on sda is /dev/sda1, the second is /dev/sda2.

Map partitions to mount points

Now that you know the drives and their partitions, use the findmnt command to view details about each partition and where they are mounted in the filesystem.

findmnt --fstab --evaluate

The fstab option instructs findmnt to search according to the /etc/fstab file, and the evaluate option converts lengthy universally unique identifiers (UUIDs) to real device names.

# findmnt --fstab --evaluate
TARGET        SOURCE    FSTYPE OPTIONS
/             /dev/sda2 ext4   errors=remount-ro,noatime,discard
/boot/efi     /dev/sda1 vfat   umask=0077,noatime,discard
none          /dev/sdb1 swap   sw
/raptor       /dev/sdb2 ext4   defaults,noatime

In the output of findmnt, SOURCE is the partition that you can reference back to the output of the parted command from before. TARGET is the path within the filesystem where each is mounted; this is also known as the mount point.

Swap space

In the output above, the swap partition shows none for its target. Verify details about your swap space using the cat command.

root@workstation:~# cat /proc/swaps
Filename				Type		Size	Used	Priority
/dev/sdb1               partition	4194300	0	    -2

The /etc/fstab file

The /etc/fstab file is where Linux stores information about your drives, partitions, and filesystem, so it needs to be edited anytime you make changes to any of them. Now that you have identified the current drives, partitions, and mount points in your system, find these items in your fstab file.

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>   <dump>  <pass>
UUID=818aad1c-fcfc-4be8-9de8-ff6963383fe1 /         ext4    errors=remount-ro,noatime,discard   0 1
UUID=87B5-E1AE                            /boot/efi vfat    umask=0077,noatime,discard          0 1
UUID=dc8b85ac-0439-4b60-9267-58eb69b7f88d none      swap    sw                                  0 0
UUID=bad318c8-e095-4870-a8bb-c54f5488f569 /raptor   ext4    defaults,noatime                    0 2

The fstab file name is short for "filesystems table." The columns are numbered from left to right:

  1. file system represents the disk partition. In the olden days of yore, the actual low-level hardware path (like what is output from the parted command, i.e., /dev/sda1) would have been placed here. Today, all partitions are given a UUID, which is now the preferred way to refer to them. This is highlighted by the note at the top of the file. As the note says, you can view your system UUIDs with the blkid command. Use the -s argument to display only the UUID of each device.
    # blkid -s UUID
    /dev/sdb1: UUID="bad318c8-e095-4870-a8bb-c54f5488f569"
    /dev/sda1: UUID="87B5-E1AE"
    /dev/sda2: UUID="818aad1c-fcfc-4be8-9de8-ff6963383fe1"
  2. mount point represents the location within the Linux filesystem where the partition is mounted.
  3. type describes the partition's filesystem format. Linux supports many types; some common ones are ext3, ext4, and XFS, and there are many more. I used JFS for a long time.
  4. options govern how a partition is mounted. For example, the options ro and rw determine whether it is mounted as read-only or read-write. Another common option is noatime, which is used to disable updating of file access times, usually to improve disk I/O performance.
  5. dump is used by the dump utility to determine if the filesystem needs to be dumped. A setting of zero means no. This feature is not used much these days.
  6. pass is used by fsck to determine the order that filesystems will be checked for errors at boot time. A setting of 0 disables the check and skips the filesystem. Generally, the root (/) filesystem should be set to 1 so that it will be checked first.

Now that you have an understanding of the drives in your system and how they are mounted, you can begin the process of replacing the HDD.

Replacing the HDD

A good plan is to install the new SSD, copy the files on the HDD to the SSD, and then remove the HDD. You want ultimately to mount the SSD at the same point as the HDD, so any applications that rely on this path won't be broken.

Install the new drive

First, shut down the computer and unplug the power supply cable to avoid the risk of shock damage to either the computer or yourself. Next, temporarily install the SSD and connect it to a power cable.

New SSD cables and drive

Then, connect the data cable between the new drive and an open SATA connector on the motherboard. After everything is connected securely, plug the system power in and turn on the computer.

Next, verify that the system recognizes the new drive in your computer's BIOS or Unified Extensible Firmware Interface (UEFI). The key or keystroke combination to enter the system BIOS or UEFI varies by computer manufacturer. Usually, modern computers will automatically detect drives and other devices that are attached to the system, so there is probably nothing that needs to be changed. That was my case; my system sees the new drive as SATA SSD.

System BIOS detected drives

After verifying that all drives are detected and displayed by the system, reboot and let Linux load. Then, make sure that Linux recognizes the drive by repeating the parted command. The command parted -l shows that I have three drives installed: sda, sdb, and sdc. Here's the output for my new sdc drive:

Model: ATA SATA SSD (scsi)
Disk /dev/sdc: 240GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1075MB  1074MB  primary  ext4         boot
 2      1075MB  240GB   239GB   primary               lvm

This output lists the partition table as msdos and indicates there are two existing partitions; this is because I am using an SSD that was previously used in another system. This will all change once the drive is reformatted for its new life.

Prepare the new SSD

After confirming that Linux identifies the new SSD, use the parted command again—but this time to reconfigure the drive. In my case, I changed the partition table to GPT, which is newer. I also created a new partition for general storage using the XFS filesystem. You won't need to delete the existing partitions first because they will automatically be deleted when the new partition table, also called label, is changed and saved.

Set the partition table

Enter the command: parted /dev/sdc.

# parted /dev/sdc
GNU Parted 3.2
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.

Use the mklabel command to change the partition table or label to gpt. You will be warned that this action will destroy all existing data on the disk. Answer yes.

(parted) mklabel gpt
Warning: The existing disk label on /dev/sdc will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? y

You can verify changes to the drive configuration using the print command. You'll also notice that all partitions are gone.

Model: ATA SATA SSD (scsi)
Disk /dev/sdc: 240GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags

Create a partition

You can create new partitions using the mkpart command. I created just one partition since I plan to use the entire drive for extra storage space.

The print free command is also useful for displaying the free space on a drive. Because I don't have any partitions defined, the entire 240GB is available.

(parted) print free
Model: ATA SATA SSD (scsi)
Disk /dev/sdc: 240GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size   File system  Name  Flags
        17.4kB  240GB  240GB  Free Space

I'll name the partition storage and set the filesystem type to xfs. Since I have just one partition, I specify the start and end of the partition to use the entire available free space using percentages. This can be done with a one-line command.

mkpart storage xfs 0% 100%

Then, confirm with the print command.

(parted) print
Model: ATA SATA SSD (scsi)
Disk /dev/sdc: 240GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  240GB  240GB  xfs          storage

When you quit parted, you will be informed that the /etc/fstab file may need to be updated.

(parted) quit
Information: You may need to update /etc/fstab.

Format the partition

Next, format the new Linux partition with the xfs filesystem using mkfs.xfs.

mkfs.xfs /dev/sdc1

The new drive is formatted completely. As I mentioned earlier, it's currently standard to refer to a partition with its UUID rather than the physical device path. Therefore, determine the UUID of the new partition with blkid.

# blkid -s UUID

/dev/sdc1: UUID="d98abe0a-f641-4331-a3dc-b89ebc60bfb5"

Copy the files

The new drive is formatted and ready to be mounted into the system. But the process of replacing another drive with this one isn't quite that simple. First, both drives must be mounted simultaneously to copy files from the old drive to the new one.

To do this, mount the new drive on a temporary mount point, copy the files, and then unmount both drives. Why? So you can mount the new drive at the same mount point where the old drive was mounted in order to preserve the path. In case there are applications with that path configured, it's just simpler this way.

# mkdir /mnt/newssd
# mount UUID="d98abe0a-f641-4331-a3dc-b89ebc60bfb5" /mnt/newssd
# cp -avT /raptor/ /mnt/newssd/

Use diff to verify the copy's success.

diff -rqy /raptor/ /mnt/newssd/

Remove the old HDD

Unmount both drives

Now that the new drive contains all of the old files, remount it in place of the old drive. First, unmount both drives.

# umount /dev/sdc1
# umount /dev/sdb2

Turn off swap

Since the old drive will no longer be used as a swap device, disable it with the swapoff command.

# swapoff /dev/sdb1

If you repeat the cat /proc/swaps command, it will no longer list this partition.

Update the fstab file

Go back to the /etc/fstab file; there are a few changes that you need to make. I advise making a backup of the file before editing it.

cp -a /etc/fstab /etc/fstab.backup

In the line for the /raptor mount point, replace the UUID with that of the new drive; replace the filesystem, ext4, with xfs; and add discard to the options. It should appear like this:

UUID=d98abe0a-f641-4331-a3dc-b89ebc60bfb5 /raptor  xfs  defaults,noatime,discard  0 2

Disable swap permanently by editing the /etc/fstab file and commenting or deleting the swap line.

# UUID=dc8b85ac-0439-4b60-9867-58eb69b7f88d none  swap  sw  0 0

Swap space can be mounted on another drive or partition if it is still needed. In this case, rather than removing this line, just replace the UUID the same way you did when setting up the new drive.

After saving the fstab file, run mount to re-mount based on the changes.

root@workstation:~# mount -av
/                        : ignored
/boot/efi                : already mounted
/raptor            : successfully mounted

Finally, shut down the computer and remove the HDD that will no longer be used. Then power up and check that the system has booted correctly and that all drives are mounted as expected. In addition to the additional storage space, I also notice a performance gain and noise reduction.

What to read next

Install Linux on a used laptop

Now that Windows XP has been officially discontinued there are a huge number of Windows XP laptops for sale on eBay. Many of these run really well with a light Linux distro…

Tags
Alan Formy-Duval Opensource.com Correspondent
Alan has 20 years of IT experience, mostly in the Government and Financial sectors. He started as a Value Added Reseller before moving into Systems Engineering. Alan's background is in high-availability clustered apps. He wrote the 'Users and Groups' and 'Apache and the Web Stack' chapters in the Oracle Press/McGraw Hill 'Oracle Solaris 11 System Administration' book.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.