odl-ovs noha: Support VLAN tagged public
[fuel.git] / mcp / scripts / lib_jump_deploy.sh
index a96beca..3dc3c44 100644 (file)
@@ -104,7 +104,7 @@ function __kernel_modules {
 function __mount_image {
   local image=$1
   local image_dir=$2
-  OPNFV_MNT_DIR="${image_dir}/ubuntu"
+  OPNFV_MNT_DIR="${image_dir}/mnt"
 
   # Find free nbd, loop devices
   for dev in '/sys/class/block/nbd'*; do
@@ -117,29 +117,68 @@ function __mount_image {
   OPNFV_MAP_DEV=/dev/mapper/$(basename "${OPNFV_NBD_DEV}")p1
   export OPNFV_MNT_DIR OPNFV_LOOP_DEV
   [ -n "${OPNFV_NBD_DEV}" ] && [ -n "${OPNFV_LOOP_DEV}" ] || exit 1
-  qemu-img resize "${image_dir}/${image}" 3G
+  [[ "${MCP_OS:-}" =~ centos ]] || \
+    qemu-img resize "${image_dir}/${image}" 3G
   sudo qemu-nbd --connect="${OPNFV_NBD_DEV}" --aio=native --cache=none \
     "${image_dir}/${image}"
   sudo kpartx -av "${OPNFV_NBD_DEV}"
   # Hardcode partition index to 1, unlikely to change for Ubuntu UCA image
   sudo partx -uvn 1:1 "${OPNFV_NBD_DEV}"
-  if sudo growpart "${OPNFV_NBD_DEV}" 1; then
+  if [[ "${MCP_OS:-}" =~ ubuntu ]] && sudo growpart "${OPNFV_NBD_DEV}" 1
+  then
+    if [ -e "${image_dir}/e2fsprogs" ]; then
+      E2FSCK_PREFIX="${image_dir}/e2fsprogs/e2fsck/"
+      RESIZE_PREFIX="${image_dir}/e2fsprogs/resize/"
+    fi
     sudo kpartx -u "${OPNFV_NBD_DEV}"
-    sudo e2fsck -pf "${OPNFV_MAP_DEV}"
-    sudo resize2fs "${OPNFV_MAP_DEV}"
+    sudo "${E2FSCK_PREFIX}e2fsck" -pf "${OPNFV_MAP_DEV}"
+    sudo "${RESIZE_PREFIX}resize2fs" "${OPNFV_MAP_DEV}"
   else
     sleep 5 # /dev/nbdNp1 takes some time to come up
   fi
   sudo partx -d "${OPNFV_NBD_DEV}"
-  # grub-update does not like /dev/nbd*, so use a loop device to work around it
-  sudo losetup "${OPNFV_LOOP_DEV}" "${OPNFV_MAP_DEV}"
   mkdir -p "${OPNFV_MNT_DIR}"
-  sudo mount "${OPNFV_LOOP_DEV}" "${OPNFV_MNT_DIR}"
+  if [ "$(uname -i)" = "aarch64" ] && [[ "${MCP_OS:-}" =~ centos ]]; then
+    # AArch64 CentOS cloud image contains a broken shim binary
+    # https://bugzilla.redhat.com/show_bug.cgi?id=1527283
+    sudo mount "${OPNFV_MAP_DEV}" "${OPNFV_MNT_DIR}"
+    sudo cp -f --remove-destination "${OPNFV_MNT_DIR}/EFI/BOOT/fbaa64.efi" \
+                                    "${OPNFV_MNT_DIR}/EFI/BOOT/BOOTAA64.EFI"
+    sudo umount -l "${OPNFV_MNT_DIR}"
+    # AArch64 CentOS cloud image has root partition at index 4 instead of 1
+    sudo mount "${OPNFV_MAP_DEV/p1/p4}" "${OPNFV_MNT_DIR}"
+  else
+    # grub-update does not like /dev/nbd*, so use a loop device to work around it
+    sudo losetup "${OPNFV_LOOP_DEV}" "${OPNFV_MAP_DEV}"
+    sudo mount "${OPNFV_LOOP_DEV}" "${OPNFV_MNT_DIR}"
+  fi
   sudo mount -t proc proc "${OPNFV_MNT_DIR}/proc"
   sudo mount -t sysfs sys "${OPNFV_MNT_DIR}/sys"
   sudo mount -o bind /dev "${OPNFV_MNT_DIR}/dev"
-  sudo mkdir -p "${OPNFV_MNT_DIR}/run/resolvconf"
-  sudo cp /etc/resolv.conf "${OPNFV_MNT_DIR}/run/resolvconf"
+  if [[ "${MCP_OS:-}" =~ ubuntu1804 ]]; then
+    # Ubuntu Bionic (18.04) or newer defaults to using netplan.io, revert it
+    sudo mkdir -p "${OPNFV_MNT_DIR}/run/systemd/resolve"
+    echo "nameserver ${dns_public}" | sudo tee \
+      "${OPNFV_MNT_DIR}/run/systemd/resolve/stub-resolv.conf"
+    sudo chroot "${OPNFV_MNT_DIR}" systemctl stop \
+      systemd-networkd.socket systemd-networkd \
+      networkd-dispatcher systemd-networkd-wait-online systemd-resolved
+    sudo chroot "${OPNFV_MNT_DIR}" systemctl disable \
+      systemd-networkd.socket systemd-networkd \
+      networkd-dispatcher systemd-networkd-wait-online systemd-resolved
+    sudo chroot "${OPNFV_MNT_DIR}" systemctl mask \
+      systemd-networkd.socket systemd-networkd \
+      networkd-dispatcher systemd-networkd-wait-online systemd-resolved
+    sudo chroot "${OPNFV_MNT_DIR}" apt --assume-yes purge nplan netplan.io
+    echo "source /etc/network/interfaces.d/*" | \
+      sudo tee "${OPNFV_MNT_DIR}/etc/network/interfaces"
+  elif [[ "${MCP_OS:-}" =~ centos ]]; then
+    sudo sed -i -e 's/^\(SELINUX\)=.*$/\1=permissive/g' \
+      "${OPNFV_MNT_DIR}/etc/selinux/config"
+  fi
+  sudo rm -f "${OPNFV_MNT_DIR}/etc/resolv.conf"
+  echo "nameserver ${dns_public}" | sudo tee \
+    "${OPNFV_MNT_DIR}/etc/resolv.conf"
   echo "GRUB_DISABLE_OS_PROBER=true" | \
     sudo tee -a "${OPNFV_MNT_DIR}/etc/default/grub"
   sudo sed -i -e 's/^\(GRUB_TIMEOUT\)=.*$/\1=1/g' -e 's/^GRUB_HIDDEN.*$//g' \
@@ -153,8 +192,10 @@ function __apt_repos_pkgs_image {
   local pkgs_r=(${4//,/ })
   [ -n "${OPNFV_MNT_DIR}" ] || exit 1
 
+  # NOTE: We don't support (yet) some features for non-APT repos: keys, prio
+
   # APT keys
-  if [ "${#apt_key_urls[@]}" -gt 0 ]; then
+  if [[ "${MCP_OS:-}" =~ ubuntu ]] && [ "${#apt_key_urls[@]}" -gt 0 ]; then
     for apt_key in "${apt_key_urls[@]}"; do
       sudo chroot "${OPNFV_MNT_DIR}" /bin/bash -c \
         "wget -qO - '${apt_key}' | apt-key add -"
@@ -164,6 +205,16 @@ function __apt_repos_pkgs_image {
   for repo_line in "${all_repos[@]}"; do
     # <repo_name>|<repo prio>|deb|[arch=<arch>]|<repo url>|<dist>|<repo comp>
     local repo=(${repo_line//|/ })
+
+    if [[ "${MCP_OS:-}" =~ centos ]]; then
+      cat <<-EOF | sudo tee "${OPNFV_MNT_DIR}/etc/yum.repos.d/${repo[0]}.repo"
+               [${repo[0]}]
+               baseurl=${repo[3]}
+               enabled=1
+               gpgcheck=0
+               EOF
+      continue
+    fi
     [ "${#repo[@]}" -gt 5 ] || continue
     # NOTE: Names and formatting are compatible with Salt linux.system.repo
     cat <<-EOF | sudo tee "${OPNFV_MNT_DIR}/etc/apt/preferences.d/${repo[0]}"
@@ -178,15 +229,23 @@ function __apt_repos_pkgs_image {
   done
   # Install packages
   if [ "${#pkgs_i[@]}" -gt 0 ]; then
-    sudo DEBIAN_FRONTEND="noninteractive" \
-      chroot "${OPNFV_MNT_DIR}" apt-get update
-    sudo DEBIAN_FRONTEND="noninteractive" FLASH_KERNEL_SKIP="true" \
-      chroot "${OPNFV_MNT_DIR}" apt-get install -y "${pkgs_i[@]}"
+    if [[ "${MCP_OS:-}" =~ ubuntu ]]; then
+      sudo DEBIAN_FRONTEND="noninteractive" \
+        chroot "${OPNFV_MNT_DIR}" apt-get update
+      sudo DEBIAN_FRONTEND="noninteractive" FLASH_KERNEL_SKIP="true" \
+        chroot "${OPNFV_MNT_DIR}" apt-get install -y "${pkgs_i[@]}"
+    else
+      sudo chroot "${OPNFV_MNT_DIR}" yum install -y "${pkgs_i[@]}"
+    fi
   fi
   # Remove packages
   if [ "${#pkgs_r[@]}" -gt 0 ]; then
-    sudo DEBIAN_FRONTEND="noninteractive" FLASH_KERNEL_SKIP="true" \
-      chroot "${OPNFV_MNT_DIR}" apt-get purge -y "${pkgs_r[@]}"
+    if [[ "${MCP_OS:-}" =~ ubuntu ]]; then
+      sudo DEBIAN_FRONTEND="noninteractive" FLASH_KERNEL_SKIP="true" \
+        chroot "${OPNFV_MNT_DIR}" apt-get purge -y "${pkgs_r[@]}"
+    else
+      sudo chroot "${OPNFV_MNT_DIR}" yum remove -y "${pkgs_r[@]}"
+    fi
   fi
   # Disable cloud-init metadata service datasource
   sudo mkdir -p "${OPNFV_MNT_DIR}/etc/cloud/cloud.cfg.d"
@@ -212,7 +271,8 @@ function __cleanup_vms {
 ##############################################################################
 
 function prepare_vms {
-  local base_image=$1; shift
+  local base_image_f=$1; shift
+  local base_image=${base_image_f%.xz}
   local image_dir=$1; shift
   local repos_pkgs_str=$1; shift # ^-sep list of repos, pkgs to install/rm
   local image=base_image_opnfv_fuel.img
@@ -222,7 +282,8 @@ function prepare_vms {
 
   cleanup_uefi
   __cleanup_vms
-  __get_base_image "${base_image}" "${image_dir}"
+  __get_base_image "${base_image_f}" "${image_dir}"
+  [ "${base_image}" == "${base_image_f}" ] || unxz -fk "${image_dir}/${_o}.xz"
   IFS='^' read -r -a repos_pkgs <<< "${repos_pkgs_str}"
 
   local _h=$(echo "${repos_pkgs_str}.$(md5sum "${image_dir}/${_o}")" | \
@@ -327,6 +388,27 @@ function create_networks {
   sudo systemctl restart opnfv-fuel-vetha
 }
 
+function cleanup_all {
+  local image_dir=$1; shift
+  local all_vnode_networks=("$@")
+  [ ! -e "${image_dir}/docker-compose" ] || COMPOSE_PREFIX="${image_dir}/"
+
+  cleanup_uefi
+  __cleanup_vms
+  sudo ip link del veth_mcp0 || true
+  sudo ip link del veth_mcp2 || true
+  for net in "mcpcontrol" "${all_vnode_networks[@]}"; do
+    if ${VIRSH} net-info "${net}" >/dev/null 2>&1; then
+      ${VIRSH} net-destroy "${net}" || true
+      ${VIRSH} net-undefine "${net}"
+    fi
+  done
+  sudo rm -f "/etc/systemd/system/multi-user.target.wants/opnfv-fuel"* \
+             "/etc/systemd/system/opnfv-fuel"*
+  sudo systemctl daemon-reload
+  "${COMPOSE_PREFIX}docker-compose" -f docker-compose/docker-compose.yaml down
+}
+
 function create_vms {
   local image_dir=$1; shift
   local image=base_image_opnfv_fuel.img
@@ -492,7 +574,6 @@ function cleanup_mounts {
       sudo sed -i -e 's/^\s*set root=.*$//g' -e 's/^\s*loopback.*$//g' \
         "${OPNFV_MNT_DIR}/boot/grub/grub.cfg"
     fi
-    sudo rm -f "${OPNFV_MNT_DIR}/run/resolvconf/resolv.conf"
     sync
     if mountpoint -q "${OPNFV_MNT_DIR}"; then
       sudo umount -l "${OPNFV_MNT_DIR}" || true