From 82307f6d493cb05ea05d8b5eca17ad2f9d02f71a Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Tue, 22 Nov 2022 10:31:20 +0100 Subject: [PATCH] for fedora 37: network initialization (for static IP only for now) --- lbuild-initvm.sh | 92 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 72 insertions(+), 20 deletions(-) diff --git a/lbuild-initvm.sh b/lbuild-initvm.sh index 6897c5d3..d312d509 100755 --- a/lbuild-initvm.sh +++ b/lbuild-initvm.sh @@ -90,20 +90,25 @@ function package_method () { } ### return -# ifcfg for redhat's -# interfaces for older debian/uuntu -# systemd for more recent debian/ubuntu -function network_method () { +# ifcfg for fedora up to 36 +# networkmanager for fedora starting with f37 (probably works with older as well...) +# interfaces for older debian/uuntu +# systemd for more recent debian/ubuntu +function network_config_method () { local fcdistro=$1; shift case $fcdistro in - f[0-9]*|centos[0-9]*|sl[0-9]*) + # have not used centos or scientific linux for a very long time + #f[0-9]*|centos[0-9]*|sl[0-9]*) + f2*|f3[0-6]) echo ifcfg ;; + f3[7-9]|f[4-9]*) + echo networkmanager ;; wheezy|jessie|trusty|xenial|bionic) echo interfaces ;; focal|jammy) echo systemd ;; *) - echo "Unknown network_method for distro $fcdistro" ;; + echo "Unknown network_config_method for distro $fcdistro" ;; esac } @@ -298,21 +303,39 @@ EOF fedora_configure_systemd $lxc - local guest_ifcfg=${lxc_root}/etc/sysconfig/network-scripts/ifcfg-$VIF_GUEST - mkdir -p $(dirname ${guest_ifcfg}) - # starting with f27, we go for NetworkManager - # no more NM_CONTROLLED nonsense - if [ -n "$NAT_MODE" ]; then - write_guest_ifcfg_natip - else - write_guest_ifcfg_publicip - fi > $guest_ifcfg + fedora_configure_network $lxc [ -z "$IMAGE" ] && fedora_configure_yum $lxc $fcdistro $pldistro return 0 } +function fedora_configure_network() { + local lxc="$1" + case $(network_config_method $fcdistro) in + ifcfg) + # probably no longer useful + local guest_ifcfg=${lxc_root}/etc/sysconfig/network-scripts/ifcfg-$VIF_GUEST + mkdir -p $(dirname ${guest_ifcfg}) + if [ -n "$NAT_MODE" ]; then + write_guest_ifcfg_natip + else + write_guest_ifcfg_publicip + fi > $guest_ifcfg + ;; + networkmanager) + local guest_keyfile=${lxc_root}/etc/NetworkManager/system-connections/'Wired connection 1.nmconnection' + mkdir -p $(dirname "${guest_keyfile}") + if [ -n "$NAT_MODE" ]; then + write_guest_networkmanager_natip + else + write_guest_networkmanager_publicip + fi > "$guest_keyfile" + chmod 600 "$guest_keyfile" + ;; + esac +} + # this code of course is for guests that do run on systemd function fedora_configure_systemd() { set -e @@ -331,9 +354,7 @@ function fedora_configure_systemd() { # sed -i 's/After=dev-%i.device/After=/' ${lxc_root}/lib/systemd/system/getty\@.service ln -sf /dev/null ${lxc_root}/etc/systemd/system/"getty@.service" rm -f ${lxc_root}/etc/systemd/system/getty.target.wants/*service || : -# can't seem to handle this one with systemctl -# second part should trigger starting with fedora31, where the network target is not manually manageable - chroot ${lxc_root} $personality chkconfig network on || chroot ${lxc_root} $personality systemctl enable NetworkManager + chroot ${lxc_root} $personality systemctl enable NetworkManager } # overwrite container yum config @@ -427,7 +448,7 @@ EOF function debian_configure () { local lxc=$1; shift local fcdistro=$1; shift - case $(network_method $fcdistro) in + case $(network_config_method $fcdistro) in interfaces) local guest_interfaces=${lxc_root}/etc/network/interfaces ( [ -n "$NAT_MODE" ] \ @@ -485,6 +506,37 @@ Gateway=$GATEWAY EOF } +function uuid() { + python -c "import uuid; print(uuid.uuid1())" +} +function write_guest_networkmanager_natip() { + cat << EOF +EOF +} +function write_guest_networkmanager_publicip() { + cat << EOF +[connection] +id=Wired connection 1 +uuid=$(uuid) +type=ethernet +autoconnect-priority=-999 +interface-name=${VIF_GUEST} + +[ethernet] + +[ipv4] +address1=${GUEST_IP}/${MASKLEN},${GATEWAY} +dhcp-hostname=${GUEST_HOSTNAME} +method=auto + +[ipv6] +addr-gen-mode=default +method=auto + +[proxy] +EOF +} + ############################## function setup_lxc() { @@ -811,7 +863,7 @@ function post_install_myplc () { [ -f /etc/sysconfig/network ] || /bin/echo NETWORKING=yes > /etc/sysconfig/network # turn off regular crond, as plc invokes plc_crond - /sbin/chkconfig crond off + /sbin/chkconfig crond off >& /dev/null # customize root's prompt /bin/cat << PROFILE > /root/.profile -- 2.43.0