Ram Disks
This revision is from 2024/06/25 00:15. You can Restore it.
Two scripts, one is the system service so that the ram disk is re-instated upon reboot, ram volatility is obviously the situation with ram disks and the ram disk script.
Setup RAM disk service on Linux
# Filename: setup-ramdisk.service # /etc/systemd/system/setup-ramdisk.service # sudo cp /home/x/ram_disk/setup-ramdisk.service /etc/systemd/system/setup-ramdisk.service # sudo systemctl daemon-reload # sudo systemctl enable setup-ramdisk # sudo systemctl start setup-ramdisk # After reboot check with df -h | grep ramdisk # sudo systemctl status setup-ramdisk[Unit]
Description=Setup RAM disk and start web server
After=network.target
[Service]
Type=oneshot
ExecStart=/home/x/ram_disk/setup-ramdisk.sh
RemainAfterExit=true
[Install]
WantedBy=multi-user.target
The second script involves keeping a copy of linux as chrooted jail for a user. Alpine Linux is sufficiently small that its works for this purpose. The second program is named schroot, apt-get install schroot. Putting the ram disk in a chroot jail insures we do not use the disk at all, a lib or two could sneak under the radar causing a bottleneck.
#!/bin/bash # Set variablesRAMDISK_SIZE="1G"
RAM_DISK="/mnt/ramdisk"
# Create the mount point if it doesn't existmkdir -p $RAM_DISK
# Mount the RAM diskmount -t tmpfs -o size=$RAMDISK_SIZE tmpfs $RAM_DISK
# Configure schroot (not part of the script) # sudo apt-get install schroot # [jimbo] # description=Chroot for username # directory=/mnt/ramdisk # users=jimbo # root-groups=root # Add filesystemsudo tar -xzf /home/x/ram_disk/alpine-minirootfs-3.20.0-x86_64.tar.gz -C ${RAM_DISK}
# Setup special directories by binding them to the host systemsudo mount -t proc /proc ${RAM_DISK}/proc
sudo mount -t sysfs /sys ${RAM_DISK}/sys
sudo mount --rbind /dev ${RAM_DISK}/dev
sudo mount --rbind /dev/pts ${RAM_DISK}/dev/pts
sudo mount --rbind /run ${RAM_DISK}/run
# Set permissionssudo chmod 1777 ${RAM_DISK}/tmp
# Set up jimbo usersudo schroot -c jimbo --run-session -- mkdir -p /home/jimbo
sudo schroot -c jimbo --run-session -- chown jimbo:jimbo /home/jimbo
# touch nginx pid file, nginxmkdir -p /run/nginx
sudo touch /run/nginx/nginx.pid
sudo schroot -c jimbo --directory=/home/jimbo -- nginx
# Note: make sure /etc/resolve.conf has DNS value such as nameserver 8.8.8.8 and nameserver 8.8.4.4 # sudo mount --bind /run/systemd/resolve/resolv.conf ${RAM_DISK}/etc/resolv.conf #echo "nameserver 8.8.8.8" | sudo tee ${RAM_DISK}/etc/resolv.conf #echo "nameserver 8.8.4.4" | sudo tee -a ${RAM_DISK}/etc/resolv.conf #echo "It works, cocksucker!" | sudo tee -a ${RAM_DISK}/var/www/localhost/htdocs/index.html #sudo schroot -c jimbo #sudo schroot -c jimbo --directory=/home/jimbo
A more complex script removes the mounts, but requires MAKEDEV to be copied into alpine-minirootfs-3.20.0-x86_64.tar.gz
#!/bin/bash # Set variablesRAMDISK_SIZE="1G"
RAM_DISK="/mnt/ramdisk"
# Create the mount point if it doesn't existmkdir -p $RAM_DISK
# Mount the RAM diskmount -t tmpfs -o size=$RAMDISK_SIZE tmpfs $RAM_DISK