qemu.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/usr/bin/env bash
  2. set -x
  3. set -euo pipefail
  4. # shellcheck disable=SC1091
  5. . lib.sh
  6. build_static_libffi () {
  7. local version=3.0.13
  8. local td
  9. td="$(mktemp -d)"
  10. pushd "${td}"
  11. curl --retry 3 -sSfL "https://github.com/libffi/libffi/archive/refs/tags/v${version}.tar.gz" -O -L
  12. tar --strip-components=1 -xzf "v${version}.tar.gz"
  13. ./configure --prefix="$td"/lib --disable-builddir --disable-shared --enable-static
  14. make "-j$(nproc)"
  15. install -m 644 ./.libs/libffi.a /usr/lib64/
  16. popd
  17. rm -rf "${td}"
  18. }
  19. build_static_libmount () {
  20. local version_spec=2.23.2
  21. local version=2.23
  22. local td
  23. td="$(mktemp -d)"
  24. pushd "${td}"
  25. curl --retry 3 -sSfL "https://kernel.org/pub/linux/utils/util-linux/v${version}/util-linux-${version_spec}.tar.xz" -O -L
  26. tar --strip-components=1 -xJf "util-linux-${version_spec}.tar.xz"
  27. ./configure --disable-shared --enable-static --without-ncurses
  28. make "-j$(nproc)" mount blkid
  29. install -m 644 ./.libs/*.a /usr/lib64/
  30. popd
  31. rm -rf "${td}"
  32. }
  33. build_static_libattr() {
  34. local version=2.4.46
  35. local td
  36. td="$(mktemp -d)"
  37. pushd "${td}"
  38. yum install -y gettext
  39. curl --retry 3 -sSfL "https://download.savannah.nongnu.org/releases/attr/attr-${version}.src.tar.gz" -O
  40. tar --strip-components=1 -xzf "attr-${version}.src.tar.gz"
  41. cp /usr/share/automake*/config.* .
  42. ./configure
  43. make "-j$(nproc)"
  44. install -m 644 ./libattr/.libs/libattr.a /usr/lib64/
  45. yum remove -y gettext
  46. popd
  47. rm -rf "${td}"
  48. }
  49. build_static_libcap() {
  50. local version=2.22
  51. local td
  52. td="$(mktemp -d)"
  53. pushd "${td}"
  54. curl --retry 3 -sSfL "https://www.kernel.org/pub/linux/libs/security/linux-privs/libcap2/libcap-${version}.tar.xz" -O
  55. tar --strip-components=1 -xJf "libcap-${version}.tar.xz"
  56. make "-j$(nproc)"
  57. install -m 644 libcap/libcap.a /usr/lib64/
  58. popd
  59. rm -rf "${td}"
  60. }
  61. build_static_pixman() {
  62. local version=0.34.0
  63. local td
  64. td="$(mktemp -d)"
  65. pushd "${td}"
  66. curl --retry 3 -sSfL "https://www.cairographics.org/releases/pixman-${version}.tar.gz" -O
  67. tar --strip-components=1 -xzf "pixman-${version}.tar.gz"
  68. ./configure
  69. make "-j$(nproc)"
  70. install -m 644 ./pixman/.libs/libpixman-1.a /usr/lib64/
  71. popd
  72. rm -rf "${td}"
  73. }
  74. main() {
  75. local version=5.1.0
  76. if_centos version=4.2.1
  77. local arch="${1}" \
  78. softmmu="${2:-}"
  79. install_packages \
  80. autoconf \
  81. automake \
  82. bison \
  83. bzip2 \
  84. curl \
  85. flex \
  86. libtool \
  87. make \
  88. patch \
  89. python3 \
  90. if_centos install_packages \
  91. gcc-c++ \
  92. pkgconfig \
  93. xz \
  94. glib2-devel \
  95. glib2-static \
  96. glibc-static \
  97. libattr-devel \
  98. libcap-devel \
  99. libfdt-devel \
  100. pcre-static \
  101. pixman-devel \
  102. libselinux-devel \
  103. libselinux-static \
  104. libffi \
  105. libuuid-devel \
  106. libblkid-devel \
  107. libmount-devel \
  108. zlib-devel \
  109. zlib-static
  110. if_centos 'curl --retry 3 -sSfL "https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD" -o /usr/share/automake*/config.guess'
  111. if_centos 'curl --retry 3 -sSfL "https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD" -o /usr/share/automake*/config.sub'
  112. # these are not packaged as static libraries in centos; build them manually
  113. if_centos build_static_libffi
  114. if_centos build_static_libmount
  115. if_centos build_static_libattr
  116. if_centos build_static_libcap
  117. if_centos build_static_pixman
  118. if_ubuntu install_packages \
  119. g++ \
  120. pkg-config \
  121. xz-utils \
  122. libattr1-dev \
  123. libcap-ng-dev \
  124. libffi-dev \
  125. libglib2.0-dev \
  126. libpixman-1-dev \
  127. libselinux1-dev \
  128. zlib1g-dev
  129. # if we have python3.6+, we can install qemu 6.1.0, which needs ninja-build
  130. # ubuntu 16.04 only provides python3.5, so remove when we have a newer qemu.
  131. is_ge_python36=$(python3 -c "import sys; print(int(sys.version_info >= (3, 6)))")
  132. if [[ "${is_ge_python36}" == "1" ]]; then
  133. if_ubuntu version=6.1.0
  134. if_ubuntu install_packages ninja-build
  135. fi
  136. local td
  137. td="$(mktemp -d)"
  138. pushd "${td}"
  139. curl --retry 3 -sSfL "https://download.qemu.org/qemu-${version}.tar.xz" -O
  140. tar --strip-components=1 -xJf "qemu-${version}.tar.xz"
  141. local targets="${arch}-linux-user"
  142. local virtfs=""
  143. case "${softmmu}" in
  144. softmmu)
  145. if [ "${arch}" = "ppc64le" ]; then
  146. targets="${targets},ppc64-softmmu"
  147. else
  148. targets="${targets},${arch}-softmmu"
  149. fi
  150. virtfs="--enable-virtfs"
  151. ;;
  152. "")
  153. true
  154. ;;
  155. *)
  156. echo "Invalid softmmu option: ${softmmu}"
  157. exit 1
  158. ;;
  159. esac
  160. ./configure \
  161. --disable-kvm \
  162. --disable-vnc \
  163. --disable-guest-agent \
  164. --enable-linux-user \
  165. --static \
  166. ${virtfs} \
  167. --target-list="${targets}"
  168. make "-j$(nproc)"
  169. make install
  170. # HACK the binfmt_misc interpreter we'll use expects the QEMU binary to be
  171. # in /usr/bin. Create an appropriate symlink
  172. ln -s "/usr/local/bin/qemu-${arch}" "/usr/bin/qemu-${arch}-static"
  173. purge_packages
  174. popd
  175. rm -rf "${td}"
  176. rm "${0}"
  177. }
  178. main "${@}"