Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Using tmpfs to reduce hard drive usage for laptops
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
ka0ttic
Retired Dev
Retired Dev


Joined: 23 Oct 2003
Posts: 46
Location: Ormond Beach, FL

PostPosted: Wed Feb 04, 2004 5:52 pm    Post subject: Using tmpfs to reduce hard drive usage for laptops Reply with quote

While at work one night, I was trying to think of ways to reduce hard drive usage on my laptop (thus more battery life). I was already using tmpfs to mount /tmp, so I figured I'd try to mount something that gets a little more disk usage.

/var/log seemed like a good candidate, so first I stopped my logger (I use metalog), backed up /var/log, then deleted everything. To test, I did:

Code:

# mount -t tmpfs tmpfs /var/log -o size=20M
# cp -Rp /var/log.backup/* /var/log
# /etc/init.d/metalog start


Everything works great. Tune the size argument to your liking. Even though this is essentially a RAM drive, the size argument isn't neccessarily how much memory it will take. It specifies the maximum memory that it can take up. Basically, I have 20 megs of space for logs until metalog starts complaining. Since I don't really log that much anyways, except for debugging stuff (it is a laptop...), I hardly ever use more than a few megs.

One of the greatest things I like about this is being able to disable metalog buffering w/o it actually constantly writing to disk (unless you run out of memory and it starts swapping).

so to do this permanently, add the following to /etc/fstab:

Code:

tmpfs               /var/log          tmpfs            size=20M          0   0


It would also be nice to keep logs across reboots, so edit the /etc/init.d/ file for your system logger. You will need to edit the start() and stop() functions, so that upon starting the logger, the saved logs are copied to /var/log, and upon stopping, they are saved to /var/log.save (or whatever you choose).

Code:

start() {
        ebegin "Starting metalog"

        echo "Restoring log files..."
        cp -Rp /var/log.save/* /var/log

        start-stop-daemon --start --quiet --exec /usr/sbin/metalog \
                -- -p /var/run/metalog.pid --daemonize ${METALOG_OPTS}
        eend $? "Failed to start metalog"
}
 
stop() {
        ebegin "Stopping metalog"
        start-stop-daemon --stop --quiet --pidfile /var/run/metalog.pid

        echo "Saving log files..."
        rm -fr /var/log.save/*
        cp -Rp /var/log/* /var/log.save

        eend $? "Failed to stop metalog"
}



Don't forget to make the saved logs directory.

There might be a better way to go about doing this, and if so please respond.

Fore more information, read /usr/src/linux/Documentation/filesystems/tmpfs.txt provided with your kernel source (assuming you have a kernel that supports tmpfs obviously.. if not, shame on you).

Please let me know if anyone finds this helpful in anyway.
Back to top
View user's profile Send private message
ckdake
l33t
l33t


Joined: 10 Apr 2003
Posts: 889
Location: Atlanta, GA

PostPosted: Wed Feb 04, 2004 7:23 pm    Post subject: Reply with quote

Thats a great idea and sounds like a pretty good way to do what you want to do. I may have to try that out if i can get xfree compiled on my powerbook
_________________
http://ckdake.com/
Back to top
View user's profile Send private message
nepenthe
Guru
Guru


Joined: 14 Jul 2002
Posts: 317
Location: Sanford, ME

PostPosted: Mon Feb 09, 2004 7:42 am    Post subject: Reply with quote

hmm I was about to try this... however my /var/log is 700mb and my /tmp is just as big... do you know if I can safely remove these files...

@ka0ttic
What size is the ram drive you made for your /tmp folder
Back to top
View user's profile Send private message
ka0ttic
Retired Dev
Retired Dev


Joined: 23 Oct 2003
Posts: 46
Location: Ormond Beach, FL

PostPosted: Mon Feb 09, 2004 10:58 am    Post subject: Reply with quote

nepenthe,

Either your box logs A LOT, or you just have A LOT of really old log files.... If it is the former, than obviously it wouldn't be the best idea to use tmpfs (unless you have a couple gigs of ram, of course). If the latter, then its time for spring cleaning!

Depending on your logger (or if you use logrotate or something similar), you may have lots of old rotated logs. sysklogd doesn't rotate logs itself I don't believe, but Metalog can be configured to rotate on size or age and how many old copies you want to keep.

If you don't have rotated logs to delete, and they are all just huge files, then you can just stop your logger and get rid of them. Whether or not the files get recreated automatically or not is logger-dependent (Metalog does.). So, I would be on the safe side and just truncate the file (cat /dev/null > filename).

I would say its time to clean /tmp as well. I thought Gentoo automatically cleaned /tmp on startup (it says it does during boot anyways...). You can safely delete anything in /tmp, however, some currently running program might be using something there... Either stop all services and programs and "rm -fr /tmp/*" or boot into single user mode and do it. If you were using tmpfs for /tmp, everything in /tmp would evaporate upon shutdown (I knew I should have called this the "self-cleaning /tmp HOWTO").

To answer your last question, on my laptop, I use 20M for /var/log (am only actually using ~2MB right now) and 10M for /tmp (only using about 16k). It really depends on 1) how much memory you have, 2) what the system is being used for (Desktop machines use /tmp A LOT more than server machines do).

Yikes, this turned out to be lengthy :wink:

HTH,
Aaron
Back to top
View user's profile Send private message
ka0ttic
Retired Dev
Retired Dev


Joined: 23 Oct 2003
Posts: 46
Location: Ormond Beach, FL

PostPosted: Mon Feb 09, 2004 11:16 am    Post subject: Reply with quote

BTW, I don't know why I didn't do this right the first time, but here is a modified /etc/init.d/ file for the logger. I changed the order of start (logically, the restored files should be there before the logger starts, not after). It also looks much nicer, and less like a quick hack by using ebegin and eend:

Code:


start() {
        ebegin "Restoring log files"
        cp -pR /var/log.save/* /var/log
        eend $? "Failed to restore log files"
 
        ebegin "Starting metalog"
        start-stop-daemon --start --quiet --exec /usr/sbin/metalog \
                -- -p /var/run/metalog.pid --daemonize ${METALOG_OPTS}
        eend $? "Failed to start metalog"
}
 
stop() {
        ebegin "Stopping metalog"
        start-stop-daemon --stop --quiet --pidfile /var/run/metalog.pid
        eend $? "Failed to stop metalog"
 
        ebegin "Saving log files"
        rm -fr /var/log.save/*
        cp -pR /var/log/* /var/log.save
        eend $? "Failed to save log files"
}



Aaron
Back to top
View user's profile Send private message
nepenthe
Guru
Guru


Joined: 14 Jul 2002
Posts: 317
Location: Sanford, ME

PostPosted: Mon Feb 09, 2004 8:21 pm    Post subject: Reply with quote

I think I shall try this.. but I wonder first why my tmp and log files are so big... looking closer into /var/log I see that the file messages and acpid are the biggest being 700.5MB and 53.8MB respectively... I wonder if it has to do with the different loggers... perhaps I'll try metalog... but you do agree it is save to just remove all these files? Ahh well I'll just back it up and give it a try =)
Thanks will update on how it goes.

nep
Back to top
View user's profile Send private message
hanzotutu
Apprentice
Apprentice


Joined: 10 Apr 2003
Posts: 170

PostPosted: Tue Feb 10, 2004 12:38 am    Post subject: Reply with quote

can you post the lines in your /etc/fstab that mount tmpfs for /tmp and /var/log? I guess I need to set the initial permissions for /tmp
Back to top
View user's profile Send private message
ka0ttic
Retired Dev
Retired Dev


Joined: 23 Oct 2003
Posts: 46
Location: Ormond Beach, FL

PostPosted: Tue Feb 10, 2004 12:54 am    Post subject: Reply with quote

If you are talking about what I am thinking you are talking about, and just created a new /tmp, then you need to "chmod 1777 /tmp" to give /tmp the proper permissions.

But here's the relevant lines for /etc/fstab if that wasnt what you were talking about:

Code:

tmpfs            /var/log            tmpfs            size=20M            0  0
tmpfs            /tmp                tmpfs            size=10M            0  0


Aaron
Back to top
View user's profile Send private message
hanzotutu
Apprentice
Apprentice


Joined: 10 Apr 2003
Posts: 170

PostPosted: Tue Feb 10, 2004 1:24 am    Post subject: Reply with quote

Thank you. I just wanna make sure everybody can write to /tmp
Back to top
View user's profile Send private message
markandrew
Apprentice
Apprentice


Joined: 04 Feb 2004
Posts: 176
Location: Manchester, UK

PostPosted: Tue Mar 30, 2004 9:33 am    Post subject: Reply with quote

i tried this a while back but came across a big drawback of mounting /var/log with tmpfs

the main reason i use my logfiles is to try and figure out what happened when something goes wrong. but using this method, if your machine crashes or locks up completely and you can't do a clean shutdown, you lose all your logs for that session, so you can't see what went wrong. that's a big enough drawback for me to not use it anymore
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
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