#!/bin/bash set -e c_super_file="$1" n_super_file="$(dirname "$c_super_file")/super.new.img" group_name="$2" c_system_file="$3" c_system_partion_name="$4" args="" super_block_device_size=0 blockdevice_info_dump=$(lpdump -j "$c_super_file" | jq -c -r '.block_devices[]') for item in $blockdevice_info_dump do if [[ "$(echo "$item" | jq -r '.name')" == "super" ]] then super_block_device_size="$(echo "$item" | jq -r '.size')" break fi done if [[ "$super_block_device_size" == "0" ]] then exit 1 fi group_maxinum_size=0 group_info_dump=$(lpdump -j "$c_super_file" | jq -c -r '.groups[]') for item in $group_info_dump do if [[ "$(echo "$item" | jq -r '.name')" == "$group_name" ]] then group_maxinum_size=$(echo "$item" | jq -r '.maximum_size') break fi done if [[ "$group_maxinum_size" == "0" ]] then exit 2 fi temp_super_dir="$(dirname "$c_super_file")/super/" total_size=0 partition_info_dump=$(lpdump -j "$c_super_file" | jq -c -r '.partitions[]') for item in $partition_info_dump do echo $item if [[ "$(echo "$item" | jq -r '.group_name')" == "$group_name" ]] then name=$(echo "$item" | jq -r '.name') size=$(echo "$item" | jq -r '.size') if [[ "$name" == "$c_system_partion_name" ]] then size="$(stat -c "%s" "$c_system_file")" fi args="$args --partition ${name}:readonly:${size}:${group_name} --image ${name}=${temp_super_dir}/${name}.img" total_size=$((total_size + size)) fi done if [[ -z "$args" ]] then exit 3 fi mkdir -p "$temp_super_dir" lpunpack "$c_super_file" "$temp_super_dir" mv "$c_system_file" "$temp_super_dir" if [[ "$group_maxinum_size" -lt "$total_size" ]] then if [[ "$super_block_device_size" -lt "$total_size" ]] then space_needed=$((total_size-group_maxinum_size)) super_block_device_size=$((super_block_device_size + space_needed)) fi group_maxinum_size="$total_size" fi lpmake \ --metadata-size 65536 \ --super-name super \ --metadata-slots 2 \ --device super:"$super_block_device_size" \ --group "$group_name":"$group_maxinum_size" \ --sparse \ --output "$n_super_file" \ $args rm -rf "$temp_super_dir"