1#!/bin/bash 2KERNEL_VERSION="5.15" 3DEFAULT_BRANCH="aosp_kernel-common-android13-${KERNEL_VERSION}" 4 5# Examples: 6# to update 7# * kernel and goldfish modules (recommended): 8# ./update_emu_kernel.sh --bug 123 --bid 6332140 9# * only goldfish modules: 10# ./update_emu_kernel.sh --bug 123 --bid 6332140 --update modules 11# * only kernel: 12# ./update_emu_kernel.sh --bug 123 --bid 6332140 --update kernel 13set -e 14set -o errexit 15source gbash.sh 16 17DEFINE_int bug 0 "Bug with the reason for the update" 18DEFINE_int bid 0 "Build id for goldfish modules" 19DEFINE_string update "both" "Select which prebuilts to update, (kernel|modules|both)" 20DEFINE_string branch "${DEFAULT_BRANCH}" "Branch for fetch_artifact" 21 22fetch_arch() { 23 scratch_dir="${1}" 24 branch="${2}" 25 bid="${3}" 26 do_fetch_kernel="${4}" 27 do_fetch_modules="${5}" 28 kernel_target="${6}" 29 kernel_artifact="${7}" 30 modules_target="${8}" 31 32 mkdir "${scratch_dir}" 33 pushd "${scratch_dir}" 34 35 if [[ "${do_fetch_kernel}" -ne 0 ]]; then 36 /google/data/ro/projects/android/fetch_artifact \ 37 --bid "${bid}" \ 38 --target "${kernel_target}" \ 39 "${kernel_artifact}" 40 fi 41 42 if [[ "${do_fetch_modules}" -ne 0 ]]; then 43 /google/data/ro/projects/android/fetch_artifact \ 44 --bid "${bid}" \ 45 --target "${modules_target}" \ 46 "*.ko" 47 fi 48 49 popd 50} 51 52move_artifacts() { 53 scratch_dir="${1}" 54 dst_dir="${2}" 55 kernel_artifact="${3}" 56 kernel_filename="${4}" 57 do_fetch_modules="${5}" 58 59 pushd "${scratch_dir}" 60 61 if [[ -f "${kernel_artifact}" ]]; then 62 mv "${kernel_artifact}" "${dst_dir}/${kernel_filename}" 63 fi 64 65 if [[ "${do_fetch_modules}" -ne 0 ]]; then 66 rm -rf "${dst_dir}/ko-new" 67 rm -rf "${dst_dir}/ko-old" 68 mkdir "${dst_dir}/ko-new" 69 mv *.ko "${dst_dir}/ko-new" 70 mv "${dst_dir}/ko" "${dst_dir}/ko-old" 71 mv "${dst_dir}/ko-new" "${dst_dir}/ko" 72 rm -rf "${dst_dir}/ko-old" 73 fi 74 75 popd 76} 77 78make_git_commit() { 79 x86_dst_dir="${1}" 80 arm_dst_dir="${2}" 81 git add "${x86_dst_dir}" 82 git add "${arm_dst_dir}" 83 84 git commit -a -m "$( 85 echo Update kernel prebuilts to go/ab/${FLAGS_bid} 86 echo 87 echo Test: TreeHugger 88 echo Bug: ${FLAGS_bug} 89 )" 90 91 git commit --amend -s 92} 93 94main() { 95 fail=0 96 97 if [[ "${FLAGS_bug}" -eq 0 ]]; then 98 echo "Must specify --bug" 1>&2 99 fail=1 100 fi 101 102 if [[ "${FLAGS_bid}" -eq 0 ]]; then 103 echo "Must specify --bid" 1>&2 104 fail=1 105 fi 106 107 do_fetch_kernel=0 108 do_fetch_modules=0 109 case "${FLAGS_update}" in 110 both) 111 do_fetch_kernel=1 112 do_fetch_modules=1 113 ;; 114 kernel) 115 do_fetch_kernel=1 116 ;; 117 modules) 118 do_fetch_modules=1 119 ;; 120 *) 121 echo "Unexpected value for --update, '${FLAGS_update}'" 1>&2 122 fail=1 123 ;; 124 esac 125 126 if [[ "${fail}" -ne 0 ]]; then 127 exit "${fail}" 128 fi 129 130 here="$(pwd)" 131 x86_dst_dir="${here}/x86_64/${KERNEL_VERSION}" 132 arm_dst_dir="${here}/arm64/${KERNEL_VERSION}" 133 134 if [[ ! -d "${x86_dst_dir}" ]]; then 135 mkdir "${x86_dst_dir}" 136 fi 137 if [[ ! -d "${x86_dst_dir}/ko" ]]; then 138 mkdir "${x86_dst_dir}/ko" 139 fi 140 if [[ ! -d "${arm_dst_dir}" ]]; then 141 mkdir "${arm_dst_dir}" 142 fi 143 if [[ ! -d "${arm_dst_dir}/ko" ]]; then 144 mkdir "${arm_dst_dir}/ko" 145 fi 146 147 scratch_dir="$(mktemp -d)" 148 x86_scratch_dir="${scratch_dir}/x86" 149 arm_scratch_dir="${scratch_dir}/arm" 150 151 fetch_arch "${x86_scratch_dir}" \ 152 "${FLAGS_branch}" "${FLAGS_bid}" \ 153 "${do_fetch_kernel}" "${do_fetch_modules}" \ 154 "kernel_x86_64" "bzImage" \ 155 "kernel_virt_x86_64" 156 157 fetch_arch "${arm_scratch_dir}" \ 158 "${FLAGS_branch}" "${FLAGS_bid}" \ 159 "${do_fetch_kernel}" "${do_fetch_modules}" \ 160 "kernel_aarch64" "Image" \ 161 "kernel_virt_aarch64" 162 163 if [[ -f "${arm_scratch_dir}/Image" ]]; then 164 gzip -9 "${arm_scratch_dir}/Image" 165 fi 166 167 move_artifacts "${x86_scratch_dir}" "${x86_dst_dir}" \ 168 "bzImage" "kernel-${KERNEL_VERSION}" \ 169 "${do_fetch_modules}" 170 171 move_artifacts "${arm_scratch_dir}" "${arm_dst_dir}" \ 172 "Image.gz" "kernel-${KERNEL_VERSION}-gz" \ 173 "${do_fetch_modules}" 174 175 make_git_commit "${x86_dst_dir}" "${arm_dst_dir}" 176} 177 178gbash::main "$@" 179