pack-ci.sh 9.13 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
#!/bin/bash
usage() {
    echo "Usage: $0 --build-directory [build directory, default: /build]
                    --vendor <vendor>
                    --orgcode <organization code>
                    --channel-id <channel id>
                    --package-type [packtype: owner or other; default: owner]
                    --project-id <project id>
                    --pipeline-id <pipeline id>
                    --url-tag <url tag>
                    --help"
    exit 1
}

BUILD_DIR="./image_build"
VENDOR=""
ORGCODE=""
CHANNELID=""
PACK_TYPE="owner"
PROJECT_ID=""
PIPELINE_ID=""
URL_TAG=""

CHROOT_TYPE="androidrom"
DEPLOY="elf"

fuyanbin's avatar
fuyanbin committed
27
export PATH=${PATH}:$(pwd)/tools/:$(pwd)/tools/amlogic_tool:$(pwd)/tools/rockchip_tool:$(pwd)/tools/rockchip_tool/obsutil
fuyanbin's avatar
fuyanbin committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
PARSED_ARUGMENTS=$(getopt -a -n "$0" -o 'x' --long help,build-directory:,vendor:,orgcode:,channel-id:,package-type:,project-id:,pipeline-id:,url-tag: -- "$@")
eval set -- "${PARSED_ARUGMENTS}"
while :
do
    case "$1" in
        --build-directory) BUILD_DIR="$2"; shift 2;;
        --vendor) VENDOR="$2"; shift 2;;
        --orgcode) ORGCODE="$2"; shift 2;;
        --channel-id) CHANNELID="$2"; shift 2;;
        --package-type) PACK_TYPE="$2"; shift 2;;
        --project-id) PROJECT_ID="$2"; shift 2;;
        --pipeline-id) PIPELINE_ID="$2"; shift 2;;
        --url-tag) URL_TAG="$2"; shift 2;;
        --help) usage ;;
        --) shift; break;;
        *) usage ;;
    esac
done

build_dir=$(mktemp -d "$BUILD_DIR"/tmp.XXXXX)
packge_dir=$(mktemp -d "$BUILD_DIR"/tmp.XXXXX)
boot_dir=$(mktemp -d "$BUILD_DIR"/tmp.XXXXX)

fuyanbin's avatar
fuyanbin committed
51
52
53
54
system_mount_point=""
system_path=""
systemfile=""
systemfile_ext4=""
fuyanbin's avatar
fuyanbin committed
55
56
57

is_amlogic=1

fuyanbin's avatar
fuyanbin committed
58
outfile_path=""
fuyanbin's avatar
fuyanbin committed
59
60

unpack_and_mount_image() {
fuyanbin's avatar
fuyanbin committed
61
    local img="$1"
fuyanbin's avatar
fuyanbin committed
62
63
64
65
66
67
68
    local loopdev

    if aml_image_v2_packer -c "$img"
    then
        is_amlogic=1
        systemfile="$build_dir/system.PARTITION"
        systemfile_ext4="$build_dir/system.PARTITION.ext4"
fuyanbin's avatar
fuyanbin committed
69

fuyanbin's avatar
fuyanbin committed
70
71
        aml_image_v2_packer -d "$img" "$build_dir"
    else
fuyanbin's avatar
fuyanbin committed
72
        local update_img="${build_dir}/$(basename "$img")-update.img"
fuyanbin's avatar
fuyanbin committed
73
        local update_img_tmp_dir="$build_dir/img_dir"
fuyanbin's avatar
fuyanbin committed
74
        is_amlogic=0
fuyanbin's avatar
fuyanbin committed
75
        img_unpack "$img" "$update_img"
fuyanbin's avatar
fuyanbin committed
76
        mkdir -p "$update_img_tmp_dir"
77
78
        # fix: afptool 只能用相对路径创建Image目录
        pushd "$build_dir"
79
        afptool -unpack "$update_img" "./img_dir"
80
        popd
fuyanbin's avatar
fuyanbin committed
81
82
        systemfile="$update_img_tmp_dir/Image/system.img"
        systemfile_ext4="$update_img_tmp_dir/Image/system.img.ext4"
fuyanbin's avatar
fuyanbin committed
83
    fi
fuyanbin's avatar
fuyanbin committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

    if file "$systemfile" | grep "Android sparse image"
    then
        simg2img "$systemfile" "$systemfile_ext4"
        loopdev=$(losetup -P -f --show "$systemfile_ext4")
    else
        loopdev=$(losetup -P -f --show "$systemfile")
    fi

    system_mount_point="$build_dir/system"
    mkdir "$system_mount_point"
    mount "$loopdev" "$system_mount_point"

    if [[ -d "$system_mount_point/system" ]]
    then
        system_path="$system_mount_point/system"
    else
        system_path="$system_mount_point"
    fi
fuyanbin's avatar
fuyanbin committed
103
104
    losetup -d "$loopdev"
}
fuyanbin's avatar
fuyanbin committed
105

fuyanbin's avatar
fuyanbin committed
106
107
108
109
110
build_image() {
    local img="$1"
    local android_sdk

    unpack_and_mount_image "$img"
fuyanbin's avatar
fuyanbin committed
111
112
113
114
115
116
117
118
    touch "$system_path/.androidrom"

    rm -f "$system_path/hugep-all.zip"
    rm -f "$system_path/bin/hugep-all.zip"

    android_sdk="$(grep "build\.version\.sdk" "$system_path"/build.prop | awk -F'=' '{print $2}')"
    android_sdk="${android_sdk//$'\r'}"
    android_sdk="${android_sdk//$'\n'}"
fuyanbin's avatar
fuyanbin committed
119
    if [[ "$android_sdk" -ge 25 ]]
fuyanbin's avatar
fuyanbin committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    then
        find ./services/ -type f -name "*.rc" -exec install -t "$system_path"/etc/init/ {} \;
    else
        pushd "$boot_dir"
        abootimg -x "$build_dir/boot.PARTITION" ramdisk
        mkdir initrd_dir
        pushd initrd_dir
        gunzip -c ../initrd.img | cpio -i
        popd
        cp initrd_dir/init.rc .
        rm -rf initrd_dir
        popd
        cp "$boot_dir"/init.rc "$packge_dir"/init.rc
        rm -rf "$boot_dir"
        if ! find_autostart -script_name preinstall.sh -initrc "$packge_dir"/init.rc -root_path "$build_dir" > "$packge_dir"/find_autostart.log 2>/dev/null
        then
            find_autostart -script_name recovery.sh -initrc "$packge_dir"/init.rc -root_path "$build_dir" > "$packge_dir"/find_autostart.log 2>/dev/null
        fi
        local auto_start_script
        auto_start_script="$(cat "$packge_dir"/find_autostart.log)"
        if [[ -z "$auto_start_script" ]] && [[ -f "$system_path"/bin/startsoftdetector.sh ]]
        then
            # TODO: startsoftdetector.sh 会阻塞在某一步,要把 starthugep.sh 加在中间部分
            auto_start_script="/system/bin/startsoftdetector.sh"
        fi
        # NOTE: 返回的脚本路径里一定以 /system/ 开头, 所以下边拼路径时没有加上 /system
        if [[ -n "$auto_start_script" ]]
        then
            local _script_path
149
            _script_path="$system_path/$(echo "$auto_start_script" | sed 's|^/system||')"
150
151
            local tmp_script_path="/tmp/tmp_auto_start.sh"
            local start_n
fuyanbin's avatar
fuyanbin committed
152
153
            if ! grep -q "#!/system/bin/sh" "$_script_path"
            then
154
155
156
                start_n=1
            else
                start_n=2
fuyanbin's avatar
fuyanbin committed
157
            fi
158
159
160
161
162
163
164
165

            {
                echo "#!/system/bin/sh"
                echo " "
                echo "/system/bin/starthugep.sh > /dev/null 2>&1 &"
                tail -n +"${start_n}" "$_script_path"
            } > "$tmp_script_path"

166
            cat "$tmp_script_path" | tee "$_script_path"
fuyanbin's avatar
fuyanbin committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
            chmod 0755 "$_script_path"
            chcon u:object_r:drmserver_exec:s0 "$_script_path"
        fi
    fi

    cp -f "$build_dir/hugep-all.zip" "$system_path"/bin/hugep-all.zip

    find ./services/ -type f ! -name  "*.rc" ! -name "*.zip" -exec install -m 0777 -t "$system_path"/bin/ {} \;
    cp -f services/manager_bin_file.zip "$system_path"/bin/manager_bin_file.zip

    for file in $(find ./services/ -type f ! -name "*.rc" ! -name "*.zip")
    do
        chcon u:object_r:drmserver_exec:s0 "$system_path/bin/$(basename "$file")"
    done

182
183
184
185
186
    for file in $(find ./services/ -type f -name "*.rc" ! -name "*.zip")
    do
        chcon u:object_r:drmserver_exec:s0 "$system_path/etc/init/$(basename "$file")"
    done

fuyanbin's avatar
fuyanbin committed
187
188
189
190
191
    pack_image "$img"
}

pack_image() {
    local img
fuyanbin's avatar
typo    
fuyanbin committed
192
    img="$1"
fuyanbin's avatar
fuyanbin committed
193

fuyanbin's avatar
typo    
fuyanbin committed
194
    umount "$system_mount_point"
fuyanbin's avatar
fuyanbin committed
195
196
197
198
199
200
201
202
203
    rm -rf "$system_mount_point"
    sync

    if [[ -f "$systemfile_ext4" ]]
    then
        rm -f "$systemfile"
        img2simg "$systemfile_ext4" "$systemfile"
        rm -f "$systemfile_ext4"
    fi
fuyanbin's avatar
fuyanbin committed
204

fuyanbin's avatar
fuyanbin committed
205
    outfile_path="${packge_dir}/${CHANNELID}-$(basename "$img")"
fuyanbin's avatar
fuyanbin committed
206
207
208
209
    if [[ "$is_amlogic" == "1" ]] 
    then
        aml_image_v2_packer -r "/$build_dir/image.cfg" "$build_dir" "$outfile_path"
    else
fuyanbin's avatar
fuyanbin committed
210
211
212
        local update_img_tmp_dir="$build_dir/img_dir"
        find "$update_img_tmp_dir/Image/" -name parameter.txt -exec cp {} "$update_img_tmp_dir/parameter" \;
        afptool -pack "$update_img_tmp_dir" "${outfile_path}.tmp"
213
214
        cp "$build_dir/img_dir/Image/MiniLoaderAll.bin" "${packge_dir}/MiniLoaderAll.bin"
        img_maker "${packge_dir}/MiniLoaderAll.bin" "${outfile_path}.tmp" "$outfile_path"
fuyanbin's avatar
fuyanbin committed
215
    fi
fuyanbin's avatar
fuyanbin committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    rm -rf "$build_dir"
}

generate_vendor_confg() {
    local yaml_file_src
    local yaml_file

    yaml_file="$build_dir"/vendor.yaml
    yaml_file_src="$build_dir"/vendor.yaml.src
    cp vendor.yaml.src "$yaml_file_src"
    sed -i'' "s#__VENDOR__#$VENDOR#g"  "$yaml_file_src"
    sed -i'' "s#__ORGCODE__#$ORGCODE#g"  "$yaml_file_src"
    sed -i'' "s#__CHROOT_TYPE__#$CHROOT_TYPE#g"  "$yaml_file_src"
    sed -i'' "s#__DEPLOY__#$DEPLOY#g"  "$yaml_file_src"
    sed -i'' "s#__CHANNEL_ID__#$CHANNELID#g"  "$yaml_file_src"
    sed -i'' "s#__PACK_TYPE__#$PACK_TYPE#g"  "$yaml_file_src"
    aio -m enc -i "$yaml_file_src" -o "$yaml_file"
}

update_hugepall_zipfile() {
    local yaml_file
    local hugep_zip_file
    local hugep_zip_file_origin

    yaml_file="$build_dir"/vendor.yaml
    hugep_zip_file="$build_dir/hugep-all.zip"
    hugep_zip_file_origin="./services/hugep-all.zip"

    cp "$hugep_zip_file_origin" "$hugep_zip_file"

    zip -u -j "$hugep_zip_file" "$yaml_file"
247
    zip -u -j "$hugep_zip_file" "./services/hugep-start.sh"
fuyanbin's avatar
fuyanbin committed
248
249
250
251
252
253
254
255
256
257
}

upload() {
    local date_i
    local outfile
    local outfile_gz
    local uri
    local random_num
    local -A result

fuyanbin's avatar
fuyanbin committed
258
    outfile="$outfile_path"
fuyanbin's avatar
fuyanbin committed
259
    outfile_gz="${outfile}.gz"
fuyanbin's avatar
fuyanbin committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
    date_i="$(date +%Y-%m-%d-%s)"

    zstd --ultra --format=gzip "$outfile" -o "$outfile_gz"
    random_num=$(< /dev/urandom tr -dC 0-9a-z | head -c 8)
    [[ -z "$URL_TAG" ]] && URL_TAG="${date_i}-${random_num}"
    uri="product/terminal/chroot_androidrom/""$ORGCODE/${date_i}-${URL_TAG}/$(basename "$outfile_gz")"
    obsutil cp "$outfile_gz" obs://fae-cdn.linkfog.cn/"$uri"

    result["url"]="https://fae-cdn.linkfog.cn/""$uri"
    result["md5"]="$(md5sum "${outfile}" | awk '{print $1}')"
    mkdir -p .result
    jq -n --arg url "${result[url]}" --arg md5 "${result[md5]}" '{$url, $md5}' > ".result/${PROJECT_ID}-${PIPELINE_ID}.json"
    echo ".result/${PROJECT_ID}-${PIPELINE_ID}.json"
    cat ".result/${PROJECT_ID}-${PIPELINE_ID}.json"

    rm -f "$outfile" "$outfile_gz"
    rm -rf "$packge_dir"
}

IMAGE_FILE="/baseimages/$VENDOR-$ORGCODE.img"
280
[[ ! -f "$IMAGE_FILE" ]] && IMAGE_FILE="CH-A03.img"
fuyanbin's avatar
fuyanbin committed
281
282
283
284
generate_vendor_confg
update_hugepall_zipfile
build_image "$IMAGE_FILE"
upload