English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Operation Method to Move Home Directory to a New Partition Without Changing Directory Structure in Linux

Problem proposed

The company's development and test server is deployed on Alibaba Cloud, and the instances provided by Alibaba Cloud are generally only one partition, 20G to40G's appearance, and then buy storage and mount it to other directories.

And the home directory is in this 20G of the root directory partition. As the number of developers increases, the root directory partition is soon filled up by everyone.

Because it is developed in multiple places, a non-perceptible home moving plan for everyone is needed.

Basic principle

Upon receiving this task, the first thought is to use the bind mount method:

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

It can achieve an imperceptible move for home.

It feels that the search for Chinese does not explain this command very clearly.This Q&A It is explained more clearly, and those who are good at English can refer to it.

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

Specific operations

First, choose a time when it's quiet and late at night, use the who command to check which children are still lingering on the server and call them one by one to clear the scene:

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

After making sure there is no one around, start moving (copying). When using the cp command to copy, remember to include -The p parameter retains the file permission settings. Use root privileges, assuming the target partition is /new_disk :

# cp -p -r /home /new_disk/

The new home has been set up, so let's assign a different doorplate number to the old home first to avoid losing the doorplate number after assigning it to the new home. Using mount --bind to mount the original home directory to a new directory:

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

At this time, we can mount the original home directory to a new directory using /Find all the files of the old home under the directory home_bkp. You can keep /home, this doorplate number is given to the new home:

# mount --bind /new_disk/home /home

Moving in completed! You can inform your friends to start working happily. In fact, you can choose not to inform everyone, as moving house is actually imperceptible to everyone.

Of course, some students may ask, the new home and the old home have the same doorplate number, both are /home, how do I know if the move was 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

-h for human-readable, not adding it is also okay, the ones listed are 'anti-human' blocks.

The above command, first go and see the old home, the mount point is / ; Go and see the new home, the mount point is /new_disk. Now you can be at ease.

Persistence

The work is not done yet. The work above only ensures that the new address is changed for the current new home. But after the restart, this information will be lost and the home address will change back. We need to find a way to retain this information.

Modify /etc/The fstab file is enough. 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

With this, the entire home partition switching work is completed.

Postscript

This plan was thought of on a whim and no precedents were found. I don't know if there will be any pitfalls... Currently, the server under this plan has been running for nearly a year with no problems, everyone is working happily, and even many people don't know about this matter.

The directory at home /home_bkp is implemented in this plan1After a month, seeing no problems, it was cleared out to make space.

Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace '#' with '@' when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like