From patchwork Thu May 14 09:19:45 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kasturi shekar X-Patchwork-Id: 5076 Return-Path: Received: from shymkent.ilbers.de ([unix socket]) by shymkent (Cyrus 2.5.10-Debian-2.5.10-3+deb9u2) with LMTPA; Thu, 14 May 2026 11:19:59 +0200 X-Sieve: CMU Sieve 2.4 Received: from mail-lj1-f185.google.com (mail-lj1-f185.google.com [209.85.208.185]) by shymkent.ilbers.de (8.15.2/8.15.2/Debian-8+deb9u1) with ESMTPS id 64E9JmH8025745 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 14 May 2026 11:19:49 +0200 Received: by mail-lj1-f185.google.com with SMTP id 38308e7fff4ca-393d5b5ae63sf36563951fa.3 for ; Thu, 14 May 2026 02:19:49 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1778750383; cv=pass; d=google.com; s=arc-20240605; b=k9XBV4qKoWncrljFMHoga+ipBHkW+hZ0onrpV0n0wHX0b0DMuZ4sipEau6PYCf0cN1 FRzK1wUZem93KeSNPyjWh/OphE6kYZYhTDCMUFI81iCPeQkO3b9Fu7NYm0XNnFLz4LSv /1lKVzb3EX4heCyq4V2rNxOyMsW/01fh4dPosCBze27sHPqGfNg/9UwEclNjR6gfaeeF kXXJN+G9P2nlkEYImS9MsWGNCBQxQs+pQrNwq0D8+O6gf4Xd5s1rcKDcDUyQK/cSGQg1 6PSVkXW86auR1PxAxwxS24LWvqqXIFKJuwiHdNAQ3RMUzfCxg0mtl1pzOkEx7h48mhkj 5d+g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:reply-to:feedback-id :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=TMDb1Dqkn4e7M/U7/rdJKNirdU6k1RqGxK2CueS8q58=; fh=h6ZjjaDsLDsq/1gb6hzdQ/x2ucn+9QK66vwwUjoLGzM=; b=MAgkXyP2m90MmbD5LIOyrh8LCl/r8XBW6G9hr6SD+XB0I9EgTsIOeg4mOD6v62spAR pJaJHWmSNCIBP5E+YS0v1ScB0KBg4gy8MT+BD5h9vKWDFeq3D5H1U6E0JjmnGJVMCK0R KwXpGWi3TSW5UTJ0n/4bzW2e/qCkPWdub6srC1aRKx4WwUaSXZpc4NNzTf+k1BiMT6ZA B8PYUrOgvdqqiB1hYjRvV+o3mlowrDSMr9Bggt0Je4XuahaEFIMlI2ZpnfeNhKsanm4w 5IkqnIBe0utk+X1xu1hiFVT+bkMyGf4iEvHRijDmd5kK4Q6ncIgP8zIW9k9c1VooEQ7z 6lyg==; darn=isar-build.org ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@siemens.com header.s=fm1 header.b=jE+ew2Pm; spf=pass (google.com: domain of fm-1328757-2026051409193966f34eb052000207e7-ii7e0g@rts-flowmailer.siemens.com designates 185.136.64.226 as permitted sender) smtp.mailfrom=fm-1328757-2026051409193966f34eb052000207e7-ii7E0G@rts-flowmailer.siemens.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=siemens.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20251104; t=1778750383; x=1779355183; darn=isar-build.org; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:reply-to :x-original-authentication-results:x-original-sender:feedback-id :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TMDb1Dqkn4e7M/U7/rdJKNirdU6k1RqGxK2CueS8q58=; b=JGRCCs71ewjAJVn7gW08ovwr/QWHDlgX3BY8VVWbkj2p6god/b52ysFLJkhJGc1gjr AOewmZ2fzc9DVg5RpcxoJxzHqG+6c1CdVVR4KIC92U/ShORS3Eo/rcRC9ncZCqZfmaQS PoXodzAgXs9R6URx8r5XQFzPzl+LhU8BzSPqNFSVpK7RpTF8vO3dAPbA62t5AJdeh/6P B3s+2heT/BZhvnlDRiAl7Q0JJWCFhFIqeLQmcJYtsAMZuE/UHt8BbfYfDaCvGON4A6cb Dv5Eu8f98tNd0xJHverKLhDWGMjyFNBjInNuVuOKoOcrRf8sPqqNDuIJUn54I5AnHAWf XTZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778750383; x=1779355183; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence:reply-to :x-original-authentication-results:x-original-sender:feedback-id :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-beenthere:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=TMDb1Dqkn4e7M/U7/rdJKNirdU6k1RqGxK2CueS8q58=; b=cR+pzEkv/h+nZWeCZSnOziaVct4nSssXoBH8V9KFZs5AOimQsQ/4ps1FdQxq2lBvOv GEevdidziGgEtlkpFgDnnmXrfINl1dA1mgSHMgAa58+GN9dAmMXRjCGQUFNglxSB7d4i mDBU5M0Mqe+aoE62UALZTNL5E6WaYdgGOTMFwHOPErnie0iDfPLg0VZVQP++drjnr0rS 76yXx8SpSVcNZt6r88D/JteoIAr/Ooe7rLkYUenlh/21rzYg9IQ4bjEI3hakqTfpqkyr slmbkzV7gge+FyBOzgVpX6A2LjY5ZChruxfcANNiknJw5Lj8lYHP2cfI+ABoNmqS9r71 gPJA== X-Forwarded-Encrypted: i=2; AFNElJ9vkJRfqQGc2jW6E444p1duwjUBfpkcCIuYkq9jY2BK8VBzk1ezLKud5Nty/PtxIE5aEu9hTcs=@isar-build.org X-Gm-Message-State: AOJu0YyB+BU34naQhVWG0foNT5S9AV6pJUxQymrs3qmBpR1+A3h2R6uR i9RYg9YES9wCXwVwciunpqhgurYFT/wnRjvtWBJkVBGnpSrf7YIZHmIV X-Received: by 2002:a05:6512:118a:b0:5a3:6731:910a with SMTP id 2adb3069b0e04-5a8ef979a7emr2370662e87.11.1778750382894; Thu, 14 May 2026 02:19:42 -0700 (PDT) X-BeenThere: isar-users@googlegroups.com; h="AUV6zMN08Pt/s5SLYjk2oc1XJINI6MzCYp4q6e+BYMeZFzNELQ==" Received: by 2002:a2e:8245:0:b0:393:8bf7:a638 with SMTP id 38308e7fff4ca-3948c25d2fals3856131fa.0.-pod-prod-03-eu; Thu, 14 May 2026 02:19:40 -0700 (PDT) X-Received: by 2002:a05:6512:138a:b0:5a8:9f6f:4647 with SMTP id 2adb3069b0e04-5a8ef9b61e3mr2340541e87.35.1778750380183; Thu, 14 May 2026 02:19:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1778750380; cv=none; d=google.com; s=arc-20240605; b=P8+L6BAu4gVPBCn9hT2RCE7qBqtYEkcIVKjj78Nj08fBMmPqP2OjM88LgviFGqEIYy Xgn9gXb16CZZSJC4e94ZV6MyiAO/lH+uis45W8iCIgRwo4+9Yh26PR9PBHU3PeyruSuO kr+P7FOtob23m7LXV6AHDZv8Ruvxc48ff8uSmgIzE/7kepBuoI7b63oNWGv101dgtHM2 FEpH9Lq9LgHdiWl23jiCcuVoPEC3C/V+Ygxo7sPeiu1ld3rvpWdInmAzrnrRz7zGWRrP hHRF57d9TGNwhbkqSSpH/f5aUvjAygy5WTNZKTG15RV7sc5/RZBAykmHtgDliibUWyW0 E+6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=feedback-id:content-transfer-encoding:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=YsWzSu2h1OdZL9+OcLVg2KAEYfVdG5HEHGFTa5qx/vI=; fh=pR4tJPO4NbHlIB/rP4bIo54Z1zh5hd/ksAGQ0tcKkYM=; b=FWdYSHpuWBeqn56uVqKN/zt403nEVaY9eXGfjgvVZm21da9TOwhEUEj/+U42YjNlaj ksTGIlSrguIohPUdKXhlaMehJirdDhN1s8HTiB6Mi8X5TGwnHU8stK5s8ezX6940o31D COF682hqy4H4qxZe3lEvbyyiIpX+ai/DZ2bQ00ln8htKj/nCxkgOw7YtOL9vqokbUmG7 28FVom0TwxWBvNx9QVMNc+OqA4p4M9mtyJpsbsopJ5OOzQQeOrMLyuoRDMZeVWy5c0pe x2ygJeLBmC2j8RIVuxmzcL/6YtL4Xs2GqLU+1DIbHVTD8v0LrAl6DyElGZ1tY/XqRNaD QkWQ==; dara=google.com ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@siemens.com header.s=fm1 header.b=jE+ew2Pm; spf=pass (google.com: domain of fm-1328757-2026051409193966f34eb052000207e7-ii7e0g@rts-flowmailer.siemens.com designates 185.136.64.226 as permitted sender) smtp.mailfrom=fm-1328757-2026051409193966f34eb052000207e7-ii7E0G@rts-flowmailer.siemens.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=siemens.com Received: from mta-64-226.siemens.flowmailer.net (mta-64-226.siemens.flowmailer.net. [185.136.64.226]) by gmr-mx.google.com with ESMTPS id 38308e7fff4ca-3945c594b83si388231fa.0.2026.05.14.02.19.39 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 May 2026 02:19:40 -0700 (PDT) Received-SPF: pass (google.com: domain of fm-1328757-2026051409193966f34eb052000207e7-ii7e0g@rts-flowmailer.siemens.com designates 185.136.64.226 as permitted sender) client-ip=185.136.64.226; Received: by mta-64-226.siemens.flowmailer.net with ESMTPSA id 2026051409193966f34eb052000207e7 for ; Thu, 14 May 2026 11:19:39 +0200 X-Patchwork-Original-From: "'Kasturi shekar' via isar-users" From: Kasturi shekar To: isar-users@googlegroups.com Cc: Kasturi Shekar Subject: [PATCH v2 1/4] installer: extract backend helpers to sys_api.sh Date: Thu, 14 May 2026 14:49:45 +0530 Message-ID: <20260514091951.1572682-2-kasturi.shekar@siemens.com> In-Reply-To: <20260514091951.1572682-1-kasturi.shekar@siemens.com> References: <20260427063957.603123-4-kasturi.shekar@siemens.com> <20260514091951.1572682-1-kasturi.shekar@siemens.com> MIME-Version: 1.0 X-Flowmailer-Platform: Siemens Feedback-ID: 519:519-1328757:519-21489:flowmailer X-Original-Sender: kasturi.shekar@siemens.com X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@siemens.com header.s=fm1 header.b=jE+ew2Pm; spf=pass (google.com: domain of fm-1328757-2026051409193966f34eb052000207e7-ii7e0g@rts-flowmailer.siemens.com designates 185.136.64.226 as permitted sender) smtp.mailfrom=fm-1328757-2026051409193966f34eb052000207e7-ii7E0G@rts-flowmailer.siemens.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=siemens.com X-Original-From: Kasturi shekar Reply-To: Kasturi shekar Precedence: list Mailing-list: list isar-users@googlegroups.com; contact isar-users+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: isar-users@googlegroups.com X-Google-Group-Id: 914930254986 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , X-Spam-Status: No, score=-4.9 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,RCVD_IN_RP_CERTIFIED, RCVD_IN_RP_RNBL,RCVD_IN_RP_SAFE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on shymkent.ilbers.de X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= From: Kasturi Shekar Move backend and system-facing installer logic into sys_api.sh This patch introduces backend helper APIs for: - install image discovery - target device discovery and filtering - root/live-device exclusion - RAID member and inactive md filtering - block device state and size helpers - bmap copy execution with locking and progress pipe support Signed-off-by: Kasturi Shekar --- .../deploy-image/files/usr/bin/sys_api.sh | 400 +++++++++++++++--- 1 file changed, 333 insertions(+), 67 deletions(-) diff --git a/meta-isar/recipes-installer/deploy-image/files/usr/bin/sys_api.sh b/meta-isar/recipes-installer/deploy-image/files/usr/bin/sys_api.sh index b144419e..9c9c5840 100644 --- a/meta-isar/recipes-installer/deploy-image/files/usr/bin/sys_api.sh +++ b/meta-isar/recipes-installer/deploy-image/files/usr/bin/sys_api.sh @@ -1,84 +1,350 @@ #!/usr/bin/env bash +# This software is a part of ISAR. +# Copyright (C) Siemens AG, 2026 # -# sys_api.sh — Ported installer system APIs -# ------------------------------------------------------------------- +# SPDX-License-Identifier: MIT -# Load shared framework utilities -source ./func.sh || { - echo "Error: func.sh not found or not readable." >&2 - exit 1 +#-------------------------------------------------------------------------- +# sys_api.sh - Backend APIs for deploy-image-wic installer. +# +# This file intentionally contains only system/backend logic: +# - image discovery +# - target device discovery and filtering +# - low-level install/copy flow +# +# UI concerns (dialog boxes, menus, confirmations) are implemented in +# installer_ui.sh and called by the main orchestrator script. +#-------------------------------------------------------------------------- + +# Globals populated by sys_discover_target_devices(). +SYS_CURRENT_ROOT_DEV="" +SYS_TARGET_DEVICES=() + +#-------------------------------------------------------------------------- +# sys_first_default_image_name +# +# Returns: +# basename of matching *.wic* image (excluding *.wic.bmap), or empty. +#-------------------------------------------------------------------------- +sys_first_default_image_name() { + local install_data_dir="$1" + + find "$install_data_dir" -type f -iname "*.wic*" -a -not -iname "*.wic.bmap" \ + -exec basename {} \; | head -n 1 +} + +#-------------------------------------------------------------------------- +# sys_list_installable_entries +# +# Returns: +# relative file paths under install_data_dir, one per line. +# Files ending in *.wic.bmap are excluded. +#-------------------------------------------------------------------------- +sys_list_installable_entries() { + local install_data_dir="$1" + + ( + cd "$install_data_dir" || return 1 + find . -type f ! -iname "*.wic.bmap" -print | sed 's#^./##' + ) } -# ------------------------------------------------------------------- -# API: locate_disk_images -# Description: -# Find disk image files (.wic, .wic.bz2) under given path. -# Usage: -# sys_locate_disk_images search_path=/install -# Returns JSON: -# {"error":"", "retval":"0", "images":["/install/image.wic", ...]} -# ------------------------------------------------------------------- -sys_locate_disk_images() { - local -A ARGS - local required=(search_path) - api_args ARGS required[@] "$@" || { - pack_return_data error "$_args_error" retval "1" - return 1 - } - - local fn="${FUNCNAME[0]}" - local path="${ARGS[search_path]}" - - log_info "$fn" "Searching for disk images in '$path'" - - local images - images=$(find "$path" -type f -iname "*.wic*" ! -iname "*.wic.bmap" 2>/dev/null) - - # when no images found in /install - if [[ -z "$images" ]]; then - log_warn "$fn" "No images found." - pack_return_data error "No images found" retval "1" - return 1 +#-------------------------------------------------------------------------- +# sys_resolve_image_path +# +# Arguments: +# install_data_dir - base directory where installable images are stored +# image_or_path - either a relative image filename under install_data_dir +# or an explicit absolute/relative filesystem path +# +# Returns: +# absolute image path if resolvable, else non-zero return code. +#-------------------------------------------------------------------------- +sys_resolve_image_path() { + local install_data_dir="$1" + local image_or_path="$2" + + if [ -f "$image_or_path" ]; then + echo "$image_or_path" + return 0 + fi + + if [ -f "$install_data_dir/$image_or_path" ]; then + echo "$install_data_dir/$image_or_path" + return 0 fi - # Convert newline-separated paths into JSON array elements - local json_images - json_images=$(printf '"%s",' $images | sed 's/,$//') - # final JSON response on stdout - echo "{ \"error\":\"\", \"retval\":\"0\", \"images\":[${json_images}] }" + return 1 } +#-------------------------------------------------------------------------- +# sys_discover_target_devices +# +# Populates globals: +# SYS_CURRENT_ROOT_DEV - current live root device identifier +# SYS_TARGET_DEVICES - array with valid target block devices (/dev/*) +# +# Filtering rules match legacy deploy-image-wic behavior, including: +# - skip installer/live root device and backing devices +# - skip loop/dm/ram/sr/mtd/zram +# - skip inactive md arrays and md members +#-------------------------------------------------------------------------- +sys_discover_target_devices() { + local current_root_dev_type + local current_root_dev + local root_source + local root_source_resolved + local base_no_part + local slave + local slave_dev + local slave_base + local devices + local mmc_devices + local device + local is_raid_member + local holder_path + local holder_name + local state + local skip_device + local ex + + local exclude_list=() + + SYS_TARGET_DEVICES=() + + # Determine the live root device so we do not offer the current system disk + # as an install target. For NFS roots use a special sentinel value. + + current_root_dev_type=$(findmnt / -o fstype -n) + if [ "$current_root_dev_type" = "nfs" ]; then + current_root_dev="nfs" + exclude_list+=("nfs") + else + root_source=$(findmnt / -o source -n) + root_source_resolved=$(readlink -f "$root_source" 2>/dev/null || echo "$root_source") + current_root_dev=${root_source_resolved#/dev/} + + exclude_list+=("$current_root_dev") + + # Strip partition suffix for common disk names so the base device + # (e.g. /dev/sda or /dev/mmcblk0) also does not become selectable. + if [[ "$current_root_dev" =~ ^(mmcblk|nvme) ]]; then + base_no_part="${current_root_dev%p[0-9]*}" + else + base_no_part="${current_root_dev%%[0-9]*}" + fi + + if [ -n "$base_no_part" ]; then + exclude_list+=("$base_no_part") + fi + + # Exclude backing devices for the live root, such as multipath or RAID + # underlying devices exposed via /sys/block//slaves. + if [ -d "/sys/block/$current_root_dev/slaves" ]; then + for slave in /sys/block/"$current_root_dev"/slaves/*; do + [ -e "$slave" ] || continue + slave_dev=$(basename "$slave") + exclude_list+=("$slave_dev") + slave_base=${slave_dev%%[0-9]*} + [ -n "$slave_base" ] && exclude_list+=("$slave_base") + done + fi + fi + + SYS_CURRENT_ROOT_DEV="$current_root_dev" + + # Gather block devices from /sys/block while preserving mmcblk devices that + # would otherwise be filtered by the main find expression. + devices=$(find /sys/block/ -type b,c,f,l -not -iname "mmcblk*" -printf "%f\n") || true + mmc_devices=$(find /sys/block/ -type b,c,f,l -iname "mmcblk[0-9]" -printf "%f\n") || true + devices="$devices $mmc_devices" + + for device in $devices; do + is_raid_member=0 + + if [ -d "/sys/block/$device/holders" ] && [ ! -d "/sys/block/$device/md" ]; then + for holder_path in /sys/block/$device/holders/*; do + holder_name=$(basename "$holder_path") + case "$holder_name" in + md[0-9]*) + is_raid_member=1 + break + ;; + esac + done + fi + + if [ "$is_raid_member" -eq 1 ]; then + # Skip RAID member devices, because we only want whole block devices + # available for standalone image installation. + continue + fi + + if [[ "$device" == md* ]]; then + # Accept only active or clean md arrays, reject degraded/inactive RAID sets. + if [ -f "/sys/block/$device/md/array_state" ]; then + state=$(cat /sys/block/$device/md/array_state | tr -d '\n' | tr -d ' ') + if [ "$state" != "active" ] && [ "$state" != "clean" ]; then + echo "Skipping RAID device $device: state='$state'" >&2 + continue + fi + echo "Found RAID device $device: state='$state'" >&2 + else + echo "Skipping RAID device $device: no array_state file" >&2 + continue + fi + fi -# ------------------------------------------------------------------- -# API: sys_list_valid_target_devices -# Description: -# Shell-friendly variant of sys_get_valid_target_devices. -# Prints one valid device per line (absolute path). -# Usage: -# sys_list_valid_target_devices -# Output: -# /dev/sdb -# /dev/sdc -# ------------------------------------------------------------------- -sys_list_valid_target_devices() { - local dev - - for dev in /sys/block/*; do - dev=$(basename "$dev") - - case "$dev" in - loop*|ram*|sr*|mtd*) + case "$device" in + dm-*|loop*|mtd*|sr*|ram*|zram*) continue ;; esac - # skip inactive md devices - if [[ "$dev" == md* ]]; then - [ -f "/sys/block/$dev/md/array_state" ] || continue - state=$(cat /sys/block/$dev/md/array_state) - [ "$state" = "active" ] || [ "$state" = "clean" ] || continue - fi + skip_device=0 + for ex in "${exclude_list[@]}"; do + if [[ "$device" == "$ex"* ]]; then + skip_device=1 + break + fi + done - echo "/dev/$dev" + if [ "$skip_device" -eq 0 ]; then + SYS_TARGET_DEVICES+=("/dev/$device") + fi done } + +#-------------------------------------------------------------------------- +# sys_device_size +# +# Returns: +# human-readable size string from lsblk, or empty if unavailable. +#-------------------------------------------------------------------------- +sys_device_size() { + local device="$1" + lsblk --nodeps --noheadings -o SIZE "$device" 2>/dev/null | tr -d " " +} + +#-------------------------------------------------------------------------- +# sys_device_is_empty +# +# Returns: +# 0 when first 1 MiB is zero-filled, 1 otherwise. +#-------------------------------------------------------------------------- +sys_device_is_empty() { + local device="$1" + cmp /dev/zero "$device" -n 1M >/dev/null 2>&1 +} + +#-------------------------------------------------------------------------- +# sys_version_ge +# +# Returns: +# 0 if current >= required, else 1. +#-------------------------------------------------------------------------- +sys_version_ge() { + local current="$1" + local required="$2" + + if [ "$(printf '%s\n' "$current"X "$required" | sort -V | head -n 1)" != "$current"X ]; then + return 0 + fi + return 1 +} + +#-------------------------------------------------------------------------- +# sys_bmap_options_for_image +# +# Returns: +# bmaptool options string ("--nobmap" when no sidecar bmap is present). +#-------------------------------------------------------------------------- +sys_bmap_options_for_image() { + local image_path="$1" + local disk_bmap + + disk_bmap="${image_path%.wic*}.wic.bmap" + if [ -f "$disk_bmap" ]; then + # If a .wic.bmap sidecar exists, return an empty option string so + # bmaptool uses the bundled map automatically. + echo "" + else + echo "--nobmap" + fi +} + +#-------------------------------------------------------------------------- +# sys_install_image_with_lock +# +# Performs the actual bmaptool copy, serializing concurrent installer +# consoles via flock. When UI hook functions are available, this API +# uses them to present a progress gauge in attended mode. +# +# Optional UI hooks (if defined): +# ui_start_progress_gauge +# ui_stop_progress_gauge +#-------------------------------------------------------------------------- +sys_install_image_with_lock() { + local image_path="$1" + local target_device="$2" + local lockfile="/run/installer.lock" + local progress_pipe="/run/installer.fifo" + local bmap_options + local bmap_version + local quiet_flag="" + + bmap_options=$(sys_bmap_options_for_image "$image_path") + + exec 9>"$lockfile" + if flock -n 9; then + bmap_version=$(bmaptool --version | awk '{ print $NF }') + + if sys_version_ge "$bmap_version" "3.6"; then + if [ -p "$progress_pipe" ]; then + rm -f "$progress_pipe" + fi + + if ! mkfifo "$progress_pipe"; then + echo "Error: Failed to create named pipe $progress_pipe" + return 1 + fi + + bmap_options="$bmap_options --psplash-pipe=$progress_pipe" + quiet_flag="-q" + + if declare -F ui_start_progress_gauge >/dev/null; then + ui_start_progress_gauge "$progress_pipe" + fi + fi + + # Run the actual copying step under the lock to avoid concurrent writes + # from multiple installer consoles or sessions. + if ! bmaptool $quiet_flag copy $bmap_options "$image_path" "$target_device"; then + if declare -F ui_stop_progress_gauge >/dev/null; then + ui_stop_progress_gauge + fi + return 1 + fi + + if declare -F ui_stop_progress_gauge >/dev/null; then + ui_stop_progress_gauge + fi + else + echo "Installation already running in another console." + sleep 5 + + if [ -e "$progress_pipe" ]; then + echo "Installation progress..." + tail -f "$progress_pipe" | while read -r line; do + printf "\r%s%%" "$line" + done + else + echo "Waiting for installation to finish..." + while pgrep -x "bmaptool" >/dev/null; do + sleep 5 + done + fi + fi + + return 0 +}