| 1 | #!/bin/bash |
| 2 | |
| 3 | set -e |
| 4 | |
| 5 | ############################################################################### |
| 6 | # Script to create a virtual machine for testing NetworkManager. |
| 7 | # |
| 8 | # Commands: |
| 9 | # - build: build a new VM, named "$VM" ("nm") |
| 10 | # Args: the name of the OS to build, from `virt-builder --list` (Fedora by default) |
| 11 | # - run: start the VM. |
| 12 | # - exec: run bash inside the VM, connecting via ssh (this is the default). |
| 13 | # Args: command to execute inside the VM (then, ssh session won't open). |
| 14 | # - stop: stop the VM. |
| 15 | # - reexec: stop and exec. |
| 16 | # - clean: stop the VM and delete the image. |
| 17 | # |
| 18 | # Options (they MUST go before the command): |
| 19 | # -v | --vm: name of the VM to run the command on (by default 'nm'). |
| 20 | # This allows one to have more than one VM at the same time |
| 21 | # -h | --help: show this text |
| 22 | # |
| 23 | # NetworkManager directories: |
| 24 | # |
| 25 | # The NetworkManager root directory is mounted in the VM as a filesystem share. |
| 26 | # You can run `meson install` and run tests. |
| 27 | # |
| 28 | # Create a symlink ./.git/NetworkManager-ci, to share the CI directory too. |
| 29 | # |
| 30 | # Required packages: |
| 31 | # |
| 32 | # Your host needs libvirt, libvirt-nss and guestfs-tools. To access the VM with |
| 33 | # `ssh root@$VM`, configure /etc/nsswitch.conf as explained in |
| 34 | # https://libvirt.org/nss.html (otherwise, `nm-in-vm exec` won't work, either). |
| 35 | # |
| 36 | # Prepare for testing: |
| 37 | # |
| 38 | # There is a script nm-env-prepare.sh to generate a net1 interface for testing. |
| 39 | # Currently NM-ci requires a working eth1, so use this before running a CI test: |
| 40 | # `nm-env-prepare.sh --prefix eth -i 1 && sleep 1 && nmcli device connect eth1` |
| 41 | # |
| 42 | # Additional VMs: |
| 43 | # |
| 44 | # By default, the VM named 'nm' is created, but additional ones can be created: |
| 45 | # $ nm-in-vm -v nm2 build |
| 46 | # $ nm-in-vm -v nm2 exec |
| 47 | # $ nm-in-vm -v nm2 stop |
| 48 | # |
| 49 | # Choosing a different OS: |
| 50 | # |
| 51 | # By default Fedora is used, but you can choose a different OS. Most from the |
| 52 | # list `virt-builder --list` will work. |
| 53 | # $ nm-in-vm build debian-12 |
| 54 | # $ nm-in-vm exec |
| 55 | # $ nm-in-vm stop |
| 56 | ############################################################################### |
| 57 | |
| 58 | # Check for libvirt |
| 59 | if ! (command -v virsh && command -v virt-builder && command -v virt-install) &>/dev/null; then |
| 60 | echo "libvirt and guestfs-tools are required" >&2 |
| 61 | exit 1 |
| 62 | fi |
| 63 | |
| 64 | # set defaults if user didn't define these values |
| 65 | VM=${VM:="nm"} |
| 66 | OS_VERSION=${OS_VERSION:=} |
| 67 | RAM=${RAM:=2048} |
| 68 | IMAGE_SIZE=${IMAGE_SIZE=20G} |
| 69 | ROOT_PASSWORD=${ROOT_PASSWORD:=nm} |
| 70 | LIBVIRT_POOL=${LIBVIRT_POOL:=default} # only useful if BASEDIR_VM_IMAGE is empty |
| 71 | BASEDIR_VM_IMAGE=${BASEDIR_VM_IMAGE:=} |
| 72 | BASEDIR_NM=${BASEDIR_NM:=} |
| 73 | BASEDIR_NM_CI=${BASEDIR_NM_CI:=} |
| 74 | HOST_BRIDGE=${HOST_BRIDGE:=virbr0} |
| 75 | SSH_LOG_LEVEL=${SSH_LOG_LEVEL:=ERROR} |
| 76 | |
| 77 | if [[ -z $OS_VERSION ]]; then |
| 78 | # if running Fedora, select same version, else select latest Fedora |
| 79 | if grep -q "^ID=fedora$" /etc/os-release 2>/dev/null ; then |
| 80 | OS_VERSION="$(sed -n 's/^VERSION_ID=\([0-9]\+\)$/fedora-\1/p' /etc/os-release)" |
| 81 | else |
| 82 | OS_VERSION=$(virt-builder --list | grep '^fedora' | sort | tail -n 1 | cut -d" " -f 1) |
| 83 | fi |
| 84 | fi |
| 85 | |
| 86 | if [[ -z $BASEDIR_NM ]]; then |
| 87 | BASEDIR_NM="$(readlink -f "$(dirname -- "$BASH_SOURCE")/..")" |
| 88 | fi |
| 89 | |
| 90 | if [[ -z $BASEDIR_NM_CI && -d "$BASEDIR_NM/.git/NetworkManager-ci" ]]; then |
| 91 | BASEDIR_NM_CI="$(readlink -f "$BASEDIR_NM/.git/NetworkManager-ci")" |
| 92 | fi |
| 93 | |
| 94 | if [[ -z $BASEDIR_VM_IMAGE ]]; then |
| 95 | libvirt_pool_path_xml=$(virsh pool-dumpxml $LIBVIRT_POOL | grep -F '<path>') |
| 96 | if [[ $libvirt_pool_path_xml =~ \<path\>(.*)\</path\> ]]; then |
| 97 | BASEDIR_VM_IMAGE=${BASH_REMATCH[1]} |
| 98 | else |
| 99 | BASEDIR_VM_IMAGE=$BASEDIR_NM |
| 100 | fi |
| 101 | fi |
| 102 | |
| 103 | ############################################################################## |
| 104 | |
| 105 | do_build() { |
| 106 | local t=$'\t' |
| 107 | local ram |
| 108 | local size |
| 109 | local os_variant |
| 110 | local nm_ci_build_args |
| 111 | local nm_ci_install_args |
| 112 | local extra_pkgs |
| 113 | local install_pkgs |
| 114 | local install_files |
| 115 | local gen_files=( |
| 116 | "bin-nm-env-prepare.sh:/usr/bin/nm-env-prepare.sh" |
| 117 | "bin-nm-deploy.sh:/usr/bin/nm-deploy.sh" |
| 118 | "etc-motd-vm:/etc/motd" |
| 119 | "etc-bashrc.my:/etc/bashrc.my" |
| 120 | "nm-90-my.conf:/etc/NetworkManager/conf.d/90-my.conf" |
| 121 | "nm-95-user.conf:/etc/NetworkManager/conf.d/95-user.conf" |
| 122 | "home-bash_history:/root/.bash_history" |
| 123 | "home-gdbinit:/root/.gdbinit" |
| 124 | "home-gdb_history:/root/.gdb_history" |
| 125 | "home-behaverc:/root/.behaverc" |
| 126 | "systemd-10-host-net.link:/etc/systemd/network/10-host-net.link" |
| 127 | "systemd-dhcp-host.service:/etc/systemd/system/dhcp-host.service" |
| 128 | "systemd-20-nm.override:/etc/systemd/system/NetworkManager.service.d/20-nm.override" |
| 129 | ) |
| 130 | |
| 131 | (( $# > 1 )) && die "build only accepts one argument" |
| 132 | (( $# > 0 )) && OS_VERSION="$1" |
| 133 | os_variant=${OS_VERSION//-/} # virt-install --os-variant value, deduced from OS_VERSION |
| 134 | os_variant=${os_variant/centosstream/centos-stream} |
| 135 | |
| 136 | if [[ ! $VM =~ ^[a-zA-Z0-9\-]*$ ]]; then |
| 137 | echo "Invalid VM name '$VM', use only letters, numbers and '-' character" |
| 138 | return 1 |
| 139 | fi |
| 140 | |
| 141 | if vm_is_installed; then |
| 142 | echo "The virtual machine '$VM' is already installed, skiping build" >&2 |
| 143 | return 0 |
| 144 | fi |
| 145 | |
| 146 | if vm_image_exists; then |
| 147 | echo "The image '$basedir_vm_image/$vm_image_file' already exists, skiping build" >&2 |
| 148 | return 0 |
| 149 | fi |
| 150 | |
| 151 | if [[ -n $IMAGE_SIZE ]]; then |
| 152 | size=(--size "$IMAGE_SIZE") |
| 153 | fi |
| 154 | |
| 155 | if [[ -n $BASEDIR_NM_CI ]]; then |
| 156 | nm_ci_build_args=( |
| 157 | --mkdir "$BASEDIR_NM_CI" |
| 158 | --link "$BASEDIR_NM_CI:/NetworkManager-ci" |
| 159 | --append-line "/etc/fstab:/NM_CI${t}$BASEDIR_NM_CI${t}9p${t}trans=virtio,rw,_netdev${t}0${t}0" |
| 160 | ) |
| 161 | nm_ci_install_args=( |
| 162 | --filesystem "$BASEDIR_NM_CI,/NM_CI" |
| 163 | ) |
| 164 | fi |
| 165 | |
| 166 | if [[ $OS_VERSION =~ fedora || $OS_VERSION =~ centosstream ]]; then |
| 167 | extra_pkgs=(bash-completion bind-utils ccache clang-tools-extra cryptsetup cscope \'dbus\*\' |
| 168 | dhcp-client dhcp-relay dhcp-server dnsmasq dracut-network ethtool firewalld gcc gdb |
| 169 | glibc-langpack-pl hostapd intltool iproute ipsec-tools iputils iscsi-initiator-utils |
| 170 | iw ldns libreswan libselinux-utils libyaml-devel logrotate lvm2 mdadm mlocate net-tools |
| 171 | NetworkManager NetworkManager-openvpn NetworkManager-ovs NetworkManager-ppp |
| 172 | NetworkManager-pptp NetworkManager-strongswan NetworkManager-team NetworkManager-vpnc |
| 173 | NetworkManager-wifi nfs-utils nispor nmap-ncat nmstate nss-tools openvpn |
| 174 | \'openvswitch2\*\' perl-IO-Pty-Easy perl-IO-Tty procps psmisc python3-behave |
| 175 | python3-black python3-devel python3-netaddr python3-pip python3-pyte python3-pyyaml |
| 176 | qemu-kvm radvd rp-pppoe scsi-target-utils strace systemd tcpdump tcpreplay tuned |
| 177 | /usr/bin/debuginfo-install /usr/bin/pytest /usr/bin/python vim wireguard-tools |
| 178 | wireshark-cli) |
| 179 | |
| 180 | if [[ $OS_VERSION == centosstream-8 ]]; then |
| 181 | install_pkgs=( |
| 182 | --run-command "dnf -y copr enable nmstate/nm-build-deps" |
| 183 | --run-command "dnf config-manager -y --set-enabled powertools" |
| 184 | --install epel-release,epel-next-release |
| 185 | ) |
| 186 | elif [[ $OS_VERSION == centosstream-9 ]]; then |
| 187 | install_pkgs=( |
| 188 | --run-command "dnf -y copr enable nmstate/nm-build-deps" |
| 189 | --run-command "dnf config-manager -y --set-enabled crb" |
| 190 | --install epel-release,epel-next-release |
| 191 | ) |
| 192 | fi |
| 193 | |
| 194 | install_pkgs+=( |
| 195 | --update |
| 196 | --run "$BASEDIR_NM/contrib/fedora/REQUIRED_PACKAGES" |
| 197 | --run-command "dnf install -y --skip-broken ${extra_pkgs[*]}" |
| 198 | --run-command "pip3 install --user behave_html_formatter" |
| 199 | --run-command "dnf debuginfo-install -y --skip-broken NetworkManager \ |
| 200 | \$(ldd /usr/sbin/NetworkManager \ |
| 201 | | sed -n 's/.* => \(.*\) (0x[0-9A-Fa-f]*)\$/\1/p' \ |
| 202 | | xargs -n1 readlink -f)" |
| 203 | ) |
| 204 | elif [[ $OS_VERSION =~ debian || $OS_VERSION =~ ubuntu ]]; then |
| 205 | extra_pkgs=(bash-completion bind9-utils ccache clang-tools cryptsetup cscope \'dbus\*\' |
| 206 | isc-dhcp-client isc-dhcp-relay isc-dhcp-server dnsmasq dracut-network ethtool firewalld |
| 207 | gcc gdb hostapd intltool iproute2 \'iputils-\*\' iw libldns3 libreswan |
| 208 | libyaml-dev logrotate lvm2 mdadm mlocate net-tools network-manager |
| 209 | network-manager-openvpn network-manager-pptp network-manager-strongswan |
| 210 | network-manager-vpnc nfs-common ncat libnss3-tools openvpn \'openvswitch2\*\' procps |
| 211 | psmisc python3-behave black python3-dev python3-netaddr python3-pip python3-pyte |
| 212 | python3-pretty-yaml qemu-kvm radvd pppoe strace systemd tcpdump tcpreplay tuned |
| 213 | debian-goodies python3-pytest python3 vim wireguard-tools tshark) |
| 214 | |
| 215 | if [[ $OS_VERSION =~ debian ]]; then |
| 216 | install_pkgs=( |
| 217 | --run-command "echo deb http://deb.debian.org/debian-debug/ \$(lsb_release -cs)-debug main >> /etc/apt/sources.list.d/debug.list" |
| 218 | --run-command "echo deb http://deb.debian.org/debian-debug/ \$(lsb_release -cs)-proposed-updates-debug main >> /etc/apt/sources.list.d/debug.list" |
| 219 | ) |
| 220 | elif [[ $OS_VERSION =~ ubuntu ]]; then |
| 221 | install_pkgs=( |
| 222 | --run-command "echo deb http://ddebs.ubuntu.com \$(lsb_release -cs) main restricted universe multiverse >> /etc/apt/sources.list.d/debug.list" |
| 223 | --run-command "echo deb http://ddebs.ubuntu.com \$(lsb_release -cs)-updates main restricted universe multiverse >> /etc/apt/sources.list.d/debug.list" |
| 224 | --run-command "echo deb http://ddebs.ubuntu.com \$(lsb_release -cs)-proposed main restricted universe multiverse >> /etc/apt/sources.list.d/debug.list" |
| 225 | ) |
| 226 | fi |
| 227 | |
| 228 | install_pkgs+=( |
| 229 | --update |
| 230 | --upload "$BASEDIR_NM/contrib/debian/REQUIRED_PACKAGES:/tmp/REQUIRED_PACKAGES" |
| 231 | --run-command "/bin/bash /tmp/REQUIRED_PACKAGES" # using only --run fails |
| 232 | --run-command "apt-get install -y \$(find-dbgsym-packages NetworkManager 2>/dev/null)" |
| 233 | --edit "/etc/locale.gen:s/^# pl_PL.UTF-8/pl_PL.UTF-8/" |
| 234 | --run-command "locale-gen" |
| 235 | ) |
| 236 | |
| 237 | for p in "${extra_pkgs[@]}"; do |
| 238 | install_pkgs+=(--run-command "apt-get install -y $p || :") |
| 239 | done |
| 240 | fi |
| 241 | |
| 242 | install_files=(--upload "$BASEDIR_NM/contrib/scripts/NM-log:/usr/bin/NM-log") |
| 243 | for f in "${gen_files[@]}"; do |
| 244 | gen_file "${f%:*}" |
| 245 | install_files+=(--upload "$datadir/data-$f") |
| 246 | done |
| 247 | |
| 248 | echo "Creating VM" |
| 249 | echo " - VM NAME: $VM" |
| 250 | echo " - OS VERSION: $OS_VERSION" |
| 251 | echo " - SIZE: $([[ -n $IMAGE_SIZE ]] && echo "$IMAGE_SIZE" || echo "don't resize")" |
| 252 | echo " - RAM: $RAM" |
| 253 | echo " - ROOT PASSWORD: $ROOT_PASSWORD" |
| 254 | echo " - IMAGE PATH: $basedir_vm_image/$vm_image_file" |
| 255 | echo " - NM DIR: $BASEDIR_NM" |
| 256 | echo " - NM CI DIR: $([[ -n $BASEDIR_NM_CI ]] && echo "$BASEDIR_NM_CI" || echo '<none>')" |
| 257 | echo " - HOST BRIDGE: $HOST_BRIDGE" |
| 258 | |
| 259 | if [[ $OS_VERSION =~ centosstream ]]; then |
| 260 | echo "WARNING: NetworkManager repositories can't be shared with the guest" \ |
| 261 | "(CentOS Stream doesn't support 9P filesystem). You'll need to manually" \ |
| 262 | "share by NFS or make a new clone of the repository inside the guest." >&2 |
| 263 | fi |
| 264 | |
| 265 | virt-builder "$OS_VERSION" \ |
| 266 | --output "$basedir_vm_image/$vm_image_file" \ |
| 267 | "${size[@]}" \ |
| 268 | --format qcow2 \ |
| 269 | --arch x86_64 \ |
| 270 | --hostname "$VM" \ |
| 271 | --root-password password:nm \ |
| 272 | --ssh-inject root \ |
| 273 | --append-line "/etc/ssh/sshd_config:PermitRootLogin yes" \ |
| 274 | --run-command "ssh-keygen -A" \ |
| 275 | --mkdir "$BASEDIR_NM" \ |
| 276 | --link "$BASEDIR_NM:/NetworkManager" \ |
| 277 | --append-line "/etc/fstab:/NM${t}$BASEDIR_NM${t}9p${t}trans=virtio,rw,_netdev${t}0${t}0" \ |
| 278 | "${nm_ci_build_args[@]}" \ |
| 279 | "${install_pkgs[@]}" \ |
| 280 | --mkdir "/etc/systemd/system/NetworkManager.service.d" \ |
| 281 | --mkdir "/etc/systemd/network" \ |
| 282 | "${install_files[@]}" \ |
| 283 | --write "/var/lib/NetworkManager/secret_key:nm-in-container-secret-key" \ |
| 284 | --chmod "700:/var/lib/NetworkManager" \ |
| 285 | --chmod "600:/var/lib/NetworkManager/secret_key" \ |
| 286 | --edit "/etc/systemd/journald.conf:s/.*RateLimitBurst=.*/RateLimitBurst=0/" \ |
| 287 | --delete "/etc/NetworkManager/system-connections/*" \ |
| 288 | --append-line "/etc/bashrc:. /etc/bashrc.my" \ |
| 289 | --run-command "updatedb" \ |
| 290 | --append-line "/etc/dhcp/dhclient.conf:send host-name = gethostname();" \ |
| 291 | --firstboot-command "systemctl enable --now dhcp-host" |
| 292 | |
| 293 | virt-install \ |
| 294 | --name "$VM" \ |
| 295 | --ram "$RAM" \ |
| 296 | --disk "path=$basedir_vm_image/$vm_image_file,format=qcow2" \ |
| 297 | --os-variant "$os_variant" \ |
| 298 | --filesystem "$BASEDIR_NM,/NM" \ |
| 299 | "${nm_ci_install_args[@]}" \ |
| 300 | --network "bridge=$HOST_BRIDGE" \ |
| 301 | --import \ |
| 302 | --autoconsole none \ |
| 303 | --noreboot |
| 304 | } |
| 305 | |
| 306 | do_clean() { |
| 307 | vm_is_running && virsh shutdown "$VM" &>/dev/null |
| 308 | vm_is_installed && virsh undefine "$VM" &>/dev/null |
| 309 | rm -f "$basedir_vm_image/$vm_image_file" |
| 310 | } |
| 311 | |
| 312 | do_run() { |
| 313 | vm_is_installed || do_build |
| 314 | vm_is_running && return 0 |
| 315 | virsh start "$VM" >&2 |
| 316 | } |
| 317 | |
| 318 | do_exec() { |
| 319 | do_run |
| 320 | |
| 321 | local failed=0 |
| 322 | while ! ping -c 1 "$VM" &>/dev/null; do |
| 323 | failed=$((failed + 1)) |
| 324 | (( failed < 15 )) || die "Timeout trying to ping the VM" |
| 325 | sleep 1 |
| 326 | done |
| 327 | |
| 328 | ssh \ |
| 329 | -o StrictHostKeyChecking=no \ |
| 330 | -o UserKnownHostsFile=/dev/null \ |
| 331 | -o LogLevel="$SSH_LOG_LEVEL" \ |
| 332 | "root@$VM" "$@" |
| 333 | } |
| 334 | |
| 335 | do_reexec() { |
| 336 | vm_is_running && do_stop |
| 337 | |
| 338 | local waited=0 |
| 339 | while vm_is_running; do |
| 340 | waited=$((waited + 1)) |
| 341 | (( waited < 30 )) || die "Timeout waiting for VM shutdown" |
| 342 | sleep 1 |
| 343 | done |
| 344 | |
| 345 | do_exec "$@" |
| 346 | } |
| 347 | |
| 348 | do_stop() { |
| 349 | vm_is_running && virsh shutdown "$VM" >&2 |
| 350 | } |
| 351 | |
| 352 | ############################################################################### |
| 353 | |
| 354 | vm_image_exists() { |
| 355 | [[ -f "$basedir_vm_image/$vm_image_file" ]] || return 1 |
| 356 | } |
| 357 | |
| 358 | vm_is_installed() { |
| 359 | virsh list --all --name | grep --fixed-strings --line-regexp "$VM" &>/dev/null || return 1 |
| 360 | } |
| 361 | |
| 362 | vm_is_running() { |
| 363 | virsh list --name | grep --fixed-strings --line-regexp "$VM" &>/dev/null || return 1 |
| 364 | } |
| 365 | |
| 366 | gen_file() { |
| 367 | sed "s|{{BASEDIR_NM}}|$BASEDIR_NM|g" "$datadir/$1.in" > "$datadir/data-$1" |
| 368 | if [[ $1 =~ ^bin- ]]; then |
| 369 | chmod 755 "$datadir/data-$1" |
| 370 | else |
| 371 | chmod 644 "$datadir/data-$1" |
| 372 | fi |
| 373 | } |
| 374 | |
| 375 | usage() { |
| 376 | echo "nm-in-vm [options] build|run|exec|stop|reexec|clean [command_args]" |
| 377 | } |
| 378 | |
| 379 | help() { |
| 380 | usage |
| 381 | echo |
| 382 | awk '/^####*$/ { if (on) exit; on=-1; } !/^####*$/ { if (on) print(substr($0,3)) }' "$BASH_SOURCE" |
| 383 | echo |
| 384 | } |
| 385 | |
| 386 | die() { |
| 387 | echo "$1" >&2 |
| 388 | exit 1 |
| 389 | } |
| 390 | |
| 391 | ############################################################################### |
| 392 | |
| 393 | cmd= |
| 394 | |
| 395 | while (( $# > 0 )); do |
| 396 | case "$1" in |
| 397 | build|run|exec|reexec|stop|clean) |
| 398 | cmd="$1" |
| 399 | shift |
| 400 | ;; |
| 401 | -h|--help) |
| 402 | help |
| 403 | exit 0 |
| 404 | ;; |
| 405 | -v|--vm) |
| 406 | (( $# > 1 )) || die "--vm requires one argument" |
| 407 | VM="$2" |
| 408 | shift 2 |
| 409 | ;; |
| 410 | --) |
| 411 | shift |
| 412 | break |
| 413 | ;; |
| 414 | *) |
| 415 | [[ $cmd != "" ]] && break |
| 416 | echo "Invalid argument '$1'" >&2 |
| 417 | echo $(usage) >&2 |
| 418 | exit 1 |
| 419 | ;; |
| 420 | esac |
| 421 | done |
| 422 | |
| 423 | # compute some values that depends on user selectable variables |
| 424 | mkdir -p "$BASEDIR_VM_IMAGE" |
| 425 | basedir_vm_image=$(readlink -f "$BASEDIR_VM_IMAGE") |
| 426 | vm_image_file="$VM.qcow2" |
| 427 | datadir="$BASEDIR_NM/tools/nm-guest-data" |
| 428 | |
| 429 | if [[ $cmd == "" ]]; then |
| 430 | cmd=exec |
| 431 | fi |
| 432 | |
| 433 | if [[ $UID == 0 ]]; then |
| 434 | die "cannot run as root" |
| 435 | fi |
| 436 | |
| 437 | if [[ $cmd != exec && $cmd != reexec && $cmd != build && $# != 0 ]]; then |
| 438 | die "Extra arguments are only allowed with exec|reexec|build command" |
| 439 | fi |
| 440 | |
| 441 | do_$cmd "$@" |