Move /home Directory to New Partition

Provides a tutorial example on how to move /home directory to a new partition when it runs out of space. A better solution is to extend its file system.

How To Move /home Directory to a New Partition? If you are running a Linux with multiple users, the /home directory where every user stores his/her files and emails will grow fast. This will cause the file system run out of space.

There are 2 options to resolve this problem.

1. Disconnect all users from the system using "pkill" command. You can disconnect users by their terminal names like "tty2". If any users are still using the system, files in the /home directory will be in use and impacted by the moving process.

(Log in as "root")
root# who
  joe      tty2         12:41 (tty2)
  root     pts/2        19:10 (tty1)

root# pkill -9 -t tty2

root# who
  root     pts/2        19:10 (tty1)

2. Stop background jobs that are using the /home directory, like email and ftp programs.

root# systemctl stop postfix

root# systemctl status postfix
   Active: inactive (dead) ...

...

3. Mount the new partition to temporary location like /mnt/home. Make sure the new partition has a file system type that is compatible with the /home directory.

root# mkdir -p /mnt/home

(assuming the new partition is /dev/sdb1)
root# mount /dev/sdb1 /mnt/home

("ext4" is compatible with "xfs")
root# grep home /etc/mtab
/dev/mapper/cl-home /home xfs rw,seclabel,relatime,attr2,inode64,...
/dev/sdb1 /mnt/home ext4 rw,seclabel,relatime 0 0

4. Try to copy a single user to /mnt/home with the "rsync -a" command with the "-a" option, which should preserve almost everything associated with each file. But the result shows that SELinux security context is not preserved.

root# rsync -av /home/joe /mnt/home/

root# ls -alZ /mnt/home/joe
   4 joe  joe  unconfined_u:object_r:unlabeled_t:s0 4096 Dec  9 11:48 .
  51 root root system_u:object_r:unlabeled_t:s0     4096 Mar 18 21:18 ..
   1 joe  joe  unconfined_u:object_r:unlabeled_t:s0   18 Nov  9  2019 .bash_logout
   1 joe  joe  unconfined_u:object_r:unlabeled_t:s0  141 Nov  9  2019 .bash_profile
  ...

root# ls -alZ /home/joe
   4 joe joe   unconfined_u:object_r:user_home_dir_t:s0   93 Dec  9 11:48 .
  50 root root system_u:object_r:home_root_t:s0         4096 Mar 12 17:12 ..
   1 joe joe   unconfined_u:object_r:user_home_t:s0       18 Nov  9  2019 .bash_logout
   1 joe joe   unconfined_u:object_r:user_home_t:s0      141 Nov  9  2019 .bash_profile
  ...

root# rm -R /mnt/home/joe

5. Copy all users to /mnt/home with the "cp -aR" command, which truly preserves everything associated with each file.

(this may take some time, if you have a large /home)
root# cp -aR /home/* /mnt/home/

6. Verify the /mnt/home by comparing it with /home. Hope there is no differences.

(this may take some time, if you have a large /home)
root# diff -r /home /mnt/home

7. Rename the old /home as a backup.

root# mv /home /home-bck

8. Re-mount the new partition to /home.

root# umount /mnt/home

root# mkdir /home

root# mount /dev/sdb1 /home

9. Ask Joe to log in and test it out. Hope everything is ok.

10. Restart email system and other background jobs.

root# systemctl start postfix
...

11. Get the "mount" detailed options from the /etc/mtab file:

root# cat /etc/mtab | grep home

/dev/sdb1 /home ext4 rw,seclabel,relatime 0 0

12. Edit /etc/fstab to add the above line, so that /home is mounted automatically at boot time. Remember to use tab key to separate fields in the /etc/fstab file.

root# edit /etc/fstab
...
/dev/sdb1 /home ext4 rw,seclabel,relatime 0 0

13. If the partition is in a GPT partition table, you need to use the UUID of the /dev/sdb1 in the /etc/fstab file.

root# blkid | grep sdb1
/dev/sdb1: UUID="031eff22-0490-4a81-b74d-..." TYPE="ext4" ...

root# edit /etc/fstab
...
UUID=031eff22-0490-4a81-b74d-...  /mnt/home  ext4 rw,seclabel,relatime 0 0

14. Test the /etc/fstab file with "mount -a". Hope everything will be re-mounted ok.

15. Reboot the system to make sure /home is mounted automatically.

Table of Contents

 About This Book

 Introduction to Linux Systems

 Cockpit - Web Portal for Administrator

 Process Management

 Files and Directories

 Users and Groups

 File Systems

 Block Devices and Partitions

 LVM (Logical Volume Manager)

 Installing CentOS

 SELinux - Security-Enhanced Linux

 Network Connection on CentOS

 Software Package Manager on CentOS - DNF and YUM

 Running Apache Web Server (httpd) on Linux Systems

 Running PHP Scripts on Linux Systems

 Running MySQL Database Server on Linux Systems

 Running Python Scripts on Linux Systems

 vsftpd - Very Secure FTP Daemon

 Postfix - Mail Transport Agent (MTA)

 Dovecot - IMAP and POP3 Server

 Email Client Tools - Mail User Agents (MUA)

 LDAP (Lightweight Directory Access Protocol)

 GCC - C/C++ Compiler

 Graphics Environments on Linux

 Conda - Environment and Package Manager

 Tools and Utilities

Administrative Tasks

Move /home Directory to New Partition

 References

 Full Version in PDF/EPUB