Ram Disks

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 variables

RAMDISK_SIZE="1G"

RAM_DISK="/mnt/ramdisk"

# Create the mount point if it doesn't exist

mkdir -p $RAM_DISK

# Mount the RAM disk

mount -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 filesystem

sudo 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 system

sudo 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 permissions

sudo chmod 1777 ${RAM_DISK}/tmp

# Set up jimbo user

sudo schroot -c jimbo --run-session -- mkdir -p /home/jimbo

sudo schroot -c jimbo --run-session -- chown jimbo:jimbo /home/jimbo

# touch nginx pid file, nginx

mkdir -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 variables

RAMDISK_SIZE="1G"

RAM_DISK="/mnt/ramdisk"

# Create the mount point if it doesn't exist

mkdir -p $RAM_DISK

# Mount the RAM disk

mount -t tmpfs -o size=$RAMDISK_SIZE tmpfs $RAM_DISK

# Configure schroot # sudo apt-get install schroot # [jimbo] # description=Chroot for username # directory=/mnt/ramdisk # users=jimbo # root-groups=root # Make filesystem

sudo tar -xzf /home/x/ram_disk/alpine-minirootfs-3.20.0-x86_64.tar.gz -C ${RAM_DISK}

# Set permissions

sudo chmod 1777 ${RAM_DISK}/tmp

#Make dev

mkdir -p ${RAM_DISK}/dev

sudo schroot -c jimbo -u root -- sh -c "cd /dev && /usr/sbin/MAKEDEV std"

sudo schroot -c jimbo --run-session -- mount -t dev dev /dev

# Setup proc within the chroot, do not bind

mkdir -p ${RAM_DISK}/proc

sudo schroot -c jimbo --run-session -- mount -t proc proc /proc

# Setup sys in ram, do not bind

mkdir -p ${RAM_DISK}/sys

sudo schroot -c jimbo --run-session -- mount -t sysfs sysfs /sys

# Setup run in ram, do not bind, no mont required

mkdir -p ${RAM_DISK}/run

mkdir -p ${RAM_DISK}/run/nginx

sudo touch ${RAM_DISK}/run/nginx/nginx.pid

########################################################## # Every program requires a pid to be made from this script ########################################################## # Set up jimbo user

sudo schroot -c jimbo --run-session -- mkdir -p /home/jimbo

sudo schroot -c jimbo --run-session -- chown jimbo:jimbo /home/jimbo

# Enter chroot and start services, run applications at boot

sudo schroot -c jimbo --directory=/home/jimbo -- nginx

# Add permanent resolve.conf to alpine-minirootfs-3.20.0-x86_64.tar.gz # rather than generating it. # 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, cock." | sudo tee -a ${RAM_DISK}/var/www/localhost/htdocs/index.html # How to login #sudo schroot -c jimbo #sudo schroot -c jimbo --directory=/home/jimbo

The root filesystem used with the above changes: alpine-minirootfs-3.20.0-x86_64.tar.gz

  

📝 📜 ⏱️ ⬆️