Partitioning in action: Moving /home

A step-by-step partition moving how-to

In this new series of tips, Daniel Robbins shows you how to change partition layout on a running system. He'll also cover several tricks of the trade to minimize downtime and avoid making costly mistakes. In this particular tip, he'll show you how to move /home to another partition.

Share:

Daniel Robbins (drobbins@gentoo.org), President and CEO, Gentoo Technologies

Residing in Albuquerque, New Mexico, Daniel Robbins is the Chief Architect of the Gentoo Project, CEO of Gentoo Technologies, Inc., the mentor for the Linux Advanced Multimedia Project (LAMP), and a contributing author for the Macmillan books Caldera OpenLinux Unleashed, SuSE Linux Unleashed, and Samba Unleashed. Daniel has been involved with computers in some fashion since the second grade, when he was first exposed to the Logo programming language as well as a potentially dangerous dose of Pac Man. This probably explains why he has since served as a Lead Graphic Artist at SONY Electronic Publishing/Psygnosis. Daniel enjoys spending time with his wife, Mary, who is expecting a child this spring. He can be reached at drobbins@gentoo.org.



01 May 2000

Also available in Japanese

The partition /home is one of the most-often-moved partitions. Sometimes, all the space in /home becomes exhausted, and an additional hard drive is required. Other times, /home is set up as part of the root partition, and it may need to be moved to improve performance or facilitate backup. Whatever the case, I'll show you how to move /home safely and efficiently.

WARNING: The following technique describes how to move a partition(s). Although this technique is designed so that you can "back out" of a failed partition move, it doesn't protect against user error. In other words, any time that you format partitions or copy large numbers of files, there's a possibility that you will type the wrong thing, causing lots of data to be destroyed. For this reason, it's highly recommended that you take appropriate steps to back up all critical files before proceeding.

Now that you're prepared, we're ready to start moving /home. The exact steps you will take depend on whether /home currently resides on its own separate partition, or whether it is located on the root partition. Keep this in mind as we go through the steps (I'll make notes where necessary). Right now, if you are moving /home to a new hard drive, it should be physically installed in your system.

1. Create a new partition if necessary

If you are moving /home to an existing partition (it doesn't need to be ext2, as long as the target primary or extended partition exists), you can proceed to step 2.

If the new partition doesn't exist yet, you'll need to create it using cfdisk (preferred) or fdisk. If the partition doesn't reside on your first drive, remember to specify the name of the device as the first argument to cfdisk or fdisk. After creating the appropriate primary or extended partition, you should reboot so that the partition table can be reread correctly. This is the only time you will need to reboot the system.


2. Create a filesystem on the new partition

To create a filesystem on the new partition, first make sure you know the exact device name for the new partition (for example, /dev/sda5). If you're not sure of the exact device name, stop now and double-check! Then type the following, as root:

# mkfs.ext2 /dev/???

In the above and following code samples, ??? should be replaced with the target partition name. After executing this command, the target partition will contain an empty ext2 filesystem.


3. Mount the new filesystem in /mnt

Create a directory called /mnt/newpart, and then mount the new partition there:

# mount /dev/??? /mnt/newpart

4. Drop to single-user mode

I delayed this step as long as possible to maximize system availability, but we now must drop into single-user mode, and copy files from /home to /mnt/newpart. You shouldn't have any files open in /home, and entering single-user mode eliminates this possibility:

# init 1

If prompted, enter the root password to perform system maintenance. You should now have a root shell.


5. Change directories to /home and copy files

Type the following:

# cd /home # cp -ax * /mnt/newpart

The cp -ax command recursively copies the contents of /home to /mnt/newpart, preserving all file attributes, and not crossing any mount points. After this command finishes, /mnt/newpart will contain an exact copy of all the files and directories currently in /home. If the old /home was on its own separate partition (listed on a separate line in /etc/fstab), go to step 6a. Otherwise, proceed to step 6b.


6a. Use the new partition (when old /home is a partition)

These instructions are for systems where the old /home is already on its own dedicated partition. If this isn't the case, see item 6b.

Unmount the old partition by typing:

# cd / # umount /home

Then, unmount and remount the new partition:

# umount /mnt/newpart # mount /dev/??? /home

Now, the new partition is available at /home and is ready to be used. We can perform the last few steps in multiuser mode. Exit single-user mode, so that the system is back up and running, by pressing CTRL-D.

Important: After the system starts up normally, log in as root and edit /etc/fstab so that /dev/??? is now mounted automatically at /home instead of your old partition. For example, change this line:

/dev/hda3 /home ext2 defaults 1 2
/dev/??? /home ext2 defaults 1 2
# cd / # mv /home /home.old # mkdir /home # mount
                /dev/??? /home

Now, leave single user mode by pressing CTRL-D. When the system is back up and running, edit /etc/fstab and add a line like the following:

/dev/??? /home ext2 defaults 1 2

That way, your new partition will get mounted correctly the next time the system is rebooted.


7. Finishing up

We deliberately left the old /home directory/partition behind, just in case there were problems copying files. After verifying that the system is running smoothly, you can either use your old /home partition for something else, or remove the /home.old directory.

Congratulations, you've just moved /home! In my next tip, we'll reconfigure a system so that /tmp and /var are on their own shared partition. See you then.

Resources

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Linux on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Linux
ArticleID=11004
ArticleTitle=Partitioning in action: Moving /home
publish-date=05012000