Skip to content

Commit c9ad22d

Browse files
committed
separate user and system services
- simpler script - added user service - fix crontab (every hour instead of every minutes...)
1 parent f317ff2 commit c9ad22d

5 files changed

Lines changed: 155 additions & 66 deletions

File tree

README.md

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,23 @@ Systemd mount this folder in RAM by default. If you are lucky, you don't have an
5858
Sys. de fichiers Type Taille Utilisé Dispo Uti% Monté sur
5959
udev devtmpfs 16G 0 16G 0% /dev
6060
tmpfs tmpfs 3,2G 9,6M 3,2G 1% /run
61-
/dev/nvme0n1p6 ext4 183G 5,2G 168G 3% /
61+
/dev/nvme0n1p6 ext4 183G 5,4G 168G 4% /
6262
tmpfs tmpfs 16G 0 16G 0% /dev/shm
6363
tmpfs tmpfs 5,0M 4,0K 5,0M 1% /run/lock
6464
tmpfs tmpfs 16G 0 16G 0% /sys/fs/cgroup
65-
tmpfs tmpfs 16G 109M 16G 1% /var/cache
66-
tmpfs tmpfs 16G 120K 16G 1% /tmp
67-
tmpfs tmpfs 16G 35M 16G 1% /var/log
65+
tmpfs tmpfs 16G 44K 16G 1% /tmp
66+
tmpfs tmpfs 16G 127M 16G 1% /var/cache
67+
tmpfs tmpfs 16G 38M 16G 1% /var/log
6868
/dev/nvme0n1p5 ext4 233M 24M 193M 11% /boot
6969
/dev/nvme0n1p2 vfat 95M 25M 71M 27% /boot/efi
7070
/dev/nvme0n1p7 ext4 262G 47G 202G 19% /home
71-
tmpfs tmpfs 16G 406M 16G 3% /home/xinouch/.cache
72-
/dev/nvme0n1p4 fuseblk 500G 42G 458G 9% /mnt/w10
71+
tmpfs tmpfs 16G 58M 16G 1% /home/xinouch/.mozilla
72+
tmpfs tmpfs 16G 434M 16G 3% /home/xinouch/.cache
73+
/dev/nvme0n1p4 fuseblk 500G 93G 407G 19% /mnt/w10
7374
/dev/sda2 fuseblk 924G 287G 637G 32% /mnt/data
74-
/dev/sda3 ext4 7,9G 584M 6,9G 8% /mnt/persistent
75+
/dev/sda3 ext4 7,9G 691M 6,8G 10% /mnt/persistent
7576
tmpfs tmpfs 3,2G 0 3,2G 0% /run/user/114
76-
tmpfs tmpfs 3,2G 16K 3,2G 1% /run/user/1000
77+
tmpfs tmpfs 3,2G 12K 3,2G 1% /run/user/1000
7778
```
7879

7980
All the lines with a `tmpfs` filesystem are mounted on the RAM. So, if you have the line
@@ -102,7 +103,9 @@ Reboot and voilà!
102103
Cache in ramdisk
103104
================
104105

105-
You have 2 cache folders: the system one in `/var/cache` and the user one in `/home/$USER/.cache`. We will also take care of the logs in `/var/log`. If you don't want it, check the last subsection of this section.
106+
You have 2 cache folders: the system one in `/var/cache` and the user one in `/home/$USER/.cache`. We will also take care of the logs in `/var/log` along with the firefox session folder. If you don't want it, check the last subsection of this section.
107+
108+
We need to distinguish 2 types of folders here: the system folders and the user folders. The user folders will be mounted later in the boot, while the system folders will be mounted as soon as possible. For me, it takes 1 minute to load user data.
106109

107110
Change files for your config
108111
----------------------------
@@ -132,23 +135,29 @@ now you can simply
132135
$ mv home-xinouch-.cache.mount home-{USER}-.cache.mount
133136
```
134137

135-
### copycache.service ###
138+
### home-xinouch-.mozilla.mount ###
139+
140+
Same as in the previous section.
141+
142+
### userramdisk.service ###
136143

137-
In the section [Unit], change the key `RequiresMountsFor` for the last element:
144+
In the section [Unit], change the key `RequiresMountsFor`:
138145

139146
```
140-
RequiresMountsFor=/var/cache /var/log /home/{USER}/.cache
147+
RequiresMountsFor=/home/{USER}/.cache /home/{USER}/.mozilla
141148
```
142149

143150
### ramdisk_cache.sh ###
144151

145-
You need to change all the `persistent` and `user` config variables at the top of the file:
152+
You need to change the `ramdisks` variable with your paths:
146153

147154
```bash
148-
cacheUser="/home/xinouch/.cache"
149-
persistent="/mnt/persistent/system"
150-
persistentLog="/mnt/persistent/log"
151-
persistentUser="/mnt/persistent/home"
155+
declare -A ramdisks=(
156+
["cache"]="/var/cache /mnt/persistent/system"
157+
["log"]="/var/log /mnt/persistent/log"
158+
["usercache"]="/home/xinouch/.cache /mnt/persistent/home"
159+
["firefoxsession"]="/home/xinouch/.mozilla /mnt/persistent/firefox"
160+
)
152161
```
153162

154163
Here, we assume that everything will be saved into a partition mounted in `/mnt/persistent`. You can save it in your home if you want, though you shouldn't store root data in your home... Anyway, be sure that the filesystem for the persistency is NOT `NTFS` but a Linux thing like `ext4`, or you will destroy the permissions... It is also recommended to mount it with the following permissions, as it should be just a backup: `defaults,nodev,noexec,nosuid,noatime,nodiratime`.
@@ -161,15 +170,17 @@ Now that everything is correctly configured, we need to install everything in th
161170
```
162171
# cp ramdisk_cache.sh /usr/local/sbin/
163172
# chmod u+x /usr/local/sbin/ramdisk_cache.sh
164-
# cp copycache.service /etc/systemd/system/
165-
# cp var-cache.mount var-log.mount home-{USER}-.cache.mount /usr/share/systemd/
173+
# cp systemramdisk.service /etc/systemd/system/
174+
# cp userramdisk.service /etc/systemd/system/
175+
# cp var-cache.mount var-log.mount home-{USER}-.cache.mount home-{USER}-.mozilla.mount /usr/share/systemd/
166176
167177
# ln -s /usr/share/systemd/var-cache.mount /etc/systemd/system/var-cache.mount
168178
# ln -s /usr/share/systemd/var-log.mount /etc/systemd/system/var-log.mount
169179
# ln -s /usr/share/systemd/home-{USER}-.cache.mount /etc/systemd/system/home-{USER}-.cache.mount
180+
# ln -s /usr/share/systemd/home-{USER}-.mozilla.mount /etc/systemd/system/home-{USER}-.mozilla.mount
170181
171182
# systemctl daemon-reload
172-
# systemctl enable var-cache.mount var-log.mount home-{USER}-.cache.mount copycache.service
183+
# systemctl enable var-cache.mount var-log.mount home-{USER}-.cache.mount home-{USER}-.mozilla.mount systemramdisk.service userramdisk.service
173184
```
174185

175186
Before rebooting, we need to save the cache (and check everything works :p), so run `ramdisk_cache.sh` as root and check that your persistent folders are filled with the correct data.
@@ -178,7 +189,7 @@ We also need to setup a `cron` so the script is run every X minutes (here every
178189

179190
```
180191
# crontab -e
181-
* */1 * * * /usr/local/sbin/ramdisk_cache.sh
192+
0 * * * * /usr/local/sbin/ramdisk_cache.sh cache log usercache firefoxsession
182193
```
183194

184195
Get rid of `/var/log`
@@ -196,14 +207,14 @@ Get rid of `/var/log`
196207

197208
At the install step, don't copy, link or enable the file `var-log.mount`.
198209

199-
### Change the service copycache ###
210+
### Change the service systemramdisk ###
200211

201-
In the section `Unit` of `copycache.service`, change the keys `Before` and `RequiresMountsFor`. If you already installed it, do the changes to the file `/etc/systemd/system/copycache.service`:
212+
In the section `Unit` of `systemramdisk.service`, change the keys `Before` and `RequiresMountsFor`. If you already installed it, do the changes to the file `/etc/systemd/system/systemramdisk.service`:
202213

203214
```
204215
Before=sysinit.target shutdown.target
205216
...
206-
RequiresMountsFor=/var/cache /home/{USER}/.cache
217+
RequiresMountsFor=/var/cache
207218
```
208219

209220
### Change the script ###
@@ -212,6 +223,11 @@ Remove everything related to logs. If you already did the install, change the fi
212223

213224
Finish by executing `systemctl daemon-reload` as root.
214225

226+
Change the user ramdisks
227+
------------------------
228+
229+
In the same way, edit the files corresponding to the user ramdisks if you don't want it.
230+
215231
License
216232
=======
217233

home-xinouch-.mozilla.mount

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Debian-Systemd-Ramdisk (DSR) is a config repo used to create a ramdisk with systemd.
2+
# Copyright (C) 2017 Rémi Ducceschi (remileduc) <remi.ducceschi@gmail.com>
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
# systemd unit file that mounts the user's cache folder
18+
[Unit]
19+
Description=User Firefox session Directory
20+
Documentation=man:hier(7)
21+
Documentation=http://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
22+
ConditionPathIsSymbolicLink=!/home/xinouch/.mozilla
23+
DefaultDependencies=no
24+
Conflicts=umount.target
25+
Before=local-fs.target umount.target
26+
After=swap.target
27+
28+
[Mount]
29+
What=tmpfs
30+
Where=/home/xinouch/.mozilla
31+
Type=tmpfs
32+
Options=mode=1777,strictatime,nosuid,nodev
33+
34+
[Install]
35+
WantedBy=local-fs.target

ramdisk_cache.sh

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,47 @@
2020

2121
set -efu
2222

23-
cache="/var/cache"
24-
log="/var/log"
25-
cacheUser="/home/xinouch/.cache"
26-
persistent="/mnt/persistent/system"
27-
persistentLog="/mnt/persistent/log"
28-
persistentUser="/mnt/persistent/home"
23+
# we create a dictionnary whose values are strings with space separated paths.
24+
# the first path is the mounted ramdisk, the second path is the save folder.
25+
26+
declare -A ramdisks=(
27+
# ["test"]="mounted persistent" # mkdir mounted persistent; touch persistent/test
28+
["cache"]="/var/cache /mnt/persistent/system"
29+
["log"]="/var/log /mnt/persistent/log"
30+
["usercache"]="/home/xinouch/.cache /mnt/persistent/home"
31+
["firefoxsession"]="/home/xinouch/.mozilla /mnt/persistent/firefox"
32+
)
2933
lockfile=".cache.lock"
3034
logfile="/tmp/ramdisk_cache.log"
3135

32-
# first we copy system cache
33-
# if the lockfile exists in cache, we save the cache in persistent
34-
if [ -e "$cache"/"$lockfile" ]; then
35-
rsync -aqu --delete --exclude "$lockfile" "$cache/" "$persistent/"
36-
echo "$(date -Iseconds) - system cache saved to persistent" >> "$logfile"
37-
else # we put persistent cache in ramdisk
38-
rsync -aq "$persistent/" "$cache/"
39-
touch "$cache"/"$lockfile"
40-
echo "$(date -Iseconds) - system cache loaded from persistent" >> "$logfile"
41-
fi
42-
43-
# second we copy system log
44-
# if the lockfile exists in cache, we save the cache in persistent
45-
if [ -e "$log"/"$lockfile" ]; then
46-
rsync -aqu --delete --exclude "$lockfile" "$log/" "$persistentLog/"
47-
echo "$(date -Iseconds) - logs saved to persistent" >> "$logfile"
48-
else # we put persistent cache in ramdisk
49-
rsync -aq "$persistentLog/" "$log/"
50-
touch "$log"/"$lockfile"
51-
echo "$(date -Iseconds) - logs loaded from persistent" >> "$logfile"
52-
fi
53-
54-
# then we copy user cache
55-
# if the lockfile exists in cache, we save the cache in persistent
56-
if [ -e "$cacheUser"/"$lockfile" ]; then
57-
rsync -aqu --delete --exclude "$lockfile" "$cacheUser/" "$persistentUser/"
58-
echo "$(date -Iseconds) - user cache saved to persistent" >> "$logfile"
59-
else # we put persistent cache in ramdisk
60-
rsync -aq "$persistentUser/" "$cacheUser/"
61-
touch "$cacheUser"/"$lockfile"
62-
echo "$(date -Iseconds) - user cache loaded from persistent" >> "$logfile"
63-
fi
36+
function manageRamdisk { # mounted, persistent
37+
mounted="$1"
38+
persistent="$2"
39+
40+
# if the lockfile exists in mounted, we save the content in persistent
41+
if [ -e "$mounted"/"$lockfile" ]; then
42+
rsync -aqu --delete --exclude "$lockfile" "$mounted/" "$persistent/"
43+
echo "$(date -Iseconds) - $mounted saved to persistent" >> "$logfile"
44+
else # we put persistent content in ramdisk
45+
rsync -aq "$persistent/" "$mounted/"
46+
touch "$mounted"/"$lockfile"
47+
echo "$(date -Iseconds) - $mounted loaded from persistent" >> "$logfile"
48+
fi
49+
}
50+
51+
for ramdisk in $@; do
52+
mounted="$(echo ${ramdisks[$ramdisk]} | cut -sd ' ' -f 1)"
53+
persistent="$(echo ${ramdisks[$ramdisk]} | cut -sd ' ' -f 2)"
54+
55+
# if wrong parameter, we terminate
56+
if [ -z "$mounted" ] || [ -z "$persistent" ]; then
57+
echo "'$ramdisk' is not valid." >> "$logfile"
58+
echo "'$ramdisk' is not valid. Use one of these:"
59+
for r in "${!ramdisks[@]}"; do
60+
echo -e "\t$r"
61+
done
62+
exit 1
63+
fi
64+
65+
manageRamdisk "$mounted" "$persistent"
66+
done
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616

1717
# systemd service unit file that calls the script to load / save persistent data
1818
[Unit]
19-
Description=Copy or Save cache folders
19+
Description=Copy or Save SYSTEM cache folders
2020
DefaultDependencies=no
2121
Before=rsyslog.service syslog.target systemd-journald.service sysinit.target shutdown.target
2222
After=local-fs.target
2323
Conflicts=shutdown.target
24-
RequiresMountsFor=/var/cache /var/log /home/xinouch/.cache
24+
RequiresMountsFor=/var/cache /var/log
2525
IgnoreOnIsolate=yes
2626

2727
[Service]
2828
Type=oneshot
29-
ExecStart=/usr/local/sbin/ramdisk_cache.sh
30-
ExecStop=/usr/local/sbin/ramdisk_cache.sh
29+
ExecStart=/usr/local/sbin/ramdisk_cache.sh cache log
30+
ExecStop=/usr/local/sbin/ramdisk_cache.sh cache log
3131
RemainAfterExit=yes
3232

3333
[Install]

userramdisk.service

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Debian-Systemd-Ramdisk (DSR) is a config repo used to create a ramdisk with systemd.
2+
# Copyright (C) 2017 Rémi Ducceschi (remileduc) <remi.ducceschi@gmail.com>
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
# systemd service unit file that calls the script to load / save persistent data
18+
[Unit]
19+
Description=Copy or Save USER cache folders
20+
DefaultDependencies=no
21+
# This may need to be changed: sysinit.target may not be the best requirement
22+
Before=sysinit.target shutdown.target
23+
After=local-fs.target
24+
Conflicts=shutdown.target
25+
RequiresMountsFor=/home/xinouch/.cache /home/xinouch/.mozilla
26+
IgnoreOnIsolate=yes
27+
28+
[Service]
29+
Type=oneshot
30+
ExecStart=/usr/local/sbin/ramdisk_cache.sh usercache firefoxsession
31+
ExecStop=/usr/local/sbin/ramdisk_cache.sh usercache firefoxsession
32+
RemainAfterExit=yes
33+
34+
[Install]
35+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)