Skip to content

Instantly share code, notes, and snippets.

@Low-power
Last active August 2, 2021 04:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Low-power/c903a393d331f41aaae2d540afa77934 to your computer and use it in GitHub Desktop.
Save Low-power/c903a393d331f41aaae2d540afa77934 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Copyright 2015-2021 Rivoreo
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
set -e
case "`systemctl is-system-running`" in
running|degraded)
;;
*)
echo "systemd not ready" 1>&2
exit 1
;;
esac
if [ ! -h /var/run ]; then
if [ -d /var/run ]; then
ln -s /run/shm /var/run/
elif [ -e /var/run ]; then
rm -f /var/run
fi
fi
if [ ! -e /var/run ]; then
ln -s /run /var/
fi
[ -e /run/shm ] && { [ -h /run/shm ] || [ ! -d /run/shm ]; } && rm -f /run/shm
if [ -h /etc/systemd/system-generators/systemd-fstab-generator ] && [ "`readlink /etc/systemd/system-generators/systemd-fstab-generator`" = /dev/null ]; then
use_systemd_fstab_generator=
else
use_systemd_fstab_generator=1
fi
need_remove_dev_shm_from_fstab=
if [ -n "$use_systemd_fstab_generator" ]; then
if systemctl is-active --quiet run-shm.mount; then
need_add_run_shm_to_fstab=
else
need_add_run_shm_to_fstab=1
fi
else
if [ -f /etc/systemd/system/mount-shm.service ]; then
need_add_run_shm_to_fstab=
else
need_add_run_shm_to_fstab=1
fi
fi
while read line; do
set -- $line
[ $# != 6 ] && continue
case "$2" in
/dev/shm*)
need_remove_dev_shm_from_fstab=1
;;
/run/shm|/run/shm/|/var/run/shm|/var/run/shm/)
need_add_run_shm_to_fstab=
;;
esac
done < /etc/fstab
[ -n "$need_remove_dev_shm_from_fstab" ] && sed -i -r '/[[:space:]]\/dev\/shm/d' /etc/fstab
[ -n "$need_add_run_shm_to_fstab" ] && echo "tmpfs /run/shm/ tmpfs rw,nodev,nosuid 0 0" >> /etc/fstab
[ -f /etc/systemd/system/remove-dev-shm.service ] || cat > /etc/systemd/system/remove-dev-shm.service << EOF
[Unit]
Description=Continuously removing /dev/shm
[Service]
Type=simple
ExecStart=/bin/sh -c "while true; do if [ -d /dev/shm ]; then umount /dev/shm/ || umount -l /dev/shm/; rmdir /dev/shm || rm -rf /dev/shm; fi; sleep 4; done"
StandardOutput=null
StandardError=null
Restart=always
[Install]
#WantedBy=sysinit.target
WantedBy=local-fs.target
EOF
if [ -n "$need_add_run_shm_to_fstab" ]; then
#/lib/systemd/system-generators/systemd-fstab-generator || true
systemctl daemon-reload
fi
mountpoint /dev/shm && fuser -mk /dev/shm || true
if [ -n "$use_systemd_fstab_generator" ]; then
systemctl start run-shm.mount
elif [ -f /etc/systemd/system/mount-shm.service ]; then
systemctl enable --now mount-shm
else
for m in /run/shm/ /var/run/shm/; do
[ -d $m ] || mkdir $m
mount $m && break
done
fi
systemctl enable --now remove-dev-shm.service
need_tmpfiles_entry=
for f in /etc/tmpfiles.d/*.conf; do
if [ -f "$f" ] && grep -Eq '^[[:space:]]*L\+?[[:space:]]+/run/shm/?[[:space:]]' "$f"; then
sed -i -r '/^[[:space:]]*L\+?[[:space:]]+\/run\/shm\/?[[:space:]]/d' "$f"
need_tmpfiles_entry=1
fi
done
for f in /usr/lib/tmpfiles.d/*.conf; do
overriding_file="/etc/tmpfiles.d/${f##*/}"
if [ -f "$f" ] && [ ! -f "$overriding_file" ] && grep -Eq '^[[:space:]]*L\+?[[:space:]]+/run/shm/?[[:space:]]' "$f"; then
sed -r '/^[[:space:]]*L\+?[[:space:]]+\/run\/shm\/?[[:space:]]/d' "$f" > "$overriding_file"
need_tmpfiles_entry=1
fi
done
[ -n "$need_tmpfiles_entry" ] && echo "d /run/shm 01777 0 0 -" > /etc/tmpfiles.d/shm.conf
for f in /lib/*/libpthread.so.0 /usr/lib/*/libqb.so.0 /usr/lib/firefox-esr/libxul.so; do
[ -f "$f" ] && grep -Fq /dev/shm "$f" && sed -i 's /dev/shm /run/shm g' "$f"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment