OpenWrt Forum Archive

Topic: Persistent sotrage for /tmp and /var

The content of this topic has been archived on 7 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hello,

I'm running a x86 Openwrt off a hard disk (or USB key) and would like to have /var,/tmp to be persistent on hd instead of RAM. I'm especially interested in keeping the stats from vnstat

Is there a way to do it?

Thank you.

Pitou!

(Last edited by Pitou on 7 Apr 2010, 03:18)

ok I just use another linux PC, to remove the symlink /var and create a true /var directory on the usb key.

Pitou!

Pitou wrote:

ok I just use another linux PC, to remove the symlink /var and create a true /var directory on the usb key.

Can you confirm if the new and true /var directory will be there after a reboot?

yes

I just tried achieving this by pointing the /var symlink to a var folder located on an external hard drive (which auto-mounts at boot).

var -> /mnt/sda1/var

This bricked my router. It seems that boot and other init scripts make use of /var before fstab is executed.
To change this behavior, I tried modifying the order of the init scripts, making fstab execute immediately after boot. In rc.d/

...
S00sysfixtime
S05boot    #needs /var
S06fstab
S10system
S11sysctl
S12log    #needs /var
...

(I also tried moving fstab right after system and right after sysctl).

In the boot script, I commented out all commands involving /var:

...
#mkdir -p /var/run
#mkdir -p /var/log
#mkdir -p /var/lock
#mkdir -p /var/state
#mkdir -p /var/tmp
mkdir -p /tmp/.uci
chmod 0700 /tmp/.uci
#touch /var/log/wtmp
#touch /var/log/lastlog
touch /tmp/resolv.conf.auto
...

and I copied them (uncommented) to the fstab script, after the /sbin/block mount command.

The router won't boot with this configuration. What am I missing? Are there scripts that must be executed before fstab? Are there other/better ways to use a persistent external /var folder?

Why do you need /var on a persistent storage medium?

tmo26 wrote:

Why do you need /var on a persistent storage medium?

1) I saw the very existence of a /var symlink as an invitation to do so by the developer who placed it: otherwise why not just dump everything to /tmp directly?
2) I want to start keeping persistent logs on file. Even if I saved logs to a different external directory, I believe I'd still need to move the fstab script before the log script in order to mount that directory's partition before logging begins. Why not make the whole /var external and store logs where they are supposed to?
3) Where it makes sense, I like to keep my system as similar as possible to linux distributions I'm familiar to. Hence, for example, /home and /srv symlinks. My squid implementation already makes use of an external var directory, with the usual lib and cache subdirectories. I'd like the system to use this directory as var and keep things tidy. 
4) In general, it makes a lot of sense to me (at least on an embedded device), that the system should mount as soon as it can whatever is set to be auto-mounted, treating external partitions as extensions of the root: it should make the assumption that files on those partitions may be required, possibly early on, during the init sequence. It is not the first time I've had problems because fstab would run too late, and one always has to resort to unpleasant hacks to deal with this. I'm no init expert, maybe there are reasons why this behavior is undesirable, but if so I'd like to understand them.   
5) Because it's a new problem and I'll have a very bad itch until I solve it smile

I guess a solution would be to write something like this in rc.local

cp -r /var/* /mnt/sda1/var/
rm /var
ln -s /mnt/sda1/var /var

but god it's an ugly one.

UPDATE
Definitely some problems with this approach too. Router boots fine but DHCP doesn't work and there's no internet connection.

(Last edited by endvour on 25 Feb 2017, 12:51)

SOLVED
This assumes /etc/config/fstab has been configured so that the partition containing the var directory will auto-mount on /mnt/sda1. Make sure you are familiar with failsafe mode before trying this, as errors may result in your router not being able to boot.
1) In /etc/init.d/boot, before the first line mentioning var, add

/sbin/block mount

2) Disable execution of fstab at boot

/etc/init.d/fstab disable

3) Give full permissions to the new var directory

chmod 777 /mnt/sda1/var

4) Replace old /var symlink with new one and reboot

ln -sf /mnt/sda1/var /var && reboot

(Last edited by endvour on 26 Feb 2017, 23:57)

The discussion might have continued from here.