Moving /usr & /var to another partition

We recently bought a couple HP blade servers. Two of the server have RHEL AS 4 as OS, and will be hosting our HR application. After one and a half day session of up2date, the two RHEL installation are updated.

..And came the bad news. Apparently the HR application requires a much larger space for installation than previously thought. So I was told to do anything necessary to make the requires space auto-magically exist.. sort of..

My plan is to move some folder from their respective partitions, and merge the into one single large partition. The only available option for that is to move my /usr and /var to “root” (/), user the space for the installation. The merging part can easily be done using LVM2. That wasn’t the case for migrating /usr /var. To do that I have to make sure that all security settings and symlink are preserved.

Now, after searching for sometime on google for hints, this is what I do:

Prepare the new partitions
In my case, this is simply my “/” partitions, so what i really do is just create a new folders on “/”. If you want to move the folders to new partitions, you can use tools like gparted to help you.

Backup the /usr and /var partitions
I have several options on how to do this. The first one is just to tar both of the folders, since with root privileges, tar will preserve every attributes of the folder. Or I can boot up the server in rescue mode using the installation CD, and use mv to move them out of the way. Instead i choose to move them by copying. 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

Copy the content of both folders to their respective backup target

cd /var
find . -depth -print0 | sudo cpio --null --sparse -pvd /var1 
cd /usr
find . -depth -print0 | sudo cpio --null --sparse -pvd /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

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

#cd /etc
#cp fstab fstab.bk

Edit /etc/fstab so that /usr and /var partition are not mounted on the next reboot. Open your fstab file by issuing

#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. When asked if you want to mount your installation as read-write, press “Continue” so that you can make changes to your installation in rescue mode. your installation will be mounted on /mnt/sysimage.

Get inside the installation by issuing

cd /mnt/sysimage

and make 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

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. I decided to move /home and /tmp using the same steps, and it works flawlessly


14 Comments Add yours

  1. says:

    ada bedanya gak kalau pake cp -a dari sisi kecepatan dibandingkan dengan cpio , saya gunakan freebsd mau pindahin /usr


  2. Ikhsan says:

    Wah kalo dari kecepatan gw ga bisa komen deh.Lah dijalaninnya sambil ditinggal makan malam hehehe

    Dan gw juga belum nyoba pake cp -a + rekursif

  3. Aaron D Nelson says:

    I’m actually surprised this worked. With my system- reboot results in a suite of failures (syslogger, mysqld, httpd, and so forth) For some reason, the whole system is borked when I replace var using this method. I see no difference in the filesystem after doing the cpio trick. I can put the old var back in and everything is happy again. very surreal. Did yours actually boot clean???

  4. Ikhsan says:

    Yes, my system reboot fine. I do stumble when trying to move /temp, but that’s because access issue. After CHMODing the correct parameters, everything works well.

    My example is for moving /var and /usr to root(/)partition. Are you trying to do the same thing? If you have prepare new partition for them, you should make the right changes on your fstab.

  5. Frank says:

    This should be done while the system is in init 1, single user mode, so there is no process writing to anyhting, except the commands you give.

    I used the find command to copy my /var, but i added some steps. The following might help someone:

    go into single user mode (init 1)
    create the partition
    mkfs.ext3 the partition

    mkdir /var1
    mount /dev/ /var1
    cd /var
    find . -depth -print0 | cpio –null –sparse -pvd /var1
    cd /
    mv /var /var.old
    umount /var1
    mv var1 var
    mount /dev/ /var
    vi /etc/fstab
    <add the line: /dev/sda3 /var ext3 defaults,noatime 0 2
    init 5
    test if your system works

  6. joo says:

    Hello, i have try you method but after copy the files to the new partition, mounted, reboot and it failed. Saying writing to the /var/run/ is fail. Do you have any idea?

    1. ikhsan says:

      Did you create a new partition for the /var? Or did you just copy it to the root partition like I do?

  7. webpole says:

    Hello, after I do what you described befor, then the auditd won’t start at boot and system logger freezes a long time.
    Any idea to fix it?
    OS: CENTOS 6

    1. ikhsan says:

      Hi, can you tell, at what step you are now, and what you did to your system. If you’ve made change to your fstab, try booting to single user mode, or use a live cd to boot and get to your linux partition and undo the change

    2. ikhsan says:

      Any error message?

  8. panzontli says:

    Maybe your problem is selinux
    in the “newest” versions selinux write a label on almost all files content in the directory /var
    So at first you need to disable selinux ,
    then on your “new reboot” (with /var content in the new partition) all services will be able to start
    Then with the new partition mounted in /var you should enable again selinux
    Fedora explain here how to enable/disable selinux

    1. dren says:

      Thx panzontli for the selinux trick! It spared my time (and nerves)
      Big thanks.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s