We’re Moving Home (/home)

Sometimes you may need to move folders to another disk or partition, due to needing more space or for safety reasons. Most often this is /home folder which is likely to be by default installed on the same partition with everything else, this is /root.
Here I’ll describe how to move a folder to another partition/disk and then mount it to where its expected to be. The (basic) process is easier then you may think – and I’ll try to keep it this way!

We will do this with an example of /home. It’s advisable to read it all before attempting to move the data.

First we need a partition or disk that will hold our /home folder content. This could be tricky if you don’t have any spare disks/partitions, or at least some unassigned space. In such case the only way may be booting up from LiveCD, USB stick or another disk and changing the partitioning of the drive where our linux lives and rules. It shouldn’t be a problem to get some LiveCD with Gparted on it – which is a nice and easy to use partitioning tool. I’m not going to cover here how to change partitioning of your disk as that’s a topic for separate post.

My partition is called sda4 and this is what I’ll use in all commands below, but you need to check what your partition name is and replace it wherever it’s needed.
Another thing to note is that you need to become ‘root’ for the time of this work, so log in as root, or run

sudo su-

Once you have the space ready to take some files on board mount it:

mount /dev/sda4

Remember to change sda4 to your new drive/partition. This will mount the drive to the default location which is /media folder (but if it’s not there check /mnt as well – or even mount it to your own folder by passing path to it as second argument).

Now copy everything from your /home directory into that location:

cp -rpfv /home/* /media/sda4/

This will copy everything from your home folder to the new drive/partition. The important bits are the options following cp: -r is for recursive copying, -p is to preserve all of the permissions. These two are really important, without them you would copy only a small portion of your data from /home directory and all permissions and ownership would have been set now to root. Then there is -f to force copying which is there just ‘in case’ and -v so you can see what’s actually being copied. Note that I’m not copying /home itself, just it’s content.

Once you have it all there and you have checked it twice, it’s time to add a line to /etc/fstab. Use your favourite editor, I’ll suggest here gedit as I’m using Gnome and want to keep things simple: write in terminal:

gedit /etc/fstab

And once in editor add this onto the end of the file replacing sda4 with your partition and ext4

/dev/sda4    /home    ext4    defaults,errors=remount-ro    0    1

Just to make it as simple as can be, go to the newly created partition and create a file that doesn’t exist in the original /home. Eg you can run this command:

touch /media/sda4/new-home

So when it comes to delete the old home you won’t be troubling yourself too much if you delete the wrong one.

Now just restart the system and after logging in check if everything starts and works as expected. Now when you enter /home you should see the “new-home” file there. If this is the case and there was no error you can attempt to delete the old /home.

To do so log out from the Gnome session and on the login screen press CTRL+ALT+F1. This will let you login into shell now. Log yourself in as usual and then perform the following commands:

1. cd / (unlock the home folder by exiting to root)
2. sudo su – (become a root)
3. ls /home (see if new-home file is listed, it should be there)
4. umount /dev/sda4 (if new-home file was listed then un-mount new home)
5. ls /home (make sure that new home was un-mounted – the new home file shouldn’t appear this time)
6. rm -fr /home (if new home has been un-mounted successfully then delete the old home directory)

If this sounds risky, or you want to make sure once more that everything will go alright, then instead of removing it in point 6 rename the old home folder by running this:

mv /home /home-old

Now restart. Once logged in, if you haven’t deleted the old home yet you can do so now.

And done! So how do you like your new place?

P.S. Be extra careful when moving your data. I have written this with the best intentions and it worked for me, however, I can’t take any responsibility for any data loss caused by following this guide.

This entry was posted in Development. Bookmark the permalink.

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>