Cachefilesd: Difference between revisions

From Lolly's Wiki
Jump to navigationJump to search
m (Text replacement - "<source " to "<syntaxhighlight ")
Line 4: Line 4:
A dir named /cache is created and ramdisk ist mounted there!
A dir named /cache is created and ramdisk ist mounted there!


<source lang=bash>
<syntaxhighlight lang=bash>
# systemctl --force --full edit  create-ramdisk@.service
# systemctl --force --full edit  create-ramdisk@.service
</source>
</source>
<source lang=ini>
<syntaxhighlight lang=ini>
[Unit]
[Unit]
Description=create cache dir in ramdisk
Description=create cache dir in ramdisk
Line 27: Line 27:
</source>
</source>
Create for example a 2gb disk with:
Create for example a 2gb disk with:
<source lang=bash>
<syntaxhighlight lang=bash>
# systemctl start create-ramdisk@$[ 2 * 1024 * 1024 ].service
# systemctl start create-ramdisk@$[ 2 * 1024 * 1024 ].service
</source>
</source>
Destroy it again:
Destroy it again:
<source lang=bash>
<syntaxhighlight lang=bash>
# systemctl stop create-ramdisk@$[ 2 * 1024 * 1024 ].service
# systemctl stop create-ramdisk@$[ 2 * 1024 * 1024 ].service
</source>
</source>


Make a 4gb one instead:
Make a 4gb one instead:
<source lang=bash>
<syntaxhighlight lang=bash>
# systemctl start create-ramdisk@$[ 4 * 1024 * 1024 ].service
# systemctl start create-ramdisk@$[ 4 * 1024 * 1024 ].service
</source>
</source>


If you found the right value, nail it for the next reboot with:
If you found the right value, nail it for the next reboot with:
<source lang=bash>
<syntaxhighlight lang=bash>
# systemctl enable create-ramdisk@$[ ${your_gigabyte_value} * 1024 * 1024 ].service
# systemctl enable create-ramdisk@$[ ${your_gigabyte_value} * 1024 * 1024 ].service
</source>
</source>


==Check if kernel supports filesystem cache for your filesystem type==
==Check if kernel supports filesystem cache for your filesystem type==
<source lang=bash>
<syntaxhighlight lang=bash>
# grep  "CONFIG_.*_FSCACHE" /boot/config-`uname -r`
# grep  "CONFIG_.*_FSCACHE" /boot/config-`uname -r`
CONFIG_NFS_FSCACHE=y
CONFIG_NFS_FSCACHE=y
Line 90: Line 90:
=== Make sure it is started by systemd ===
=== Make sure it is started by systemd ===
==== Disable the SYSV way of starting ====
==== Disable the SYSV way of starting ====
<source lang=bash>
<syntaxhighlight lang=bash>
# update-rc.d cachefilesd disable
# update-rc.d cachefilesd disable
</source>
</source>
==== Make cachefilesd started by systemd ====
==== Make cachefilesd started by systemd ====
<source lang=bash>
<syntaxhighlight lang=bash>
# systemctl edit --force --full cachefilesd.service
# systemctl edit --force --full cachefilesd.service
</source>
</source>
<source lang=ini>
<syntaxhighlight lang=ini>
[Unit]
[Unit]
Documentation=man:cachefilesd
Documentation=man:cachefilesd
Line 121: Line 121:
</source>
</source>
==== Enable starting new service ====
==== Enable starting new service ====
<source lang=bash>
<syntaxhighlight lang=bash>
# systemctl enable cachefilesd.service
# systemctl enable cachefilesd.service
</source>
</source>
==== Verify autofs is depending on cachefilesd.service ====
==== Verify autofs is depending on cachefilesd.service ====
<source lang=bash>
<syntaxhighlight lang=bash>
# systemctl show -p After,Before autofs.service | grep 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
After=cachefilesd.service network.target network-online.target sysinit.target ypbind.service basic.target system.slice sssd.service systemd-journald.socket remote-fs.target
Line 132: Line 132:
== Define a cached CIFS share with autofs ==
== Define a cached CIFS share with autofs ==
=== Install needed packages ===
=== Install needed packages ===
<source lang=bash>
<syntaxhighlight lang=bash>
# apt install cachefilesd autofs cifs-utils
# apt install cachefilesd autofs cifs-utils
</source>
</source>


=== Create the credentials file ===
=== Create the credentials file ===
<source lang=bash>
<syntaxhighlight lang=bash>
# mkdir --mode=0700 /etc/cifs_cred
# mkdir --mode=0700 /etc/cifs_cred
# touch /etc/cifs_cred/credentials
# touch /etc/cifs_cred/credentials
Line 148: Line 148:


=== Create basedir of your cifs mounts ===
=== Create basedir of your cifs mounts ===
<source lang=bash>
<syntaxhighlight lang=bash>
# mkdir --mode=0755 /data/cifs
# mkdir --mode=0755 /data/cifs
</source>
</source>
Line 168: Line 168:
== Check if things are getting cached ==
== Check if things are getting cached ==
Initially there is nothing in the cache (almost all values are zero):
Initially there is nothing in the cache (almost all values are zero):
<source lang=bash>
<syntaxhighlight lang=bash>
# cat /proc/fs/fscache/stats
# cat /proc/fs/fscache/stats
FS-Cache statistics
FS-Cache statistics
Line 196: Line 196:
</source>
</source>
but after a few requests:
but after a few requests:
<source lang=bash>
<syntaxhighlight lang=bash>
# cat /proc/fs/fscache/stats
# cat /proc/fs/fscache/stats
FS-Cache statistics
FS-Cache statistics

Revision as of 17:52, 25 November 2021

Cachefilesd

Create ramdisk for cache if enough ram

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

<syntaxhighlight lang=bash>

  1. systemctl --force --full edit create-ramdisk@.service

</source> <syntaxhighlight lang=ini> [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 </source> Create for example a 2gb disk with: <syntaxhighlight lang=bash>

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

</source> Destroy it again: <syntaxhighlight lang=bash>

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

</source>

Make a 4gb one instead: <syntaxhighlight lang=bash>

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

</source>

If you found the right value, nail it for the next reboot with: <syntaxhighlight lang=bash>

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

</source>

Check if kernel supports filesystem cache for your filesystem type

<syntaxhighlight lang=bash>

  1. 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 </source>

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

<syntaxhighlight lang=bash>

  1. update-rc.d cachefilesd disable

</source>

Make cachefilesd started by systemd

<syntaxhighlight lang=bash>

  1. systemctl edit --force --full cachefilesd.service

</source> <syntaxhighlight lang=ini> [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 </source>

Enable starting new service

<syntaxhighlight lang=bash>

  1. systemctl enable cachefilesd.service

</source>

Verify autofs is depending on cachefilesd.service

<syntaxhighlight lang=bash>

  1. 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 </source>

Define a cached CIFS share with autofs

Install needed packages

<syntaxhighlight lang=bash>

  1. apt install cachefilesd autofs cifs-utils

</source>

Create the credentials file

<syntaxhighlight lang=bash>

  1. mkdir --mode=0700 /etc/cifs_cred
  2. touch /etc/cifs_cred/credentials
  3. chmod 0600 /etc/cifs_cred/credentials
  4. cat > /etc/cifs_cred/credentials <<EOF

username=myuser password=mypass EOF </source>

Create basedir of your cifs mounts

<syntaxhighlight lang=bash>

  1. mkdir --mode=0755 /data/cifs

</source>

/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): <syntaxhighlight lang=bash>

  1. 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 </source> but after a few requests: <syntaxhighlight lang=bash>

  1. 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 </source>