1080*80 ad

Migrating Linux Root to Smaller Disk

How to Safely Migrate Your Linux Installation to a Smaller Disk

Upgrading your system’s primary drive is one of the best performance boosts you can get, especially when moving from a traditional hard disk drive (HDD) to a solid-state drive (SSD). However, a common challenge arises when your new drive is smaller than the old one. Standard disk cloning tools often fail in this scenario, as they expect the destination to be the same size or larger.

Fortunately, it is entirely possible to migrate a full Linux installation, including the root partition, to a smaller disk. The process requires careful preparation and a few powerful command-line tools. This guide will walk you through the essential steps to perform this advanced task safely and effectively.

A critical word of warning: This procedure involves low-level disk operations. A single mistake, such as mixing up the source and destination drives, can result in permanent data loss. Always back up all your important data before you begin.

Prerequisites

Before starting, ensure you have the following ready:

  • A bootable Linux live USB drive: You will need to boot into a separate Linux environment to work on your system’s unmounted partitions. Any major distribution like Ubuntu or Mint will work.
  • The new, smaller destination drive: It must be large enough to hold all the actual data from your source partition, with a little extra space for overhead.
  • A complete backup of your data: This cannot be stressed enough.

Step 1: Boot into a Live Environment and Identify Disks

First, shut down your computer, install the new drive, and boot from your Linux live USB. Once you are in the live desktop environment, open a terminal window.

The first and most important step is to correctly identify your source and destination drives. Use the lsblk or fdisk -l command to list all connected storage devices.

sudo lsblk

You will see an output listing devices like /dev/sda, /dev/sdb, etc., along with their partitions (/dev/sda1, /dev/sda2). Carefully identify which is your old drive (the source) and which is your new, smaller drive (the destination). For this guide, we will assume:

  • /dev/sda is the old, larger drive.
  • /dev/sda1 is the source root partition we want to migrate.
  • /dev/sdb is the new, smaller drive (the destination).

Double-check these device names. Writing to the wrong drive will destroy your data.

Step 2: Shrink the Source Filesystem

The core of this process is to shrink the filesystem on the source partition to a size that will fit on the new disk. You are not shrinking the partition itself, but the filesystem within it.

  1. Run a Filesystem Check: Before resizing, it’s crucial to run a check to ensure the filesystem is healthy.

    sudo e2fsck -f /dev/sda1
    
  2. Resize the Filesystem: Now, resize the filesystem. You need to calculate a size that is slightly smaller than your new target disk to leave room for the partition table and other metadata. If your new disk is 240GB, for example, shrinking the filesystem to 235GB (or 235G) is a safe bet.

    sudo resize2fs /dev/sda1 235G
    

This command will shrink the ext4 filesystem located on /dev/sda1 down to 235 gigabytes.

Step 3: Partition the New Destination Disk

Your new disk needs a partition table and a partition to hold the migrated filesystem. You can use a tool like fdisk or gparted.

Using fdisk:

sudo fdisk /dev/sdb

Inside the fdisk utility:

  • Press g to create a new GPT partition table.
  • Press n to create a new partition. Accept the defaults to use the entire disk.
  • Press a to make the partition bootable.
  • Press w to write the changes to the disk and exit.

After this, your new, empty partition will be available at /dev/sdb1.

Step 4: Clone the Shrunken Filesystem

Now it’s time to copy the data. We will use the dd command to perform a block-level copy of the shrunken filesystem from the source partition to the new one.

This is the most dangerous step. The dd command is nicknamed “disk destroyer” for a reason. Triple-check your if (input file) and of (output file) parameters.

sudo dd if=/dev/sda1 of=/dev/sdb1 bs=4M status=progress

This command copies the content from /dev/sda1 to /dev/sdb1. The status=progress flag will show you the copy progress. This will take some time depending on the amount of data.

Step 5: Expand the Filesystem on the New Disk

After the copy is complete, the filesystem on the new drive is still set to the shrunken size (235G in our example). You need to expand it to fill the entire new partition.

  1. Check the new partition:

    sudo e2fsck -f /dev/sdb1
    
  2. Resize the filesystem:

    sudo resize2fs /dev/sdb1
    

Running resize2fs without a size argument automatically expands the filesystem to use all available space in its partition.

Step 6: Reinstall the GRUB Bootloader

Your operating system is now on the new drive, but the computer doesn’t know how to boot from it. You must install the GRUB bootloader. This is done using a chroot environment.

  1. Mount the new partition:

    sudo mount /dev/sdb1 /mnt
    
  2. Enter the chroot environment: This will make the live system treat your new partition as its own root.

    sudo chroot /mnt
    
  3. Install GRUB: Now, inside the chroot, install GRUB to the disk (not the partition).

    grub-install /dev/sdb
    update-grub
    
  4. Exit chroot:

    exit
    

Step 7: Update fstab (Optional but Recommended)

The /etc/fstab file manages how disks are mounted at boot, often using unique identifiers (UUIDs). Since your new partition has a new UUID, it’s best to update this file to avoid potential boot issues.

  1. Find the new UUID:

    sudo blkid /dev/sdb1
    

    Copy the UUID from the output.

  2. Edit the fstab file on the new drive:

    sudo nano /mnt/etc/fstab
    
  3. Find the line for the root partition (mounted at /) and replace the old UUID with the new one you just copied. Save the file and exit the editor.

Final Step: Test Your New System

You are now ready to test the migration. Unmount the partition and reboot your computer.

sudo umount /mnt
sudo reboot

As the computer starts, enter your BIOS/UEFI setup and change the boot order to prioritize your new, smaller drive. If all steps were performed correctly, your Linux system will boot up from the new drive.

Once you have confirmed that everything is working perfectly for a few days, you can reformat and use your old drive for backups or extra storage.

Source: https://kifarunix.com/shrink-linux-root-filesystem-by-migrating-to-new-disk/

900*80 ad

      1080*80 ad