Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 8 down vote accepted

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
share|improve this answer
1  
You will still have the partition that was /var but you can use a partition manager to fix that –  Yuugian Nov 14 '12 at 14:31
    
Could you explain how to correct /etc/fstab? –  Bakuriu Nov 14 '12 at 14:36
    
in /mnt/root/etc/fstab (before you reboot), remove the line that looks similar to /dev/sdb1 /var –  Yuugian Nov 14 '12 at 14:44
1  
I don't have any line that starts with /dev/sd* but I have a line that starts with UUID=5ebc2009-3089-4f5b-ac93-d8d4738913b3 /var ext4 defaults which I believe refers to the /var partition. So I simply remove this without modifying anything else? –  Bakuriu Nov 14 '12 at 14:47
2  
I think the recipe above won't work without an extra step. Inside /mnt/root there will be a var directory to serve as a mountpoint for the /var partition. Presumably it will be empty. It needs to be deleted before step 5, else the cp command will copy the /mnt/var directory into /mnt/root/var/, rather than making a copy of it as /mnt/root/var, as desired. (I suggested an edit to this effect, but it was rejected.) –  dubiousjim Nov 14 '12 at 15:32

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

share|improve this answer
    
The offline or single-user mode way is safest - it's only worth trying to attempt a live changeover if minimizing downtime is important. –  jw013 Nov 14 '12 at 19:58

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

share|improve this answer
4  
I strongly doubt this works. At least reading @rozcietrzewiacz's answer I believe this will break your system. –  Bakuriu Nov 14 '12 at 19:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.