So you decided to get a PXE boot set up going and you needed a place to store the ISO files, the kernel, the initrd file, and the rootfs squashfs image. You scratched your head and thought, why don’t I just use the USB method? You then remembered that the server is in your garage, it’s cold out, and you don’t feel like bundling up to go install an OS that you may wipe away in a few days (why are we all so lazy?).
Out of pure laziness, you are now recommitted to possibly hours of work instead of the 10 minutes it would take to bundle up and go out to the rack.
You remembered that old laptop collecting dust in the closet. You dig it out, blow off the cobwebs, pray it still works as it slowly boots up.
Miraculously, it springs to life! You scan the specs - not much, but enough to install Ubuntu. You get to work installing the OS, DHCP server, TFTP server, copying over the files you need. Many hours later, beads of sweat on your forehead, you finally have a working PXE server!
The age old, “I am too lazy, let me automate this” excuse. Let’s be honest, we have all been there.
I decided to write a simple guide on how to host files using Apache server. I will show you my script and explain what we are doing. I am using Harvester as my example. I won’t be sharing my PXE files.
#!/bin/bash
# Update package list and install apache2
sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install -y apache2
# Enable required Apache modules
sudo a2enmod proxy proxy_http
# Create a directory to store ISO files and set permissions
ISO_DIR="/var/www/html/iso"
sudo mkdir -p ${ISO_DIR}
sudo chown -R www-data:www-data ${ISO_DIR}
sudo chmod -R 755 ${ISO_DIR}
# Create a virtual host configuration for serving ISO files
VHOST_CONF="/etc/apache2/sites-available/iso.conf"
cat > ${VHOST_CONF} << EOL
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot ${ISO_DIR}
ErrorLog \${APACHE_LOG_DIR}/iso-error.log
CustomLog \${APACHE_LOG_DIR}/iso-access.log combined
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:80/
ProxyPassReverse / http://127.0.0.1:80/
</VirtualHost>
EOL
# Enable the ISO virtual host
sudo a2ensite iso
# Restart Apache
sudo systemctl restart apache2
# Print the message if successful
if systemctl status apache2; then
echo "Apache is successfully configured to serve ISO files from ${ISO_DIR}"
else
echo "Failed to setup apache2. It is probably your fault! Fix it!"
exit 1
fi
#Define the base URL
BASE_URL="https://releases.rancher.com/harvester/v1.2.1/harvester-v1.2.1"
#Download the ISO files
for file in amd64.iso initrd-amd64 vmlinuz-amd64 rootfs-amd64.squashfs
do
wget -P ${ISO_DIR} ${BASE_URL}-${file} || \
{ echo "BRO! Failed to download ${file}! Exiting script. Fix that script!"; exit 1; }
done
echo "Wow! the ISO files downloaded to ${ISO_DIR}"
As you can see, I like to add comments throughout my scripts. I also like to add snarky error codes. But what does the script do?
Here's a line-by-line explanation:
The `apt-get update` and `apt-get upgrade` commands are used to update the package lists for upgrades for packages that need upgrading. This is crucial to ensure the server has the latest security patches and software updates.
`sudo apt-get install -y apache2` - Installs the Apache2 HTTP server, `-y` flag is used to automatically confirm the installation.
`sudo a2enmod proxy proxy_http` - Enables Apache proxy modules. These modules are required for redirecting network traffic proxied by Apache.
`ISO_DIR="/var/www/html/iso"` - Defines a variable ISO_DIR holding the path where ISO files will be stored.
`mkdir -p ${ISO_DIR}` - Creates a directory (and its parents if needed) for storing ISO files. The `-p` flag helps ensure the full path is created if it doesn’t exist.
`chown -R www-data:www-data ${ISO_DIR}` and `chmod -R 755 ${ISO_DIR}` - Set permissions of the directory so that the Apache server can properly access it.
The lines within `cat > ${VHOST_CONF} << EOL ... EOL` create a new Apache virtual host configuration file with specific settings for serving the ISO files.
`sudo a2ensite iso` - Enables the newly created Apache site configuration.
`sudo systemctl restart apache2` - Restarts the Apache2 service to apply all the changes made.
The ensuing `if` statement checks if the Apache server is up and running successfully, providing a success or failure message accordingly.
`BASE_URL="https://releases.rancher.com/harvester/v1.2.1/harvester-v1.2.1"` - Defines a BASE_URL from where to download ISO files.
The `for` loop starting at `for file in amd64.iso initrd-amd64 vmlinuz-amd64 rootfs-amd64.squashfs` downloads the ISO files to the created directory.
After each file is downloaded, there is an error check to verify the successful download of each file. Upon download failure, a message is displayed and the script is terminated.
Finally, upon successfully downloading all of the ISO files, a message is displayed signifying completion of the downloads.
You can hit the IP of the VM and navigate to /iso. You should see your files. It’s pretty simple.
It goes without saying, only expose this to your local network. Do not share this with the public internet.
Cheers,
Joe