File: //bigscoots/lxd/ord/migrate-eth0-to-vlan10.sh
#!/bin/bash
# Configuration variables
VLAN_ID=10
VLAN_NAME="eth0.10"
CONF_DIR="/etc/systemd/network"
BACKUP_DIR="/etc/systemd/network/backup_$(date +%Y%m%d_%H%M%S)"
# 1. Grab current IP and Mask from eth0
# Note: This assumes eth0 currently has the IP you want to migrate.
CURRENT_IP=$(ip -o -4 addr show eth0 | awk '{print $4}' | head -n1)
CURRENT_GW="167.253.104.1" # Hardcoded as requested
if [ -z "$CURRENT_IP" ]; then
echo "Error: Could not detect IP on eth0. Exiting."
exit 1
fi
echo "Detected IP: $CURRENT_IP. Preparing migration to VLAN $VLAN_ID..."
# 2. Create backup directory and move old configs
echo "Backing up current network configurations to $BACKUP_DIR"
mkdir -p "$BACKUP_DIR"
# Move existing configs to avoid conflicts
mv $CONF_DIR/*.network $CONF_DIR/*.netdev "$BACKUP_DIR/" 2>/dev/null || true
# 3. Create 00-eth0.network (The Physical Link)
# This tells systemd-networkd to treat eth0 as a carrier for the VLAN
cat <<EOF > $CONF_DIR/00-eth0.network
[Match]
Name=eth0
[Network]
VLAN=$VLAN_NAME
LinkLocalAddressing=no
IPv6AcceptRA=no
EOF
# 4. Create 01-eth0.10.netdev (The Virtual Device)
cat <<EOF > $CONF_DIR/01-eth0.10.netdev
[NetDev]
Name=$VLAN_NAME
Kind=vlan
[VLAN]
Id=$VLAN_ID
EOF
# 5. Create 02-eth0.10.network (The Tagged IP Config)
cat <<EOF > $CONF_DIR/02-eth0.10.network
[Match]
Name=$VLAN_NAME
[Network]
Address=$CURRENT_IP
Gateway=$CURRENT_GW
DNS=1.1.1.1 1.0.0.1
EOF
echo "------------------------------------------------"
echo "New configuration generated in $CONF_DIR"
echo "Backup of old files located in $BACKUP_DIR"
echo ""
echo "WARNING: Applying these changes may drop your connection."
echo "To apply, run:"
echo "sudo networkctl reload && sudo systemctl restart systemd-networkd"