6

At a server that I only have access via ssh and vnc to (which means no booting from a "live" CD/DVD is possible), I would like to shrink the partition assigned to /var (in order to free some space for an additional partition). What would a good and safe way to do it?

Solution (based on David Spillet answer, provided that you have enough space on / to accommodate a copy of /var):

  1. stop anything that is using /var where possible
  2. remount /var readonly for good measure mount -f -oremount,ro /var (didn't seem to have any effect for me)
  3. copy /var to / under a different name with mkdir /vartmp; cp -av /var/* /vartmp/
  4. comment out the entry for /var in /etc/fstab
  5. reboot
  6. move things around with mv /var /varmount; mv /vartmp /var
  7. reboot, partition
  8. uncomment the entry for /var in /etc/fstap
  9. reboot
7

If you have room on your root partition for the contents of /var you could:

  1. stop anything that is using /var where possible
  2. remount /var readonly for good measure mount -f -oremount,ro /var
  3. copy /var to / under a different name with mkdir /vartmp; cp -av /var/* /vartmp/
  4. move things around with mv /var /varmount; mv /vartmp /var
  5. comment out the entry for /var in /etc/fstab
  6. reboot

You should now have /var in place as-was on your root partition instead of its own. You can now resize the old partition as needed. Step 4 should let you rename the /var directory even though it is in use as a mount-point and otherwise busy (and proceses with open files there will track the change as the open files are not referred to by path+name, but instead by inode, once open).

If you don't have space on / but do on /someotherfs then you could try move it there with a symlink in / like so:

  1. stop anything that is using /var where possible
  2. remount /var readonly for good measure mount -f -oremount,ro /var
  3. copy /var with cp -av /var /someotherfs/var
  4. move things around with mv /var /varmount; ln -s /someotherfs/var /var
  5. comment out the entry for /var in /etc/fstab
  6. reboot

This is slightly more risky though as you need to be sure that /someotherfs gets mounted before /var when booting.

If you don't have room where you want to copy it to temporarily you might be able to reduce the size of /var by removing things like cached packages (aptitude clean on Debian style setups, there is no doubt an equivalent yum command), moving other stuff away (for instance, Debian keeps the default httpdocs in /var/www so if it is still there and you have lots of data in there, move it to another partition), and deleting files from /var/log that are not very recent (backing the up first, in case you do need to refer to them later).

As Zoredache says: what ever you do, make sure you are happy with your backup arrangement before proceeding.

Caveat: all the above is from memory, I've not tested it anywhere, follow at own risk!

| improve this answer | |
  • Thanks for your effort explaining! Followed your first scenario with a little modification: couldn't rename /var to /varmount on step 4 ("device busy" - probably because /var is mounted straight to a partition), so first did 5), rebooted, and then "moved things around" (and rebooted again). – user25298 Dec 2 '09 at 19:30
  • I was sure it would let you move it (I've deleted a whole directory structure while processes had files open within it before now) - you (re)learn something every day! – David Spillett Dec 3 '09 at 9:29
  • NB I don't believe these instructions will work on a modern version of Debian. – Sue Spence Nov 4 '16 at 11:42
1

The traditional way is to boot in single-user mode. That way, /var won't even be mounted by default, so you can mess around with it all you like.

| improve this answer | |
  • What would be the command to reboot in single-user mode? – user25298 Dec 2 '09 at 19:58
  • I'd have to Google for the details, it's been a while. It's not hard to find. – reinierpost Dec 7 '09 at 13:55
  • I've added a link. – reinierpost Aug 30 '17 at 10:10
0
  1. stop any services that may be using var (lsof | grep "/var")
  2. umount /var
  3. shrink /var
  4. re-mount /var
  5. restart the services you've stopped at 1.
| improve this answer | |
  • 1
    Stuck at step 2.: lsof | grep "/var" shows no services anymore (all killed), yet trying to unmount /var I get this: "umount: /var: device is busy" – user25298 Dec 2 '09 at 17:53
  • 1
    You forgot step 0. Make sure your backups are up-to-date and functional. – Zoredache Dec 2 '09 at 18:00
  • Try "umount -f" -- just make sure your current working directory isn't somewhere under /var or your shell will get killed (never hurts to have a backup login shell standing by, especially on remote machines). – Geoff Fritz Dec 2 '09 at 18:06
  • 1
    @Geoff, here's what I get: ~ sudo umount -f /var umount2: Device or resource busy umount: /var: device is busy umount2: Device or resource busy umount: /var: device is busy It didn't kick me out, but didn't do the unmount either. Earlier I tried "fuser -km /var" - that did kick me out (although I wasn't anywhere under /var), so, obviously, my ssh session depends on /var, too. – user25298 Dec 2 '09 at 18:14
0

If you have access to the console (e.g. keyboard-video-mouse via ethernet) then you can do:

  1. init 1 (and give root-password)
  2. maybe a systemctl stop systemd-timesyncd is required only if you use it
  3. umount /var
  4. e2fsck /dev/volume-where-var-is-in-your-system
  5. resize2fs /dev/volume... to whatever (in my case I wanted to shrink it so I used -M)
  6. do more stuff (lvresize or whatever)

then reboot

| improve this answer | |
0

Single user is the safest bet:

These instructions cover resizing the var LV/FS on a Red Hat 7.x system

Take inventory before going into single-user:

# df -h /var
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg00-var   13G  6.2G  5.9G  52% /var

# vgs
  VG         #PV #LV #SN Attr   VSize   VFree
  vg00         1   7   0 wz--n- <39.51g <2.63g

# lvs | grep var
  var      vg00       -wi-ao---- <12.86g

Boot into single user mode relevant to the platform you are using.

NOTE: The system will be accessible via /sysroot.

Activate vg00:

# lvm vgchange -ay vg00

Recreate LV device files:

# lvm vgmknodes -vv

# e2fsck -f /dev/vg00/var

To be safe, we’ll reduce the FS to 8GB and then the LV to 8.5GB (with a subsequent FS resize to fill it out):

# /sysroot/sbin/resize2fs -p /dev/vg00/var 8G

Some resize math:

VG PE size: PE Size 4.00 MiB

4M * 1024K = 4096K

8.5G * 1024K * 1024K = 8,912,896K

8,912,89 / 4096 = 2176 PEs

NOTE: I received the following error initially and had to manually override locking_type: "Read-only set locking type. Write locks are prohibited."

# grep locking_type /etc/lvm/lvm.conf
locking_type = 4

Reduce LV:

# lvm lvreduce -l 2176 /dev/vg00/var --config 'global {locking_type=1}'

RE-execute resize to fill the LV container out to 8.5GB:

# /sysroot/sbin/resize2fs /dev/vg00/var

Confirm new size with:

# lvm lvs

# reboot
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy