You are not logged in.

#1 2012-01-18 21:42:46

shaurz
Member
Registered: 2004-02-02
Posts: 358

SSD saver - /var/log on tmpfs with sync back to SSD

I recently built a system with an SSD. The usual recommendation is to mount /var on a hard disk partition, but instead I decided to make a little script that puts /var/log on tmpfs (assuming /tmp is mounted as tmpfs), and then copies the logs back to the SSD on shutdown. Of course there is the risk of losing the logs in a power failure, but I don't really care about them that much.

Copy to: /etc/rc.d/functions.d/mount_log

mount_log() {
    mkdir -p /tmp/log
    chmod 755 /tmp/log
    mv -f /var/log/* /tmp/log/
    rm -rf /var/log
    ln -s /tmp/log /var/log
}

umount_log() {
    if [ "`readlink /var/log`" == "/tmp/log" ]; then
        rm -f /var/log
        mv -f /tmp/log /var/
    fi
}

add_hook sysinit_end mount_log
add_hook shutdown_preumount umount_log

I guess this could be extended for other directories that might get written to a lot - any ideas?

Offline

#2 2012-01-22 05:12:54

shaurz
Member
Registered: 2004-02-02
Posts: 358

Re: SSD saver - /var/log on tmpfs with sync back to SSD

I made a lot of improvements and generalised the code. It now mounts a tmpfs instead of making symlinks in to /tmp. I came up with the name "nvtmpfs" meaning non-volatile tmpfs.

/etc/rc.d/functions.d/nvtmpfs

#!/bin/bash

NVTMPFS_DIR="/tmp/.nvtmpfs"

nvtmpfs_mount() {
    if [ -e "$1" ]; then
        if [ -z "`mount -t tmpfs | grep $1`" ]; then
            mkdir -p "$NVTMPFS_DIR"
            cp -r "$1"/* "$NVTMPFS_DIR/"
            mount -t tmpfs -o noatime,mode=1755 tmpfs "$1"
            mv -f "$NVTMPFS_DIR"/* "$1"
            rm -rf "$NVTMPFS_DIR"
        else
            echo "nvtmpfs: $1 is already mounted"
        fi
    else
        echo "nvtmpfs: $1 does not exist"
        return 1
    fi
}

nvtmpfs_umount() {
    if [ -n "`mount -t tmpfs | grep $1`" ]; then
        mkdir -p "$NVTMPFS_DIR"
        mv -f "$1"/* "$NVTMPFS_DIR/"
        umount "$1"
        mv -f "$NVTMPFS_DIR"/* "$1"
        rm -rf "$NVTMPFS_DIR"
    else
        echo "nvtmpfs: $1 is not mounted"
        return 1
    fi
}

nvtmpfs_startup() {
    nvtmpfs_mount /var/log
    nvtmpfs_mount /var/tmp
}

nvtmpfs_shutdown() {
    nvtmpfs_umount /var/log
    nvtmpfs_umount /var/tmp
}

add_hook sysinit_end nvtmpfs_startup
add_hook shutdown_preumount nvtmpfs_shutdown

Last edited by shaurz (2012-01-22 05:18:53)

Offline

#3 2012-01-29 00:44:02

shaurz
Member
Registered: 2004-02-02
Posts: 358

Re: SSD saver - /var/log on tmpfs with sync back to SSD

New version using rsync

#!/bin/bash

NVTMPFS_DIR="/tmp/.nvtmpfs"

nvtmpfs_mount() {
    if [ -e "$1" ]; then
        if [ -z "`mount -t tmpfs | grep $1`" ]; then
            mkdir -p "$NVTMPFS_DIR"
            rsync -ur --delete "$1/" "$NVTMPFS_DIR/"
            mount -t tmpfs -o noatime,mode=1755 tmpfs "$1"
            rsync -ur --delete "$NVTMPFS_DIR/" "$1/"
            rm -rf "$NVTMPFS_DIR"
        else
            echo "nvtmpfs: $1 is already mounted"
        fi
    else
        echo "nvtmpfs: $1 does not exist"
        return 1
    fi
}

nvtmpfs_umount() {
    if [ -n "`mount -t tmpfs | grep $1`" ]; then
        mkdir -p "$NVTMPFS_DIR"
        rsync -ur --delete "$1/" "$NVTMPFS_DIR/"
        umount "$1"
        rsync -ur --delete "$NVTMPFS_DIR/" "$1/"
        rm -rf "$NVTMPFS_DIR"
    else
        echo "nvtmpfs: $1 not mounted"
        return 1
    fi
}

nvtmpfs_startup() {
    nvtmpfs_mount /var/log
    nvtmpfs_mount /var/tmp
}

nvtmpfs_shutdown() {
    nvtmpfs_umount /var/log
    nvtmpfs_umount /var/tmp
}

add_hook sysinit_end nvtmpfs_startup
add_hook shutdown_preumount nvtmpfs_shutdown

Offline

#4 2012-01-29 22:57:03

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: SSD saver - /var/log on tmpfs with sync back to SSD

why don't you just make /var/log an on-disk filesystem like ext4 with a large commit interval setting?


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#5 2012-01-29 23:52:19

shaurz
Member
Registered: 2004-02-02
Posts: 358

Re: SSD saver - /var/log on tmpfs with sync back to SSD

Because I didn't know you could do that...

Offline

#6 2012-02-09 20:10:03

teamchris
Member
Registered: 2012-02-09
Posts: 2

Re: SSD saver - /var/log on tmpfs with sync back to SSD

Dieter@be wrote:

why don't you just make /var/log an on-disk filesystem like ext4 with a large commit interval setting?

Although this would reduce writes against the SSD, wouldn't it be missing the increased read performance of a tmpfs?
(I'm genuinely curious as I'm considering doing something like this myself. Thanks everyone so far for the info.)

PS- profile-sync-daemon may also be of interest to some.

Offline

#7 2012-02-09 23:06:48

shaurz
Member
Registered: 2004-02-02
Posts: 358

Re: SSD saver - /var/log on tmpfs with sync back to SSD

There probably isn't any real difference. This scripts needs to read the whole directory from disk on startup anyway.

Offline

#8 2012-05-10 02:05:17

jsumners
Member
From: Fairburn, GA
Registered: 2006-09-05
Posts: 19
Website

Re: SSD saver - /var/log on tmpfs with sync back to SSD

I came up with the same idea, but with a slight difference. I created a loopback image to store the data between boots. Thus, I have a dedicated section of my SSD for storing my /var/log that doesn't get written to very often. I wrote up my solution at http://jrfom.com/2012/05/09/prolonging- … ng-writes/

Offline

#9 2012-05-10 06:24:22

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: SSD saver - /var/log on tmpfs with sync back to SSD


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#10 2012-05-10 12:47:22

jsumners
Member
From: Fairburn, GA
Registered: 2006-09-05
Posts: 19
Website

Re: SSD saver - /var/log on tmpfs with sync back to SSD

Very nice. I was unaware of anyone else's work in this regard when I started. I like your daemon, but it doesn't quite fit my personal requirements. But it's good to know it's there if I need it for another situation.

Offline

Board footer

Powered by FluxBB