Bienvenido! - Willkommen! - Welcome!

Bitácora Técnica de Tux&Cía., Santa Cruz de la Sierra, BO
Bitácora Central: Tux&Cía.
Bitácora de Información Avanzada: Tux&Cía.-Información
May the source be with you!

Saturday, August 23, 2008

LX: moving /home

Sources: 1, Howto migrate user accounts-old to new server

1. Drop to single-user mode
Create a new partition, if necessary
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:
Code Listing 2.1: Creating the filesystem
# 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:
Code Listing 3.1: Mounting the partition
# mount /dev/??? /mnt/newpart

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:
Code Listing 4.1: Entering single user mode
# 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:
Code Listing 5.1: Copying files
# 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.

6. Use the new partition

6a: /home on its own partition

Note: These instructions are for systems where the old /home is already on its own dedicated partition. If this isn't the case, see step 6b.
Unmount the old partition by typing:
Code Listing 6.1: Unmounting
# cd /
# umount /home

Then, unmount and remount the new partition:
Code Listing 6.2: Remounting the 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:
Code Listing 6.3: Old fstab
/dev/hda3 /home ext2 defaults 1 2

to this line:
Code Listing 6.4: New fstab
/dev/??? /home ext2 defaults 1 2
6b: /home on a shared partition

Note: These instructions are for systems where the old /home is on a shared partition.
Code Listing 6.5: Moving the directory
# cd /
# mv /home /home.old
# mkdir /home
# mount /dev/??? /home

Important: 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:
Code Listing 6.6: Editing fstab
/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.

No comments: