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):
- stop anything that is using /var where possible
- remount /var readonly for good measure mount -f -oremount,ro /var (didn't seem to have any effect for me)
- copy /var to / under a different name with mkdir /vartmp; cp -av /var/* /vartmp/
- comment out the entry for /var in /etc/fstab
- reboot
- move things around with mv /var /varmount; mv /vartmp /var
- reboot, partition
- uncomment the entry for /var in /etc/fstap
- reboot
Best Answer
If you have room on your root partition for the contents of /var you could:
/var
where possiblemount -f -oremount,ro /var
/var
to/
under a different name withmkdir /vartmp; cp -av /var/* /vartmp/
mv /var /varmount; mv /vartmp /var
/var
in/etc/fstab
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:/var
where possiblemount -f -oremount,ro /var
/var
withcp -av /var /someotherfs/var
mv /var /varmount; ln -s /someotherfs/var /var
/var
in/etc/fstab
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 equivalentyum
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!