Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/sh
# Copyright 2015-2020 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; 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
for f in /lib/x86_64-linux-gnu/libpthread.so.0 /usr/lib/x86_64-linux-gnu/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
You can’t perform that action at this time.