migrating/moving /usr and /var

Space problem with /var and /usr , The server is running mysql .

My Plan is to move my /usr and /var to “root” (/). To do that I have to make sure that all security settings and symlink are preserved.

Prepare the new partitions
In my case, this is simply my “/” partitions, so what i really do is just create a new folders on “/”.

Backup the /usr and /var partitions
I will boot up the server in rescue mode using the installation CD, and use rsync to move them out of the way.
Create folders to backup the content of /usr and /var on the target partitions, in my case it the “/” partition.

#mkdir /var1
#mkdir /usr1

If you’re planning to relocate the folders to new partitions, you can label and mount them on these folders.

#e2label /dev/hda5 /var1
#e2label /dev/hda6 /usr1

#mount /dev/hda5 /var1
#mount /dev/hda6 /usr1

Backup your /etc/fstab file so that you can roll back your change easier.

#cd /etc
#cp -p fstab fstab.bk

Edit /etc/fstab so that /usr and /var partition are not mounted on the next reboot.
#vi /etc/fstab

Comment any line that refers to /var and /usr. This is how my fstab files looks like:

LABEL=/ / ext3 defaults 1 1
none /dev/pts devpts gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
LABEL=/home /home ext3 defaults 1 2
none /proc proc defaults 0 0
none /sys sysfs defaults 0 0
LABEL=/tmp /tmp ext3 defaults 1 2
#LABEL=/usr /usr ext3 defaults 1 2
#LABEL=/var /var ext3 defaults 1 2
LABEL=SWAP-cciss/c0d0p swap swap defaults 0 0

Reboot your system using rescue mode with your install CD, or a live cd. On RHEL machine, press F5 on the first screen, and type “linux rescue” at the prompt. your installation will be mounted on /mnt/sysimage.

Get inside the installation by issuing

cd /mnt/sysimage

Copy the content of both folders to their respective backup target

#rsync -avurlp /var/ /var1
#rsync -avurlp /usr/ /usr1

The command above will copy your existing /usr and /var to their respective backup folder, while maintaining all the original attributes. Now we’re ready to replace both of the folders

Replacing the folders after making sure that /mnt/sysimage/usr and /mnt/sysimage/var are empty

As I mentioned above, in my case, I would like to move /var and /usr to root (/), so what I do is I just simply delete /mnt/sysimage/var and /mnt/sysimage/usr, and rename /mnt/sysimage/var1 and /mnt/sysimage/usr1 to /mnt/sysimage/var and /mnt/sysimage/usr. The
command should be like these:

#rm /mnt/sysimage/var
#rm /mnt/sysimage/usr
#mv /mnt/sysimage/var1 /mnt/sysimage/var
#mv /mnt/sysimage/usr1 /mnt/sysimage/usr

Since /var and /usr is now technically under “/”, all I have to do are making sure that /var and /usr are not mounted on a partitions and simply reboot my system. If you’re going to replace the partitions with new ones, you can open the fstab file, and edit them so that it will mount the new partitions as /var and /usr respectively. Open fstab by typing:

vi /mnt/sysimage/etc/fstab

Point /var and /usr to their new partition. Mine will look like this:

LABEL=/ / ext3 defaults 1 1
none /dev/pts devpts gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
LABEL=/home /home ext3 defaults 1 2
none /proc proc defaults 0 0
none /sys sysfs defaults 0 0
LABEL=/tmp /tmp ext3 defaults 1 2
LABEL=/usr1 /usr ext3 defaults 1 2
LABEL=/var1 /var ext3 defaults 1 2
LABEL=SWAP-cciss/c0d0p swap swap defaults 0 0

Also make sure on the new location , there may be little problem with permissions .

# chown -R mysql.mysql /var/lib/mysql
Some times when mysql does not start ,u can start mysql in safe mode, on next reboot it will be started normally.

Now, Save the changes, and reboot. The changes will be active the moment we reboot the system.

After all the steps above the installation should be using/var and /usr on the new area. I need keep the old partitions intact until I’m sure that everything is working properly. You can try running applications that is installed under /usr/bin to check if it’s ok.

If rollback is needed the, simply reboot the system into rescue mode again, and undo the changes from fstab by:

cd /mnt/sysimage/etc
cp fstab fstab.bk2
mv fstab.bk fstab

And reboot the system. /usr and /var should be mounted on the old partitions.

This method should work for different folders. You can also move /home and /tmp using the same steps, and it works flawlessly

This entry was posted on Tuesday, July 6th, 2010 at 8:06 pm and is filed under CentOS, Linux. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “migrating/moving /usr and /var”

  1. Manoj Chauhan Says:

    Good work :)

  2. Spielvogel Says:

    Found your site on Altavista, great content, but the site looks awkward in linux browsers, but works fine in IE. Go figure.

Leave a Reply