Cachefilesd

From Lolly's Wiki
Jump to navigationJump to search

Cachefilesd

Create ramdisk for cache if enough ram

A dir named /cache is created and ramdisk ist mounted there!

# systemctl --force --full edit  create-ramdisk@.service
[Unit]
Description=create cache dir in ramdisk
After=remote-fs.target
Before=cachefilesd.service
[Service]
Type=oneshot
RemainAfterExit=yes
TimeoutSec=0
ExecStartPre=/sbin/modprobe brd rd_nr=1 rd_size=%i
ExecStartPre=/sbin/sgdisk -Z --new 1:0:0 /dev/ram0
ExecStartPre=/sbin/mkfs.ext4 -m 0 /dev/ram0p1 
ExecStartPre=-/bin/mkdir /cache
ExecStart=/bin/mount -o user_xattr /dev/ram0p1 /cache
ExecStop=/bin/umount /cache
ExecStop=/sbin/rmmod brd
[Install]
WantedBy=multi-user.target

Create for example a 2gb disk with:

# systemctl start create-ramdisk@$[ 2 * 1024 * 1024 ].service

Destroy it again:

# systemctl stop create-ramdisk@$[ 2 * 1024 * 1024 ].service

Make a 4gb one instead:

# systemctl start create-ramdisk@$[ 4 * 1024 * 1024 ].service

If you found the right value, nail it for the next reboot with:

# systemctl enable create-ramdisk@$[ ${your_gigabyte_value} * 1024 * 1024 ].service

Check if kernel supports filesystem cache for your filesystem type

# grep  "CONFIG_.*_FSCACHE" /boot/config-`uname -r`
CONFIG_NFS_FSCACHE=y
CONFIG_CEPH_FSCACHE=y
CONFIG_CIFS_FSCACHE=y
CONFIG_AFS_FSCACHE=y
CONFIG_9P_FSCACHE=y

Setup /etc/cachefilesd.conf

###############################################################################
#
# Copyright (C) 2006,2010 Red Hat, Inc. All Rights Reserved.
# Written by David Howells (dhowells@redhat.com)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
###############################################################################

# obviously this should be a path to a ramdisk if you have enough ram
dir /var/cache/fscache
secctx cachefiles_kernel_t
tag mycache
debug 1
brun 10%
bcull 7%
bstop 3%
frun 10%
fcull 7%
fstop 3%

# Assuming you're using SELinux with the default security policy included in
# this package
# secctx system_u:system_r:cachefiles_kernel_t:s0

Problems with autofs mounted filesystems

In case of using automount with caching the cachefilesd must be running before autofs comes up an might mount the filesystem.

Make sure it is started by systemd

Disable the SYSV way of starting

# update-rc.d cachefilesd disable

Make cachefilesd started by systemd

# systemctl edit --force --full cachefilesd.service
[Unit]
Documentation=man:cachefilesd
Description=LSB: CacheFiles daemon
After=remote-fs.target
Before=autofs.service

[Service]
Type=simple
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
SuccessExitStatus=5 6
RuntimeDirectory=cachefilesd
ExecStartPre=-/sbin/modprobe -qab cachefiles
ExecStart=/sbin/cachefilesd -n -p /run/cachefilesd/cachefilesd.pid

[Install]
WantedBy=multi-user.target

Enable starting new service

# systemctl enable cachefilesd.service

Verify autofs is depending on cachefilesd.service

# systemctl show -p After,Before autofs.service | grep cachefilesd.service
After=cachefilesd.service network.target network-online.target sysinit.target ypbind.service basic.target system.slice sssd.service systemd-journald.socket remote-fs.target

Define a cached CIFS share with autofs

Install needed packages

# apt install cachefilesd autofs cifs-utils

Create the credentials file

# mkdir --mode=0700 /etc/cifs_cred
# touch /etc/cifs_cred/credentials
# chmod 0600 /etc/cifs_cred/credentials
# cat > /etc/cifs_cred/credentials <<EOF
username=myuser
password=mypass
EOF

Create basedir of your cifs mounts

# mkdir --mode=0755 /data/cifs

/etc/auto.master

...
/data/cifs /etc/auto.cifs-shares --timeout=0 --ghost
...

/etc/auto.cifs-shares

The option Template:Strong enables caching:

myshare -fstype=cifs,credentials=/etc/cifs_cred/credentials,nounix,file_mode=0644,vers=3.0,dir_mode=0755,noperm,fsc ://cifsserver.my.dom/the_cifs_share

Check if things are getting cached

Initially there is nothing in the cache (almost all values are zero):

# cat /proc/fs/fscache/stats
FS-Cache statistics
Cookies: idx=2 dat=0 spc=0
Objects: alc=0 nal=0 avl=0 ded=0
ChkAux : non=0 ok=0 upd=0 obs=0
Pages  : mrk=0 unc=0
Acquire: n=2 nul=0 noc=0 ok=2 nbf=0 oom=0
Lookups: n=0 neg=0 pos=0 crt=0 tmo=0
Invals : n=0 run=0
Updates: n=0 nul=0 run=0
Relinqs: n=0 nul=0 wcr=0 rtr=0
AttrChg: n=0 ok=0 nbf=0 oom=0 run=0
Allocs : n=0 ok=0 wt=0 nbf=0 int=0
Allocs : ops=0 owt=0 abt=0
Retrvls: n=0 ok=0 wt=0 nod=0 nbf=0 int=0 oom=0
Retrvls: ops=0 owt=0 abt=0
Stores : n=0 ok=0 agn=0 nbf=0 oom=0
Stores : ops=0 run=0 pgs=0 rxd=0 olm=0
VmScan : nos=0 gon=0 bsy=0 can=0 wt=0
Ops    : pend=0 run=0 enq=0 can=0 rej=0
Ops    : ini=0 dfr=0 rel=0 gc=0
CacheOp: alo=0 luo=0 luc=0 gro=0
CacheOp: inv=0 upo=0 dro=0 pto=0 atc=0 syn=0
CacheOp: rap=0 ras=0 alp=0 als=0 wrp=0 ucp=0 dsp=0
CacheEv: nsp=0 stl=0 rtr=0 cul=0

but after a few requests:

# cat /proc/fs/fscache/stats
FS-Cache statistics
Cookies: idx=3 dat=77 spc=0
Objects: alc=80 nal=0 avl=80 ded=70
ChkAux : non=0 ok=1 upd=0 obs=1
Pages  : mrk=3138215 unc=181438
Acquire: n=150 nul=0 noc=0 ok=80 nbf=0 oom=0
Lookups: n=80 neg=78 pos=2 crt=78 tmo=0
Invals : n=0 run=0
Updates: n=0 nul=0 run=0
Relinqs: n=70 nul=0 wcr=0 rtr=70
AttrChg: n=0 ok=0 nbf=0 oom=0 run=0
Allocs : n=0 ok=0 wt=0 nbf=0 int=0
Allocs : ops=0 owt=0 abt=0
Retrvls: n=72447 ok=0 wt=6 nod=72447 nbf=0 int=0 oom=0
Retrvls: ops=72447 owt=15 abt=0
Stores : n=3136954 ok=3136954 agn=0 nbf=0 oom=0
Stores : ops=67042 run=3203996 pgs=3136954 rxd=3136954 olm=0
VmScan : nos=180177 gon=0 bsy=0 can=0 wt=0
Ops    : pend=15 run=139489 enq=3203996 can=0 rej=0
Ops    : ini=3209401 dfr=266 rel=3209401 gc=266
CacheOp: alo=0 luo=0 luc=0 gro=0
CacheOp: inv=0 upo=0 dro=0 pto=0 atc=0 syn=0
CacheOp: rap=0 ras=0 alp=0 als=0 wrp=0 ucp=0 dsp=0
CacheEv: nsp=1 stl=0 rtr=0 cul=0