Software & AppsOperating SystemLinux

How To Move /usr to a New Partition in Ubuntu Without Losing Data

Ubuntu 21

In this article, we will be discussing a detailed procedure on how to move the /usr directory to a new partition in Ubuntu without losing any data. This process involves several steps, including copying files, editing system files, and rebooting your system.

Quick Answer

It is possible to move the /usr directory to a new partition in Ubuntu without losing data. This can be achieved by copying the files to the new partition, editing the /etc/fstab file to mount the new partition as /usr, and rebooting the system. However, it is important to note that this task should ideally be performed using a Live CD and a backup of your data is recommended as a precautionary measure.

Precautions

Before we begin, it’s important to note that this task should ideally be performed using a Live CD. This allows you to work on the partition without any open files interfering with the process. If you choose to proceed while Ubuntu is running, ensure you have a solid backup of your data as a precautionary measure.

Step 1: Copying the Files

The first step is to copy all the files from the /usr directory to the new partition. This can be done using the cp -a command. The -a option is used to preserve the file attributes and ensures a complete copy. Here’s an example of how to use this command:

cp -a /usr/* /path/to/new/partition

Remember to replace /path/to/new/partition with the actual location of your new partition.

Step 2: Identifying the UUID of the New Partition

Next, we need to identify the UUID (Universally Unique Identifier) of the new partition. This can be done by running the following command:

ls -l /dev/disk/by-uuid/

or

sudo blkid

These commands will list the UUIDs of all the partitions on your system. Identify the UUID of your new partition for the next step.

Step 3: Editing the /etc/fstab File

Now, we need to edit the /etc/fstab file to add a new entry for the /usr partition. The /etc/fstab file is a system configuration file that lists all available disks and disk partitions, and indicates how they should be initialized or otherwise integrated into the overall system’s file system.

Open the file using a text editor with root permissions, for example:

sudo nano /etc/fstab

Add a new entry for the /usr partition. Replace your-uuid and ext4 with your own values. Here’s an example:

UUID=your-uuid /usr ext4 defaults 0 2

Step 4: Rebooting the System

After editing the /etc/fstab file, save the changes and reboot your system. This ensures that the changes take effect and the new partition is mounted on /usr.

Step 5: Deleting the Old Files

After rebooting, the old files in the original /usr location will be hidden by the new partition. To delete them, we can use the mount --bind trickery. Execute the following commands:

sudo mount --bind / /mnt
sudo rm -rf /mnt/usr/*
sudo umount /mnt

Please be extremely careful while deleting the old files, as a slight mistake can cause unintended consequences.

Alternative Solution: Using rsync

Instead of using cp -a, you can use rsync -avz to copy the files. The rsync command is better suited for moving files between partitions, block devices, or machines. The -avz option stands for archive mode, verbose, and compress. Archive mode ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved in the transfer. Verbose increases the amount of information you are given during the transfer, and compress reduces the size of the data as it is sent over the network.

rsync -avz /usr/* /path/to/new/partition

Remember to replace /path/to/new/partition with the actual location of your new partition.

It is crucial to preserve hard links when moving files, as failure to do so can break package upgrades in the future. To ensure this, use rsync -aH instead of cp or rsync -avz.

Conclusion

In this article, we have discussed a detailed procedure on how to move the /usr directory to a new partition in Ubuntu without losing any data. This process involves several steps, including copying files, editing system files, and rebooting your system. Remember to always have a backup of your data before proceeding with such operations.

Is it necessary to use a Live CD to perform this task?

It is highly recommended to use a Live CD to perform this task. Working on the partition without any open files interfering with the process ensures a smoother and safer operation.

What happens if I don’t have a backup of my data?

If you don’t have a backup of your data and something goes wrong during the process, there is a risk of data loss. It is always recommended to have a solid backup of your data before proceeding with any partition-related operations.

Can I use a different text editor instead of nano to edit the /etc/fstab file?

Yes, you can use any text editor with root permissions to edit the /etc/fstab file. Some popular alternatives to nano include vi, vim, and emacs.

What should I do if I make a mistake while editing the /etc/fstab file?

If you make a mistake while editing the /etc/fstab file, it can potentially cause booting issues. In such cases, you can use a Live CD to access the file and correct the mistake. Alternatively, you can use the recovery mode to restore a backup of the /etc/fstab file.

Why do we need to delete the old files after rebooting?

After rebooting, the old files in the original /usr location will be hidden by the new partition. However, they will still occupy disk space. Deleting the old files ensures that you free up the disk space and avoid any confusion or conflicts in the future.

Can I use rsync instead of cp to copy the files?

Yes, you can use rsync instead of cp to copy the files. In fact, using rsync with the -avz options is considered a better approach for moving files between partitions. It ensures that symbolic links, attributes, permissions, ownerships, etc. are preserved in the transfer.

What is the purpose of preserving hard links when moving files?

Preserving hard links when moving files is crucial because failure to do so can break package upgrades in the future. Hard links allow multiple file names to refer to the same underlying data. By preserving hard links, you ensure the integrity and functionality of the files and their associated packages.

Leave a Comment

Your email address will not be published. Required fields are marked *