29

I have a / partition which contains /var and is too small. I have another existing partition with enough space.

Here is my df:

File system          Size. Occ. Avai. %Ful. Monté sur
/dev/sda1             5,0G  4,5G  289M  95% /
tmpfs                 242M     0  242M   0% /lib/init/rw
udev                   10M  2,7M  7,4M  27% /dev
tmpfs                 242M     0  242M   0% /dev/shm
/dev/sda2              15G  406M   14G   3% /home

How can I move the /var folder from sda1 to sda2 ?

0

3 Answers 3

29

Go into single user mode, and make sure any process writing to /var is stopped. (Check with lsof -n | grep /var)

  • mkdir -p /home/var
  • rsync -va /var/. /home/var/.
  • mv /var /var.old # you can remove /var.old when you are done to reclaim the space
  • mkdir -p /var
  • mount -o bind /home/var /var
  • update your /etc/fstab to make the bind-mount permanent.

/etc/fstab

 /home/var /var        none    bind
14
  • Thks! How can I enter into single user mode? Will 'var' folder stay in the /home?
    – Jérémie
    Sep 19, 2012 at 16:48
  • Yes there would always be a /home/var directory if you solve the problem this way. One way you can get into single user mode by rebooting, and choosing single user mode from the Boot menu.
    – Zoredache
    Sep 19, 2012 at 16:51
  • I am not really sure if this will work fine all the time. If you are using a more recent version (wheezy) you may be OK as it has a /run filesystem. OTOH: early in the boot process files can get opened in /var before it is bind mounted.
    – cstamas
    Sep 19, 2012 at 17:09
  • 1
    @cstamas, Having /var on a separate filesystem is supported, and always has been. Having /var on a separate is even suggested as being a good thing in FHS, and the official Debian docs.
    – Zoredache
    Sep 19, 2012 at 17:37
  • Ok, I think you are right. It was just strange to me to do this with bind mounts.
    – cstamas
    Sep 19, 2012 at 18:41
6

You can also use:

 mkdir /home/var
 <move contents of /var to /home/var -- however you want; EX: mv /var/* /home/var>
 mv /var /var.old
 ln -s /home/var /var

This seems a lot easier than messing around with the fstab and mount stuff.

5
  • 1
    Not sure why anyone would have down-vote this. This would work perfectly fine. A sym-link should work fine. I just use bind mounts for other things, so I tend to think of those first.
    – Zoredache
    Sep 19, 2012 at 19:11
  • 1
    To add to this, it may not be as safe, but it does in fact work. I've recently done this on a machine prior to placing it in production. Iw ould use caution if it's a machine that's been in use or has a lot of processes running. In my case, it was a fresh server meant to run only tomcat.
    – A.J. Brown
    Aug 9, 2013 at 17:54
  • 1
    Symlinking /var works, but instead of doing so I prefer to put an entry in /etc/fstab, to remember myself on which partition/volume I put what, and why (you can add comments). This way is also more understandable to me what I have to change in configuration if I need to change something in my hardware.
    – gerlos
    Mar 4, 2014 at 9:15
  • 1
    I can't see how this would work. I want it to work, but basic things like init use /var, don't they?
    – Sue Mynott
    Nov 4, 2016 at 11:40
  • I just tried this here on my arch linux, since this was also my typical approach. But it fails. The system does not boot when your var is a symlink to a mount defined in fstab (like /home here). Var has to be bind-mounted, see accepted answer. Aug 17, 2022 at 11:37
6

Move /var without changing into single-user mode

When I took over a new virtual server that had been provisioned for me by my employer’s hosting company, I created extra logical volumes for var and home which had been regular directories in the root partition. Since the virtual server provider didn’t provide a KVM-like interface by which I could access the server in single-user mode, the above answers were not applicable to my setup. I hope this answer is useful for others in a similar situation (I've kept the LVM details but these can be skipped as it’s not particularly relevant whether the new filesystem is created on a logical volume or a disk partition).

Create and use a new /var filesystem with LVM

Create the filesystem for the new var volume, mount it (using a temporary directory) and copy files from the current /var to the new filesystem. When copying files with rsync, use its -a, --archive option to preserve time-stamps, ownership, modes, etc. and its -X, --xattrs option to preserve the extended attributes such as the security labels used by AppArmor and SELinux.

sudo lvcreate -L 60GB -n var VolGroup00
sudo mkfs.ext4 /dev/VolGroup00/var
sudo mkdir /var.new
sudo mount /dev/VolGroup00/var /var.new
sudo rsync -raX /var/ /var.new/

Update the filesystem table

Configure the new filesystem to be used as a new mount-point for /var by adding the following line to /etc/fstab. Note that 0 is used as the pass number (last field) so that the filesystem won’t be automatically checked (fsck) after a certain number of reboots (I’ve no access to log in to the server in single-user mode).

/dev/mapper/VolGroup00-var    /var    ext4  defaults  0 0

Since I can’t change into single-user mode, reboot the computer to use this new volume as /var.

Recover disk space from the root filesystem

After the machine has restarted, carry out the following steps to clean up the temporary directory and remove the old /var files from the root filesystem:

  1. Remove the temporary mount point:

    sudo rmdir /var.new
    
  2. Create a new mount point to create an alternative path to the files on the old /var directory on the root filesystem (it’s currently “masked” by the new /var filesystem mounted on the directory):

    sudo mkdir /old-root
    sudo mount /dev/mapper/VolGroup00-root /old-root/
    sudo rm -rf /old-root/var/*
    sudo umount /old-root/
    sudo rmdir /old-root/
    
1
  • Do you think it's safe to have VolGroup00-root mounted twice as Read-Write ? IMO it would corrupt most of commodity FSes.
    – saulius2
    Mar 17, 2022 at 8:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .