CentOS & moving /var to a new disk/partition
A good thing is that your system is probably a VM as can be known by the device file node names (e.g. /dev/vdb
), which will simplify our procedure by excluding the need to use any specialized tool (e.g. iscsi-initiator-utils
) if it came from, say iSCSI storage.
CentOS offers a minimal installation media that includes the most important tool for our purpose, rsync
. If you are on CentOS 6 and use a 64-bit system then you can download the minimal CD ISO image by selecting the mirror of your choice, for instance, this. Then, you could download the minimal ISO from here that has a size under 400 MB. Guidelines on the additional steps are as follows:
Prepare spare disk. Attach the new disk to the VM and format it as usual. You have already written that you know how to do this.
-
Boot into rescue mode. Gracefully shutdown the VM and boot the minimal ISO. One of the installation options offered is Rescue installed system. Selecting it will allow us to boot from the CD without any processes writing to your
/var
directory. This will provide us with a safe environment to transfer data from the old disk to the new one. There are few menus that you will have to navigate before being thrown into a shell prompt. Items in brackets are those that make most sense to me. Please choose ones that you find suitable.- Choose language. [English]
- Keyboard type [us]
- Do you want to start the network interfaces on this system? [No]. We can do this because you are on a VM and the new disk is going to be available without network connectivity.
- The rescue environment... [Skip]. This is a long dialogue. Simply choose skip.
- Start shell
-
Transfer data. Let us say the device node on your new disk, which will store the new
/var
data is/dev/vdc1
and your old root partition, holding the/var
directory is/dev/vda2
. Create temporary mount points and copy the data:mkdir /mnt/var /mnt/var_old mount /dev/vda2 /mnt/var_old mount /dev/vdc1 /mnt/var rsync -a /mnt/var_old/ /mnt/var/
-
Move old /var. Move the old
/var
directory away and create a new empty mount-point:mv /mnt/var_old/var /mnt/var_old/var.old mkdir /mnt/var_old/var
-
Edit fstab. Setup
/etc/fstab
so that the filesystem on the new disk gets mounted on/var
. Given the above device nodes, add an entry such as the following to/mnt/var_old/etc/fstab
:/dev/vdc1 /var ext4 defaults 1 2
Reboot. Type
exit
on the shell prompt and select to reboot the VM. Then boot into your regular OS installation and not the minimal CD.
If everything went fine then your old /var
data would be present in /var.old
and /var
would contain all that data and be ready for use. You might, if you so desire, remove /var.old
after a few days of normal operation.