Remove /var partition and replace it with /var into / (root) partition

I accidentaly created a /var partition and I’d like to get rid of it. Is it possible to remove this partition and use the / partition to contain /var without reinstalling the OS or do I have to repartition my harddrive and reinstall everything?

In the latter case is there something I can do to reinstall the already installed updates/programs automatically in the new system or do I have to list everything I add to the system and manually reinstall it?
My /home is on a separated partition so probably it could be reused by the new installation, even though I care much more for what I installed into /.

The OS is Kubuntu 12.10.

Asked By: Bakuriu

||

You can boot the machine with a live CD OS. This will allow you to move /var without corrupting the OS.
I have done this in the other direction with /tmp, /var, /opt, and /usr on a SLES install. I think it would work on others distros.

  1. Boot the live CD
  2. Mount the old /var partition in /mnt/var
  3. Mount the real root directory in /mnt/root
  4. Correct /mnt/root/etc/fstab
  5. Remove the old mount point with rmdir /mnt/root/var
  6. Run a cp -a /mnt/var /mnt/root/var
  7. Boot the real OS
Answered By: Yuugian

Yuugian’s answer is definitely the safe and proper way to go. But for some people, it might be good to know that you can actually do that without even shutting down your running system. You can treat this answer as a training or last resort in situations where booting from a liveCD is not an option.

Disclaimer: This procedure is not recommended for beginner users. If you don’t put enough attention to step 1, your system might crash while you perform next steps and you can experience unexpected behaviour of various applications after reboot. Before you attempt this, you should check that you’re familiar with /var description on tldp and your distribution-specific deviations from that.

  1. Close programs that have open files under /var. Start by switchin to console (one of the “real” ttys) and shutting down the X server, as well as the Desktop Manager (kdm/gdm/lightdm/…), if you use one. If you don’t know how to do that or what it means, do NOT continue – chances that you might break you system are too high, and there are just too many possibilities to cover in a step-by-step procedure here – sorry!

    Use lsof or fuser to find remaining programs that have files open under /var. Notice that if a program was started as/by a system service, it is always better to stop the service, rather than killing the task. If there are some programs/services too stubborn to deal with this way, you might also try switching to single-user mode (init level 1) – if that is acceptable for you. In most cases it would be OK to have a few(!) files still open under /var – for instance the pid files under /var/run, in case your system uses that location.

  2. Mount your root partition (for the second time). Simply choose any empty directory as a temporary mount point for that – I’ll use /mnt/tmp as an example. Usually the mount command (without parameters) will not display the real device name behind your root partition, you will need to check which one is it (unless you know for sure). You can usually get the information either from lsblk (if you have it on your system), or bootloader configuration, or by cat /proc/cmdline, or by identifying the partition by size reported by fdisk.

  3. Copy /var contents. Once you’ve found the partition and mounted it, copy the contents of /var to your rootfs (mounted in the new location), using cp -ad, such as: cp -ad /var /mnt/tmp/. Note: if your distro still manages /var/run and /var/lock directories instead of recently introduced /run/, you should skip the contents of these two directories. If extglob is enabled in your shell, you can do that using cp -ad /var/!(run|lock) /mnt/tmp/var/ – or use cpio instead. Just to create the two directories under /mnt/tmp/var/ and set their permissions properly after copying.

  4. Remove old /var entry in fstab. The safe way is to comment it out, of course.

  5. Reboot

Answered By: rozcietrzewiacz

Under the root:

cp -rf /var /var-new && umount /var && mv /var /var-old && mv /var-new /var

afterwards remove or comment out /var mountpoint from /etc/fstab and reboot

Answered By: cjayho
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.