[pve-devel] [PATCH qemu-server 4/4] add firstboot debian sample

Alexandre Derumier aderumier at odiso.com
Fri Oct 6 16:49:14 CEST 2017


simple example of firstboot script to generate network config
---
 firstboot.debian | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 119 insertions(+)
 create mode 100755 firstboot.debian

diff --git a/firstboot.debian b/firstboot.debian
new file mode 100755
index 0000000..8ab4e46
--- /dev/null
+++ b/firstboot.debian
@@ -0,0 +1,119 @@
+#!/bin/bash
+
+cdr2mask ()
+{
+   # Number of args to shift, 255..255, first non-255 byte, zeroes
+   set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
+   [ $1 -gt 1 ] && shift $1 || shift
+   echo ${1-0}.${2-0}.${3-0}.${4-0}
+}
+
+
+MODULE="qemu_fw_cfg"
+
+if lsmod | grep "$MODULE" &> /dev/null ; then
+  echo "$MODULE is loaded!"
+else
+  echo "$MODULE is not loaded!"
+  insmod /lib/modules/`uname -r`/kernel/drivers/firmware/qemu_fw_cfg.ko
+fi
+
+
+while IFS=": " read -r key value; do
+  case "$key" in
+    '#'*) ;;
+    *)
+      eval "$key=\"$value\""
+  esac
+done < /sys/firmware/qemu_fw_cfg/by_name/opt/Proxmox/raw
+
+
+
+
+if [ ! -z $ipconfig0 ];then
+
+    echo "# This file describes the network interfaces available on your system" > /etc/network/interfaces
+    echo "# and how to activate them. For more information, see interfaces(5)." >> /etc/network/interfaces
+    echo " " >> /etc/network/interfaces
+    echo "# The loopback network interface" >> /etc/network/interfaces
+    echo "auto lo" >> /etc/network/interfaces
+    echo "iface lo inet loopback" >> /etc/network/interfaces
+    echo ""
+    echo "# The primary network interface" >> /etc/network/interfaces
+    echo "auto eth0" >> /etc/network/interfaces
+
+
+    while IFS="," read  -a values; do
+
+       for key in "${!values[@]}"
+       do
+	   while IFS="=" read -r k v; do
+             case "$k" in
+             "ip" )
+              IFS='/'
+                declare -a ip_address=(${v})
+                netmask=$( cdr2mask ${ip_address[1]} )
+                echo "iface eth0 inet static" >> /etc/network/interfaces
+                echo "        address $ip_address" >> /etc/network/interfaces
+                echo "        netmask $netmask" >> /etc/network/interfaces
+             ;;
+             "gw" )
+                echo "        gateway $v" >> /etc/network/interfaces
+             ;;
+             esac
+
+	   done <<< ${values[${key}]}
+
+       done
+    done <<< $ipconfig0
+fi
+
+if [ ! -z $searchdomain ];then
+    echo $searchdomain
+    echo "search $searchdomain" > /etc/resolv.conf
+
+fi
+
+if [ ! -z $nameserver ];then
+    echo $nameserver
+    echo "nameserver $nameserver" >> /etc/resolv.conf
+fi
+
+if [ ! -z $sshkey ];then
+    echo $sshkey > /root/.ssh/authorized_keys
+fi
+
+if [ ! -z $name ];then
+    echo $name > /etc/hostname
+    hostnamectl set-hostname $name
+    hostname $name
+    if [ ! -z $searchdomain ];then
+       echo "$name.$searchdomain" >/etc/mailname
+    fi
+fi
+
+
+/etc/init.d/networking restart
+sleep 10
+
+
+#generate ssh keys
+echo ""
+echo "Generating unique host keys for your SSH server..."
+find /etc/ssh -name "ssh_host_*_key*" -exec rm {} \;
+DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical dpkg-reconfigure openssh-server
+echo ""
+echo "Key generation complete"
+
+
+#update
+
+apt-get update
+apt-get dist-upgrade
+
+#disable firstboot script
+systemctl disable firstboot
+
+echo "*** Configuration complete - reboot the system..."
+reboot
+
-- 
2.11.0



More information about the pve-devel mailing list