weizhouapache commented on code in PR #14493:
URL: https://github.com/apache/cloudstack/pull/14493#discussion_r4512242456


##########
scripts/util/build-acs-image.sh:
##########
@@ -0,0 +1,659 @@
+#!/bin/bash
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Prepare a generic cloud qcow2 image for CloudStack: patch it, shrink it and 
convert it to VHD/OVA.
+#
+# Usage: build-acs-image.sh [-a ACTIONS] [-f FORMATS] [-o OUTDIR] [-g GUESTOS] 
[-u] [-z]
+#                           [-d debian|rhel|suse] [-p ROOT_PASSWORD] 
<image.qcow2>
+#   -a  actions, a list separated by commas or spaces, done in this order 
(default: all)
+#         patch    install the guest tools (including xe-guest-utilities for 
XenServer on Debian/Ubuntu
+#                  and EL), set the root password, cloud-init and sshd config
+#                  (OS detected from the image: Debian/Ubuntu, EL, SUSE and 
variants)
+#         shrink   virt-sparsify and qemu-img convert -c
+#         convert  convert to the formats of -f
+#         all      patch, shrink and convert
+#   -f  formats for the convert action, a list like "vhd,ova" or "ova, xen" 
(default: all)
+#         vhd  plain VHD for Hyper-V (also hyperv)           <name>-acs.vhd 
(removed if xen is built too)
+#         ova  OVA for VMware (also vmware)                  <name>-acs.ova
+#         xen  VHD for XenServer, bzip2 (also xenserver)     
<name>-acs-xen.vhd.bz2
+#         all  vhd, ova and xen
+#   -o  output directory (default: directory of the image)
+#   -g  VMware guestos id for the OVA (default: detected from /etc/os-release 
in the image,
+#       e.g. debian12-64, ubuntu-64, rhel9-64, sles15-64, otherlinux-64)
+#   -u  with patch: also run a full package upgrade inside the image
+#   -z  with shrink: do not compress (plain qcow2 rewrite)
+#   -d  with patch: OS family, if it cannot be detected from the image
+#   -p  with patch: root password to set (default: password)
+#
+# The image is never modified. Patch and shrink work on a copy named 
<name>-acs.<ext> in the
+# output directory (the name is kept if it already ends with -acs), and the 
converted files
+# are named <name>-acs.vhd and <name>-acs.ova. Convert alone reads the 
original image.
+#
+# SHA512SUMS with the checksums of the output files is written to the output 
directory when all
+# is done (the entries of other files are kept). The tools needed by the 
actions and formats are
+# checked first, and nothing is installed.
+set -euo pipefail
+
+usage() { sed -n '19,46p' "$0"; exit 1; }
+
+ACTIONS="all"
+FORMATS="all"
+OUTDIR=""
+GUESTOS=""
+UPDATE=""
+COMPRESS="-c"
+OS_FAMILY=""
+ROOT_PASSWORD="password"
+while getopts "a:f:o:g:uzd:p:" opt; do
+  case $opt in
+    a) ACTIONS="$OPTARG" ;;
+    f) FORMATS="$OPTARG" ;;
+    o) OUTDIR="$OPTARG" ;;
+    g) GUESTOS="$OPTARG" ;;
+    u) UPDATE="--update" ;;
+    z) COMPRESS="" ;;
+    d) OS_FAMILY="$OPTARG" ;;
+    p) ROOT_PASSWORD="$OPTARG" ;;
+    *) usage ;;
+  esac
+done
+shift $((OPTIND - 1))
+
+IMAGE="${1:-}"
+[ -f "$IMAGE" ] || { echo "Image not found: '$IMAGE'" >&2; usage; }
+case "$OS_FAMILY" in ""|debian|rhel|suse) ;; *) echo "Invalid OS family: 
$OS_FAMILY" >&2; exit 1 ;; esac
+
+# ACTIONS and FORMATS are lists separated by commas and/or spaces
+DO_PATCH="" DO_SHRINK="" DO_CONVERT=""
+for a in $(echo "$ACTIONS" | tr 'A-Z,;' 'a-z  '); do
+  case "$a" in
+    patch) DO_PATCH=1 ;;
+    shrink) DO_SHRINK=1 ;;
+    convert) DO_CONVERT=1 ;;
+    all) DO_PATCH=1; DO_SHRINK=1; DO_CONVERT=1 ;;
+    *) echo "Invalid action: '$a' (supported: patch, shrink, convert, all)" 
>&2; exit 1 ;;
+  esac
+done
+[ -n "$DO_PATCH$DO_SHRINK$DO_CONVERT" ] || { echo "No action given" >&2; exit 
1; }
+
+WANT_VHD="" WANT_OVA="" WANT_XEN=""
+for f in $(echo "$FORMATS" | tr 'A-Z,;' 'a-z  '); do
+  case "$f" in
+    vhd|hyperv) WANT_VHD=1 ;;
+    ova|vmware) WANT_OVA=1 ;;
+    xen|xenserver) WANT_XEN=1 ;;
+    all) WANT_VHD=1; WANT_OVA=1; WANT_XEN=1 ;;
+    *) echo "Invalid format: '$f' (supported: vhd, ova, xen, all, or a list 
like vhd,ova)" >&2; exit 1 ;;
+  esac
+done
+[ -n "$WANT_VHD$WANT_OVA$WANT_XEN" ] || { echo "No output format given" >&2; 
exit 1; }
+# the formats are only needed by the convert action
+if [ -z "$DO_CONVERT" ]; then WANT_VHD=""; WANT_OVA=""; WANT_XEN=""; fi
+
+# names: <name>-acs.<ext> for the copy that is patched and shrunk, 
<name>-acs.<format> for the conversions
+IMAGE=$(readlink -f "$IMAGE")
+[ -n "$OUTDIR" ] || OUTDIR=$(dirname "$IMAGE")
+mkdir -p "$OUTDIR"
+OUTDIR=$(readlink -f "$OUTDIR")
+file=$(basename "$IMAGE")
+if [[ "$file" == *.* ]]; then stem="${file%.*}"; ext=".${file##*.}"; else 
stem="$file"; ext=""; fi
+case "$stem" in *-acs) OUT_BASE="$stem" ;; *) OUT_BASE="${stem}-acs" ;; esac
+WORK_IMAGE="$OUTDIR/$OUT_BASE$ext"
+# patch and shrink work on the copy, convert alone reads the original image
+SOURCE="$IMAGE"
+if [ -n "$DO_PATCH$DO_SHRINK" ]; then SOURCE="$WORK_IMAGE"; fi
+
+export LIBGUESTFS_BACKEND=direct
+
+# Hint on the package names for the missing tools, according to the package 
manager of the host
+pkg_hint() {
+  local what="$1"
+  if command -v apt-get >/dev/null 2>&1; then
+    [ "$what" = "libguestfs" ] && echo "apt install libguestfs-tools 
qemu-utils" || echo "apt install $what"
+  elif command -v zypper >/dev/null 2>&1; then
+    [ "$what" = "libguestfs" ] && echo "zypper install guestfs-tools 
libguestfs qemu-tools" || echo "zypper install $what"
+  elif command -v dnf >/dev/null 2>&1 || command -v yum >/dev/null 2>&1; then
+    [ "$what" = "libguestfs" ] && echo "dnf install guestfs-tools 
(libguestfs-tools-c on EL8) qemu-img" || echo "dnf install $what"
+  else
+    echo "install libguestfs tools and qemu-img"
+  fi
+}
+
+# Check what the actions and formats need, before anything is created, and 
report all the
+# problems at once. Packages are never installed here, as the script runs on 
Debian/Ubuntu,
+# EL, SUSE, ...
+check_dependencies() {
+  local missing=() warnings=()
+  local cmd
+
+  for cmd in qemu-img cp mktemp sha512sum; do
+    command -v "$cmd" >/dev/null 2>&1 || missing+=("$cmd not found ($(pkg_hint 
libguestfs))")
+  done
+  if [ -n "$DO_PATCH" ]; then
+    for cmd in virt-customize virt-cat virt-ls virt-inspector; do
+      command -v "$cmd" >/dev/null 2>&1 || missing+=("$cmd not found 
($(pkg_hint libguestfs))")
+    done
+    command -v curl >/dev/null 2>&1 || missing+=("curl not found, needed to 
download xe-guest-utilities ($(pkg_hint curl))")
+  fi
+  if [ -n "$DO_SHRINK" ]; then
+    command -v virt-sparsify >/dev/null 2>&1 || missing+=("virt-sparsify not 
found ($(pkg_hint libguestfs))")
+  fi
+
+  # the formats needed by qemu-img
+  if command -v qemu-img >/dev/null 2>&1 && [ -n "$DO_CONVERT" ]; then
+    local formats
+    formats=$(qemu-img --help 2>&1 || true)
+    if [ -n "$WANT_VHD" ]; then
+      echo "$formats" | grep -qw vpc || missing+=("qemu-img does not support 
the vpc (VHD) format")
+    fi
+    if [ -n "$WANT_OVA" ]; then
+      echo "$formats" | grep -qw vmdk || missing+=("qemu-img does not support 
the vmdk format")
+    fi
+  fi
+
+  # the VMware guestos of the OVA is detected from the image, unless -g is 
given
+  if [ -n "$WANT_OVA" ] && [ -z "$GUESTOS" ] && [ -z "$DO_PATCH" ]; then
+    command -v virt-cat >/dev/null 2>&1 || missing+=("virt-cat not found, 
needed to detect the VMware guestos of the OVA from the image ($(pkg_hint 
libguestfs)), or use -g")
+  fi
+
+  # ovftool must be runnable, not only be present
+  if [ -n "$WANT_OVA" ]; then
+    if ! command -v ovftool >/dev/null 2>&1; then
+      missing+=("ovftool not found, required for OVA (VMware OVF Tool, add it 
to PATH)")
+    elif ! ovftool --version >/dev/null 2>&1; then
+      missing+=("ovftool cannot be run (check its libraries and that the whole 
ovftool directory is installed)")
+    fi
+  fi
+
+  # the XenServer VHD: bzip2, and the 32-bit vhd-util with faketime (as in 
tools/appliance/build.sh)
+  if [ -n "$WANT_XEN" ]; then
+    command -v bzip2 >/dev/null 2>&1 || missing+=("bzip2 not found, required 
for the XenServer VHD (install bzip2)")
+    command -v faketime >/dev/null 2>&1 || missing+=("faketime not found, 
required for the XenServer VHD (install faketime)")
+    if ! command -v vhd-util >/dev/null 2>&1; then
+      missing+=("vhd-util not found, required for the XenServer VHD (see the 
comments in tools/appliance/build.sh, or 
http://packages.shapeblue.com/systemvmtemplate/vhd-util)")

Review Comment:
   @wido 
   binaries have been uploaded to 
https://download.cloudstack.org/tools/vhd-util.x86_64/
   script has been updated



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to