linux moves the home directory to a new partition without changing the directory structure

  • 2020-06-19 12:35:25
  • OfStack


The company's development and test servers are deployed in Aliyun, and the example 1 given by Aliyun generally looks like one partition, 20G to 40G, and then buys storage to mount to other directories.

The home directory is under the 20G directory partition, and as the number of developers grows, the root directory partition quickly fills up.

Because it is multi-site research and development, so we need a perceptual home moving scheme for everyone.

The basic principle of

The first thing that comes to mind is the bind mount approach:

mount --bind /some/where /else/where

You can move home without perception.

It seems that the explanation of this command in Chinese search is not very clear. This essay is quite clear, for those who are good at English, please refer to it.

Of course, you can also ask the man: man mount

Specific operation

First of all, in the dead of night, use the who command to check if there are any children still on the server. Call 11 to clear the scene:

$ who
Frodo  pts/0    2017-04-17 09:07 (
Sam   pts/1    2017-04-18 08:45 (
Pippin  pts/3    2017-04-18 09:06 (
Merry  pts/4    2017-04-18 09:07 (

When using the cp command to copy, remember to take the -ES38en parameter and keep the file permission setting. Using the root permission, assume the target partition is /new_disk:

# cp -p -r /home /new_disk/

The new home has been built, we first to the old home to get another number, in order not to give the new home number, can not find the old home.

# mkdir /home_bkp
# mount --bind /home /home_bkp

At this point, you can find all the files in your old home under /home_bkp.

# mount --bind /new_disk/home /home

The move is complete! In fact, you can not inform everyone that the move is actually insensitive to everyone.

Of course, there are students asked, new home home number 1, is /home, how do I know whether the move is successful ? You can use the df command to confirm:

# cd /home_bkp
# df -h .
Filesystem   Size Used Avail Use% Mounted on
/dev/xvda1    20G  17G 2.1G 90% /
# cd /home
# df -h .
Filesystem   Size Used Avail Use% Mounted on
/dev/xvdb1   296G  42G 240G 15% /new_disk

-ES65en is ES66en-ES67en, or if not, it lists "anti-human" blocks as units.

Above the command, first go home to see, the hardpoint is /; If you go to your new home, the hardpoint is /new_disk.


That's not the end of the job. All the above work is to ensure that the current home address is changed. But after restarting, this information will be lost and the home address will be changed back. We need to find a way to keep this information.

Just modify the /etc/fstab file. Open this file and add the following two lines at the end:

/home      /home_bkp none bind 0 0
/new_disk/home  /home   none bind 0 0

This completes the whole home switching partition work.


This plan comes up on a whim, and there is no precedent for it. I don't know if there will be a pit... At present, the server under the scheme has been running for nearly a year, there is no problem, everyone is very happy to work, even many people do not know there is this thing.

The directory /home_bkp was cleared out after a month of the scheme.

Related articles: