repack.sh 2.19 KB
Newer Older
fuyanbin's avatar
fuyanbin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/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"