Tags
#!/bin/bash
# =============================================================================
# Script Name: lxc_create_container.sh
# Version: 1.1
# Description: This script creates a new LXC container from a template, assigns
#              a unique MAC address, updates the hostname and /etc/hosts file,
#              and verifies internet access.
# =============================================================================

# Prompt for the new container hostname
read -e -p "LXCHOSTNAME: " LXCHOSTNAME
export LXCHOSTNAME

# Check if the template container is running and stop it if necessary
if lxc-info -n template | grep -q 'RUNNING'; then
    echo "Stopping the template container..."
    if ! lxc-stop -n template; then
        echo "Failed to stop the template container."
        exit 1
    fi
else
    echo "Template container is not running."
fi

# Copy the template to create a new container with the given hostname
echo "Creating a new container with hostname: $LXCHOSTNAME..."
if ! lxc-copy -n template -N "$LXCHOSTNAME"; then
    echo "Failed to copy the template container."
    exit 1
fi

# Function to generate a unique MAC address
generate_unique_hwaddr() {
    local hwaddr
    local existing_hwaddrs
    while : ; do
        hwaddr=$(printf '00:16:3e:%02x:%02x:%02x\n' $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)))
        existing_hwaddrs=$(grep "lxc.net.0.hwaddr" /var/lib/lxc/*/config | grep "$hwaddr")
        if [ -z "$existing_hwaddrs" ]; then
            # MAC address is unique
            echo "$hwaddr"
            return
        fi
    done
}

# Generate a unique MAC address
NEW_HWADDR=$(generate_unique_hwaddr)

# Path to the LXC configuration file
CONFIG_FILE="/var/lib/lxc/$LXCHOSTNAME/config"

# Replace the existing hwaddr line
echo "Updating MAC address in $CONFIG_FILE to $NEW_HWADDR..."
if ! sed -i "/^lxc.net.0.hwaddr/c\lxc.net.0.hwaddr = $NEW_HWADDR" "$CONFIG_FILE"; then
    echo "Failed to update MAC address in $CONFIG_FILE."
    exit 1
fi

# Start the new container
echo "Starting the new container..."
if ! lxc-start -n "$LXCHOSTNAME"; then
    echo "Failed to start the container $LXCHOSTNAME."
    exit 1
fi

# Wait for the container to start
sleep 5

# Change the hostname inside the container
echo "Changing the hostname inside the container..."
if ! lxc-attach -n "$LXCHOSTNAME" -- bash -c "echo '$LXCHOSTNAME' > /etc/hostname" || \
   ! lxc-attach -n "$LXCHOSTNAME" -- hostname "$LXCHOSTNAME"; then
    echo "Failed to set the hostname inside the container."
    exit 1
fi

# Update /etc/hosts
echo "Updating /etc/hosts inside the container..."
if ! lxc-attach -n "$LXCHOSTNAME" -- bash -c "echo '127.0.0.1 $LXCHOSTNAME' >> /etc/hosts"; then
    echo "Failed to update /etc/hosts inside the container."
    exit 1
fi

# Ensure the container has internet access (optional)
echo "Checking internet connectivity inside the container..."
if ! lxc-attach -n "$LXCHOSTNAME" -- ping -c 4 google.com; then
    echo "Container $LXCHOSTNAME does not have internet access."
fi
echo

# Stop and restart the container
echo "Restarting the container..."
if ! lxc-stop -n "$LXCHOSTNAME" || ! lxc-start -n "$LXCHOSTNAME"; then
    echo "Failed to restart the container $LXCHOSTNAME."
    exit 1
fi

# Display the MAC addresses to verify the changes
echo "Displaying the MAC addresses to verify the changes..."
grep lxc.net.0.hwaddr /var/lib/lxc/*/config

# Wait and list containers to ensure they are running
sleep 9
echo "Listing all containers..."
lxc-ls -f

echo "LXC container setup completed successfully."