X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lbuild-initvm.sh;h=fd4059dc53dc561b62fb4f38ec745ca103c31eb3;hb=228e5b2b7f689837761f9ca1fb4d669e0545d5b7;hp=b0e732c4262982cf51c879e386f3f51fa0f06fc9;hpb=4a63162c62e4ae7d6834b399ec3fd837ab4d9611;p=build.git diff --git a/lbuild-initvm.sh b/lbuild-initvm.sh index b0e732c4..fd4059dc 100755 --- a/lbuild-initvm.sh +++ b/lbuild-initvm.sh @@ -30,7 +30,7 @@ function lxcroot () { # XXX fixme : when creating a 32bits VM we need to call linux32 as appropriate...s -DEFAULT_FCDISTRO=f33 +DEFAULT_FCDISTRO=f37 DEFAULT_PLDISTRO=lxc DEFAULT_PERSONALITY=linux64 DEFAULT_MEMORY=3072 @@ -82,10 +82,33 @@ function package_method () { case $fcdistro in f[0-9]*|centos[0-9]*|sl[0-9]*) echo dnf ;; - wheezy|jessie|trusty|xenial|bionic) + wheezy|jessie|trusty|xenial|bionic|focal|jammy) echo debootstrap ;; *) - echo Unknown distro $fcdistro ;; + echo "Unknown package_method for distro $fcdistro" ;; + esac +} + +### return +# 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 + # 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_config_method for distro $fcdistro" ;; esac } @@ -280,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 @@ -313,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 @@ -378,7 +417,7 @@ function debian_mirror () { case $fcdistro in wheezy|jessie) echo http://ftp2.fr.debian.org/debian/ ;; - trusty|xenial|bionic) + trusty|xenial|bionic|focal|jammy) echo http://www-ftp.lip6.fr/pub/linux/distributions/Ubuntu/archive/ ;; *) echo unknown distro $fcdistro; exit 1;; esac @@ -407,8 +446,23 @@ EOF } function debian_configure () { - local guest_interfaces=${lxc_root}/etc/network/interfaces - ( [ -n "$NAT_MODE" ] && write_guest_interfaces_natip || write_guest_interfaces_publicip ) > $guest_interfaces + local lxc=$1; shift + local fcdistro=$1; shift + case $(network_config_method $fcdistro) in + interfaces) + local guest_interfaces=${lxc_root}/etc/network/interfaces + ( [ -n "$NAT_MODE" ] \ + && write_guest_interfaces_natip \ + || write_guest_interfaces_publicip ) > $guest_interfaces + ;; + systemd) + local systemd_config="${lxc_root}/etc/systemd/network/wired.network" + ( [ -n "$NAT_MODE" ] \ + && write_guest_systemd_natip \ + || write_guest_systemd_publicip ) > $systemd_config + chroot "${lxc_root}" systemctl enable systemd-networkd + ;; + esac } function write_guest_interfaces_natip () { @@ -427,6 +481,56 @@ netmask $NETMASK gateway $GATEWAY EOF } + +# systemd-networkd +# https://wiki.archlinux.org/title/systemd-networkd +# https://www.linuxtricks.fr/wiki/systemd-le-reseau-avec-systemd-networkd +function write_guest_systemd_natip () { + cat << EOF +[Match] +Name=eth0 + +[Network] +DHCP=ipv4 +EOF +} + +function write_guest_systemd_publicip () { + cat << EOF +[Match] +Name=eth0 + +[Network] +Address=${GUEST_IP}/${MASKLEN} +Gateway=$GATEWAY +EOF +} + +function uuid() { + python -c "import uuid; print(uuid.uuid1())" +} +# xxx this seems to be no longer needed ? +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 +# setting this seems to be counter-productive +# interface-name=${VIF_GUEST} + +[ipv4] +address1=${GUEST_IP}/${MASKLEN},${GATEWAY} +#dhcp-hostname=${GUEST_HOSTNAME} +method=manual +EOF +} + ############################## function setup_lxc() { @@ -460,7 +564,7 @@ function setup_lxc() { if [ -z "$IMAGE" ]; then debian_install $lxc || { echo "failed to install debian/ubuntu root image"; exit 1 ; } fi - debian_configure || { echo "failed to configure debian/ubuntu for a container"; exit 1 ; } + debian_configure $lxc $fcdistro || { echo "failed to configure debian/ubuntu for a container"; exit 1 ; } ;; *) echo "$COMMAND:: unknown package_method - exiting" @@ -474,7 +578,14 @@ function setup_lxc() { ### set up resolv.conf from host # ubuntu precise and on, /etc/resolv.conf is a symlink to ../run/resolvconf/resolv.conf [ -h $lxc_root/etc/resolv.conf ] && rm -f $lxc_root/etc/resolv.conf - cp /etc/resolv.conf $lxc_root/etc/resolv.conf + ### since fedora36, our hosts use systemd-resolved, but the guests can't use that + # so the administrator has the option to create /etc/resolv.conf.containers + # and if that file exists it will be copied in the containers instead of /etc/resolv.conf + if [ -f /etc/resolv.conf.containers ]; then + cp /etc/resolv.conf.containers $lxc_root/etc/resolv.conf + else + cp /etc/resolv.conf $lxc_root/etc/resolv.conf + fi ### and /etc/hosts for at least localhost [ -f $lxc_root/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $lxc_root/etc/hosts @@ -746,7 +857,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 @@ -950,8 +1061,8 @@ function main () { GUEST_IP=$(gethostbyname $GUEST_HOSTNAME) # use same NETMASK as bridge interface br0 - masklen=$(ip addr show $PUBLIC_BRIDGE | grep -v inet6 | grep inet | awk '{print $2;}' | cut -d/ -f2) - NETMASK=$(masklen_to_netmask $masklen) + MASKLEN=$(ip addr show $PUBLIC_BRIDGE | grep -v inet6 | grep inet | awk '{print $2;}' | cut -d/ -f2) + NETMASK=$(masklen_to_netmask $MASKLEN) GATEWAY=$(ip route show | grep default | awk '{print $3}' | head -1) VIF_HOST="vif$(echo $GUEST_HOSTNAME | cut -d. -f1)" fi