Fix swap once again

This commit is contained in:
Micke Nordin 2023-02-23 16:52:36 +01:00
parent 2d50ca2dac
commit eec40a23ba
Signed by untrusted user: Micke
GPG key ID: 0DA0A7A5708FE257

View file

@ -1,23 +1,21 @@
#!/bin/bash #!/bin/bash
# Clean up old swap version # Clean up old swap version
if [[ -f /swapfile ]]; then if [[ -f /cryptswap1 ]]; then
swapoff -a swapoff -a
sed -i 's_^/swapfile none swap sw 0 0_#/swapfile none swap sw 0 0_' /etc/fstab sed -i 's_^/dev/mapper/cryptswap1 none swap sw 0 0__' /etc/fstab
rm /swapfile rm /swapfile
fi fi
# Create crypt swap that should play nice with puppet if ! [[ -f /swapfile ]]; then
if ! [[ -f /cryptswap1 ]]; then
gb=$(free --gibi| grep Mem: | awk '{print $2}') gb=$(free --gibi| grep Mem: | awk '{print $2}')
fallocate -l "${gb}G" /cryptswap1 fallocate -l "${gb}G" /swapfile
chmod 600 /cryptswap1 chmod 600 /swapfile
loop=$(losetup -f) mkswap /swapfile
losetup "${loop}" /cryptswap1 swapon /swapfile
cryptsetup open --type plain --key-file /dev/urandom "${loop}" cryptswap1
mkswap /dev/mapper/cryptswap1
swapon /dev/mapper/cryptswap1
if ! grep -E '^/dev/mapper/cryptswap1' /etc/fstab; then if ! grep -E '^/swapfile' /etc/fstab; then
echo '/dev/mapper/cryptswap1 none swap sw 0 0' >> /etc/fstab echo '/swapfile none swap sw 0 0' >> /etc/fstab
fi fi
else
swapon -a
fi fi