View previous topic :: View next topic |
Author |
Message |
cwng n00b
Joined: 21 Nov 2002 Posts: 68 Location: Singapore
|
Posted: Tue Jan 25, 2005 4:18 am Post subject: Using tmpfs for /var/{log,lock,...} |
|
|
EDIT: A new, simplified version
The following is a new simplified version. The original post is moved to subsequent post (3 or 4 post down).
-----
The thread in Using tmpfs for /tmp prompted me to experiment with using tmpfs for some /var directories that has frequent disk read/write, such as /var/log. I imagine doing so will have certain advantages:
- Speed: it speeds up the logging process
- I/O: it reduces disk I/O
The following are the scripts I have conjured, to see if anyone brave enough to try them. I have been using them for a while, no problems so far.
/etc/init.d/vartmp - This is the startup script:
Code: |
#!/sbin/runscript
#
# This init script mounts various /var sub-directories into RAM-based filesystem
# (i.e. tmpfs). There are two main advantages in doing so:
# - speed: its speeds up logging processes
# - disk-sync: it reduces the need for constant disk syncs
#
# and make sure the ${VARTMP} directory is created and/or mounted before
# executing this script.
depend() {
need localmount
before bootmisc
}
start() {
ebegin "Migrating /var to tmpfs "
#
# Check existence of ${VARTMP}
#
if [ -z ${VARTMP} ]; then
VARTMP=/dev/shm/vartmp
fi
if [ ! -d ${VARTMP} ]; then
mkdir ${VARTMP}
fi
for d in ${VARTMP_DIRS}; do
move_to_vartmp ${d}
done
eend $?
}
stop() {
ebegin "Syncing /var from tmpfs "
if [ -z ${VARTMP} ]; then
VARTMP=/dev/shm/vartmp
fi
for d in ${VARTMP_DIRS}; do
move_from_vartmp ${d}
done
eend $?
}
# This function transfer /var/* to ${VARTMP}
move_to_vartmp() {
ebegin "Moving /var/${1} to ${VARTMP}/${1} "
#
# Move /var/xxx contents to ${VARTMP}
#
if [ -e /var/${1} ]; then
mv /var/${1} ${VARTMP}
ln -sf ${VARTMP}/${1} /var/${1}
else
if [ ! -d ${VARTMP}/${1} ]; then
rm -f ${VARTMP}/${1}
mkdir ${VARTMP}/${1}
chmod 775 ${VARTMP}/${1}
fi
fi
eend $?
}
# This function transfer ${VARTMP} back to /var
move_from_vartmp() {
ebegin "Moving ${VARTMP}/${1} back to /var/${1} "
if [ -d ${VARTMP}/${1} ]; then
if [ -e /var/${1} ]; then
rm -f /var/${1}
fi
mv ${VARTMP}/${1} /var/
fi
eend $?
}
|
/etc/conf.d/vartmp - This is the configuration file:
Code: |
# /etc/conf.d/vartmp
#
# VARTMP defines where the tmpfs base directory for /var is
VARTMP="/dev/shm/vartmp"
# VARTMP_DIRS defines which of the /var sub-directories resides on the tmpfs
VARTMP_DIRS="log lock run"
|
Steps - Here is what I did:
First, edit /etc/conf.d/vartmp to your liking (particularly the VARTMP_DIRS="" variable that specify which of the subdirectories to move to tmpfs. You definitely don't want to move all of them, unless you have obscene amount of RAM, like 32G ) and run the init script:
Code: | rc-update add vartmp boot
/etc/init.d/vartmp start
|
For me, I will see this:
Code: | bach cwng # rc-update add vartmp boot
* vartmp added to runlevel boot
* Caching service dependencies...
* rc-update complete.
bach cwng # /etc/init.d/vartmp start
* Migrating /var to tmpfs ...
* Moving /var/log to /dev/shm/vartmp/log [ ok ]
* Moving /var/lock to /dev/shm/vartmp/lock [ ok ]
* Moving /var/run to /dev/shm/vartmp/run [ ok ]
bach cwng # ls -l /var
total 0
drwxr-xr-x 6 root root 74 Dec 21 00:21 cache
drwxr-xr-x 3 root root 16 Oct 28 14:36 db
drwxr-xr-x 2 root root 18 Jan 4 11:10 empty
drwxrwx--T 2 root gdm 18 Jan 10 22:45 gdm
drwxr-xr-x 13 root root 150 Dec 29 19:40 lib
lrwxrwxrwx 1 root root 15 Jan 25 16:48 lock -> /dev/shm/vartmp/lock
lrwxrwxrwx 1 root root 15 Jan 25 16:48 log -> /dev/shm/vartmp/log
lrwxrwxrwx 1 root root 15 Dec 20 19:06 mail -> /var/spool/mail
lrwxrwxrwx 1 root root 15 Jan 25 16:48 run -> /dev/shm/vartmp/run
drwxr-xr-x 8 root root 92 Jan 14 16:25 spool
drwxr-xr-x 2 root root 18 Oct 28 14:23 state
drwxrwxrwt 5 root root 93 Jan 24 15:05 tmp
|
Well, all seems fine. If for any reason you wish to revert back to the original setting, you just have to do:
Code: | /etc/init.d/vartmp stop
rc-update del vartmp
|
Some suggestions from freeix (see his post below for detail)
- Specify PORT_LOGDIR in /etc/make.conf to avoid having a huge emerge.log in RAM.
- You can move /var/tmp to tmpfs as well, but you may want to specify PORTAGE_TMPDIR in /etc/make.conf to avoid having the entire portage working directories on RAM.
WARNING: Doing so may cause loss of log files when system is hang, or looses power. As freeix suggested, you may wish to symlink /var/log/critical to somewhere else.
/rgds
/cwng
Last edited by cwng on Wed Jan 26, 2005 2:27 am; edited 5 times in total |
|
Back to top |
|
|
Gherald Veteran
Joined: 23 Aug 2004 Posts: 1399 Location: CLUAConsole
|
Posted: Tue Jan 25, 2005 5:05 am Post subject: |
|
|
You are my hero! err, that is to say, this is exactly the sort of thing I was planning to write myself
A few quick notes:
- I recommend using /dev/shm. May as well get some use out of it.
- Back up your /var/log before proceeding, and make sure it is of reasonable size!
- If you have set PORT_LOGDIR=/var/log/emerge in make.conf, you probably want to change this to /var/portage/log or somesuch (create the appropriate directories, of course). In my case /var/log/emerge was over 200MB, and you don't want that sitting in your ram!
PORT_LOGDIR is a very useful feature, btw. Well worth checking out when you need to see why an ebuild failed.
- It is probably worth including /var/tmp as well. Just not /var/tmp/portage. So change PORTAGE_TMPDIR in make.conf from "/var/tmp" to "/var/portage" (and create the portage directory, if you haven't already). |
|
Back to top |
|
|
cwng n00b
Joined: 21 Nov 2002 Posts: 68 Location: Singapore
|
Posted: Tue Jan 25, 2005 7:19 am Post subject: |
|
|
freeix wrote: | - I recommend using /dev/shm. May as well get some use out of it. |
True, unless you have programs running that do a lot of shared memory. That's why if you don't specify VARTMP_MOUNT, it is defaulted to /dev/shm/vartmp.
Your insights into PORT_LOGDIR and PORTAGE_TMPDIR are all very true. For directories that may grew very large and you don't want them to be in tmpfs, one method is to soft-link the directory to somewhere else, eg:
Code: | mv /var/log/samba /somewhere-else-on-disk/
ln -sf /somewhere-else-on-disk/samba /var/log/samba
|
There is one more caution I wish to make: if for any reason, your power is tripped or the whole system hangs, you will lose all the changes to your /var/xxx files since the system last booted. This shouldn't be an issue for /var/run and /var/locks. But you may loose some of the log files that may prove critical to providing information on why your system hanged. |
|
Back to top |
|
|
Gherald Veteran
Joined: 23 Aug 2004 Posts: 1399 Location: CLUAConsole
|
Posted: Tue Jan 25, 2005 7:51 am Post subject: |
|
|
The best solution is probably to use metalog and link /var/log/critical to disk.
For samba, you can set the maximum log size to something reasonable like 100k |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|