bokumin.org

Github

Migrate specific directories from SSD to HDD in Linux to extend the lifespan of SSD (Btrfs)

This article is a translation of the following my article:

 

 

* Translated automatically by Google.
* Please note that some links or referenced content in this article may be in Japanese.
* Comments in the code are basically in Japanese.

 

by bokumin

 

Extending SSD Lifespan by Moving Specific Directories to HDD on Linux (Btrfs)

 

Introduction

 

This time, I added an HDD to my desktop PC at home. Until now, I had configured the entire system on SSD (256GB).

 

・SSD has a limit on the number of writes (frequent writing will shorten its lifespan)
・HDD is inexpensive and can provide large storage capacity
・Separating system files and user data makes backup and management easier
・You can work while retaining specific directories when reinstalling or updating the system

 

From the above points, I decided to move the /home (user data) /var (log files and cache files) directories from the SSD to the HDD.
Actually, I have never heard of using an SSD until it reaches the end of its lifespan, but I would like to introduce that there is a method like this.
Now, I will explain how I actually implemented it.

 

手順

 

OS : Opensuse Tumbleweed

 

1. Creating a new subvolume

 

First, I created new Btrfs subvolumes named @home and @var on the new drive (/dev/sdb2).

 

sudo mkdir /mnt/newhdd
sudo mount /dev/sdb2 /mnt/newhdd
sudo btrfs subvolume create /mnt/newhdd/@home
sudo btrfs subvolume create /mnt/newhdd/@var

 

2. Moving data

 

Next, we will migrate the existing /home and /var contents to the new subvolume.

 

su -l
sudo rsync -avxHAX /home/ /mnt/newhdd/@home
sudo rsync -avxHAX /var/ /mnt/newhdd/@var

 

3. Backup/Delete existing directories (Be sure to stop any applications running on the system before deleting the var directory)

 

mkdir home_old;cp -r /home /home_old
mkdir var_old;cp -r /var /var_old
sudo rm -rf /home/*
sudo rm -rf /home/.*
sudo rm -rf /var/*
sudo rm -rf /var/.*

 

4. Update fstab

 

#/dev/sdb2のUUIDを確認
sudo blkid

#fstabに以下を追記
sudo nano /etc/fstab
UUID=yourUUID /home btrfs subvol=@home,defaults,noatime 0 0
UUID=yourUUID /var btrfs subvol=@var,defaults,noatime 0 0

 

*The fstab for /home and /var should be written, so be sure to comment out that line

 

5. Restart・Confirm

 

sudo reboot
#起動後、上手く認識しているか確認
df -h
tmpfs           3.2G  1.4M  3.2G   1% /run
tmpfs           1.0M     0  1.0M   0% /run/credentials/systemd-journald.service
tmpfs           1.0M     0  1.0M   0% /run/credentials/systemd-udev-load-credentials.service
tmpfs           1.0M     0  1.0M   0% /run/credentials/systemd-tmpfiles-setup-dev-early.service
tmpfs           1.0M     0  1.0M   0% /run/credentials/systemd-sysctl.service
tmpfs           1.0M     0  1.0M   0% /run/credentials/systemd-tmpfiles-setup-dev.service
tmpfs           1.0M     0  1.0M   0% /run/credentials/systemd-vconsole-setup.service
/dev/sda2       112G   11G  101G  10% /.snapshots
/dev/sda2       112G   11G  101G  10% /boot/grub2/i386-pc
/dev/sda2       112G   11G  101G  10% /boot/grub2/x86_64-efi
/dev/sda2       112G   11G  101G  10% /opt
/dev/sda2       112G   11G  101G  10% /root
/dev/sda2       112G   11G  101G  10% /srv
tmpfs           7.9G  268K  7.9G   1% /tmp
/dev/sda2       112G   11G  101G  10% /usr/local
/dev/sdb2       464G  1.6G  461G   1% /home
/dev/sdb2       464G  1.6G  461G   1% /var
/dev/loop2       64M   64M     0 100% /snap/core20/

 

If it is recognized successfully, it is complete.