27

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 ?

Jérémie
  • 373
  • 1
  • 3
  • 5

3 Answers3

25

Go into single user mode, and make sure any process writing to /var is stopped. (Check with lsof | 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
Amedee Van Gasse
  • 308
  • 3
  • 18
Zoredache
  • 128,755
  • 40
  • 271
  • 413
  • Thks! How can I enter into single user mode? Will 'var' folder stay in the /home? – Jérémie Sep 19 '12 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 '12 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 '12 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 '12 at 17:37
  • Ok, I think you are right. It was just strange to me to do this with bind mounts. – cstamas Sep 19 '12 at 18:41
  • I'm on Debian, Linux version 2.6.32. I have no access to a boot menu as this is a hosted server... – Jérémie Sep 20 '12 at 08:09
  • My home is a software raid `md` device. How can I make sure that `md0` comes up before binding occurs? – Lord Loh. Oct 16 '14 at 08:09
  • @Jérémie From the command line you can go to single-user mode so that there isn't rw activity on the directory during the process by typing *init 1* – Denja Jun 17 '16 at 01:07
  • I can't rename `/var` because the "resource is busy" :/ There is no output with `lsof | grep var` nor with `ps aux | grep var` – aliopi Dec 06 '16 at 09:36
  • @aliopi - you'll need sudo in front of it as some of the processes with open file handles to /var will run under root – James B Nov 02 '19 at 18:41
  • I'm on Debian 12 and this caused errors when loading several drivers, such as iwlwifi. I dodged the problem by using the above solution on very specific directories, like `/var/lib/postgresql` (which takes around 60% of the space used by /var in my config) – gogaz Mar 07 '21 at 18:21
5

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.

mdpc
  • 11,698
  • 28
  • 51
  • 65
  • 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 '12 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 09 '13 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 04 '14 at 09: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 Spence Nov 04 '16 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. – Ralf Ulrich Aug 17 '22 at 11:37
5

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/
    
Anthony Geoghegan
  • 2,800
  • 1
  • 23
  • 34
  • 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 '22 at 08:05