2
0

qemustart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #!/usr/bin/env bash
  2. SELF="$0"
  3. # Linux bridge for connecting lan and wan network of guest machines
  4. BR_LAN="${BR_LAN:-br-lan}"
  5. BR_WAN="${BR_WAN:-br-wan}"
  6. # Host network interface providing internet access for guest machines
  7. IF_INET="${IF_INET:-eth0}"
  8. # qemu-bridge-helper does two things here
  9. #
  10. # - create tap interface
  11. # - add the tap interface to bridge
  12. #
  13. # as such it requires CAP_NET_ADMIN to do its job. It will be convenient to
  14. # have it as a root setuid program. Be aware of the security risks implied
  15. #
  16. # the helper has an acl list which defaults to deny all bridge. we need to add
  17. # $BR_LAN and $BR_WAN to its allow list
  18. #
  19. # # sudo vim /etc/qemu/bridge.conf
  20. # allow br-lan
  21. # allow br-wan
  22. #
  23. # Other allowed directives can be 'allow all', 'deny all', 'include xxx', See
  24. # qemu-bridge-helper.c of qemu source code for details.
  25. #
  26. # The helper can be provided by package qemu-system-common on debian, or
  27. # qemu-kvm-common on rhel
  28. #
  29. HELPER="${HELPER:-/usr/libexec/qemu-bridge-helper}"
  30. ### end of global settings
  31. __errmsg() {
  32. echo "$*" >&2
  33. }
  34. do_setup() {
  35. # setup bridge for LAN network
  36. sudo ip link add dev "$BR_LAN" type bridge
  37. sudo ip link set dev "$BR_LAN" up
  38. sudo ip addr add 192.168.1.3/24 dev "$BR_LAN"
  39. # setup bridge for WAN network
  40. #
  41. # minimal dnsmasq config for configuring guest wan network with dhcp
  42. #
  43. # # sudo apt-get install dnsmasq
  44. # # sudo vi /etc/dnsmasq.conf
  45. # interface=br-wan
  46. # dhcp-range=192.168.7.50,192.168.7.150,255.255.255.0,30m
  47. #
  48. sudo ip link add dev "$BR_WAN" type bridge
  49. sudo ip link set dev "$BR_WAN" up
  50. sudo ip addr add 192.168.7.1/24 dev "$BR_WAN"
  51. # guest internet access
  52. sudo sysctl -w "net.ipv4.ip_forward=1"
  53. sudo sysctl -w "net.ipv4.conf.$BR_WAN.proxy_arp=1"
  54. while sudo iptables -t nat -D POSTROUTING -o "$IF_INET" -j MASQUERADE 2>/dev/null; do true; done
  55. sudo iptables -t nat -A POSTROUTING -o "$IF_INET" -j MASQUERADE
  56. }
  57. check_setup_() {
  58. ip link show "$BR_LAN" >/dev/null || return 1
  59. ip link show "$BR_WAN" >/dev/null || return 1
  60. [ -x "$HELPER" ] || {
  61. __errmsg "helper $HELPER is not an executable"
  62. return 1
  63. }
  64. }
  65. check_setup() {
  66. [ -n "$o_network" ] || return 0
  67. check_setup_ || {
  68. __errmsg "please check the script content to see the environment requirement"
  69. return 1
  70. }
  71. }
  72. #do_setup; check_setup; exit $?
  73. usage() {
  74. cat >&2 <<EOF
  75. Usage: $SELF [-h|--help]
  76. $SELF <target>
  77. [<subtarget> [<extra-qemu-options>]]
  78. [--kernel <kernel>]
  79. [--rootfs <rootfs>]
  80. [--machine <machine>]
  81. [-n|--network]
  82. <subtarget> will default to "generic" and must be specified if
  83. <extra-qemu-options> are present
  84. e.g. <subtarget> for malta can be le, be, le64, be64, le-glibc, le64-glibc, etc
  85. <kernel>, <rootfs> can be required or optional arguments to qemu depending on
  86. the actual <target> in use. They will default to files under bin/targets/
  87. Examples
  88. $SELF x86 64
  89. $SELF x86 64 --machine q35,accel=kvm -device virtio-balloon-pci
  90. $SELF x86 64 -incoming tcp:0:4444
  91. $SELF x86 64-glibc
  92. $SELF malta be -m 64
  93. $SELF malta le64
  94. $SELF malta be-glibc
  95. $SELF armvirt 32 \\
  96. --machine virt,highmem=off \\
  97. --kernel bin/targets/armvirt/32/openwrt-armvirt-32-zImage \\
  98. --rootfs bin/targets/armvirt/32/openwrt-armvirt-32-root.ext4
  99. EOF
  100. }
  101. rand_mac() {
  102. hexdump -n 3 -e '"52:54:00" 3/1 ":%02x"' /dev/urandom
  103. }
  104. parse_args() {
  105. o_network=
  106. o_qemu_extra=()
  107. while [ "$#" -gt 0 ]; do
  108. case "$1" in
  109. --kernel) o_kernel="$2"; shift 2 ;;
  110. --rootfs) o_rootfs="$2"; shift 2 ;;
  111. --machine|-machine|-M) o_mach="$2"; shift 2 ;;
  112. --network|-n) o_network=1; shift ;;
  113. --help|-h)
  114. usage
  115. exit 0
  116. ;;
  117. *)
  118. if [ -z "$o_target" ]; then
  119. o_target="$1"
  120. elif [ -z "$o_subtarget" ]; then
  121. o_subtarget="$1"
  122. else
  123. o_qemu_extra+=("$1")
  124. fi
  125. shift
  126. ;;
  127. esac
  128. done
  129. MAC_LAN="$(rand_mac)"
  130. MAC_WAN="$(rand_mac)"
  131. [ -n "$o_target" ] || {
  132. usage
  133. return 1
  134. }
  135. [ -n "$o_subtarget" ] || o_subtarget="generic"
  136. o_bindir="bin/targets/$o_target/$o_subtarget"
  137. }
  138. start_qemu_armvirt() {
  139. local kernel="$o_kernel"
  140. local rootfs="$o_rootfs"
  141. local mach="${o_mach:-virt}"
  142. local cpu
  143. local qemu_exe
  144. case "${o_subtarget%-*}" in
  145. 32)
  146. qemu_exe="qemu-system-arm"
  147. cpu="cortex-a15"
  148. [ -n "$kernel" ] || kernel="$o_bindir/openwrt-$o_target-${o_subtarget%-*}-zImage-initramfs"
  149. ;;
  150. 64)
  151. qemu_exe="qemu-system-aarch64"
  152. cpu="cortex-a57"
  153. [ -n "$kernel" ] || kernel="$o_bindir/openwrt-$o_target-${o_subtarget%-*}-Image-initramfs"
  154. ;;
  155. *)
  156. __errmsg "target $o_target: unknown subtarget $o_subtarget"
  157. return 1
  158. ;;
  159. esac
  160. [ -z "$rootfs" ] || {
  161. if [ ! -f "$rootfs" -a -s "$rootfs.gz" ]; then
  162. gunzip "$rootfs.gz"
  163. fi
  164. o_qemu_extra+=( \
  165. "-drive" "file=$rootfs,format=raw,if=virtio" \
  166. "-append" "root=/dev/vda rootwait" \
  167. )
  168. }
  169. [ -z "$o_network" ] || {
  170. o_qemu_extra+=( \
  171. "-netdev" "bridge,id=lan,br=$BR_LAN,helper=$HELPER" \
  172. "-device" "virtio-net-pci,id=devlan,netdev=lan,mac=$MAC_LAN" \
  173. "-netdev" "bridge,id=wan,br=$BR_WAN,helper=$HELPER" "-device" \
  174. "virtio-net-pci,id=devwan,netdev=wan,mac=$MAC_WAN" \
  175. )
  176. }
  177. "$qemu_exe" -machine "$mach" -cpu "$cpu" -nographic \
  178. -kernel "$kernel" \
  179. "${o_qemu_extra[@]}"
  180. }
  181. start_qemu_malta() {
  182. local is64
  183. local isel
  184. local qemu_exe
  185. local rootfs="$o_rootfs"
  186. local kernel="$o_kernel"
  187. local mach="${o_mach:-malta}"
  188. # o_subtarget can be le, be, le64, be64, le-glibc, le64-glibc, etc..
  189. is64="$(echo $o_subtarget | grep -o 64)"
  190. [ "$(echo "$o_subtarget" | grep -o '^..')" = "le" ] && isel="el"
  191. qemu_exe="qemu-system-mips$is64$isel"
  192. [ -n "$kernel" ] || kernel="$o_bindir/openwrt-malta-${o_subtarget%-*}-vmlinux-initramfs.elf"
  193. [ -z "$rootfs" ] || {
  194. if [ ! -f "$rootfs" -a -s "$rootfs.gz" ]; then
  195. gunzip "$rootfs.gz"
  196. fi
  197. o_qemu_extra+=( \
  198. "-drive" "file=$rootfs,format=raw" \
  199. "-append" "root=/dev/sda rootwait" \
  200. )
  201. }
  202. # NOTE: order of wan, lan -device arguments matters as it will affect which
  203. # one will be actually used as the wan, lan network interface inside the
  204. # guest machine
  205. [ -z "$o_network" ] || {
  206. o_qemu_extra+=( \
  207. "-netdev" "bridge,id=wan,br=$BR_WAN,helper=$HELPER" "-device" \
  208. "virtio-net-pci,id=devwan,netdev=wan,mac=$MAC_WAN" \
  209. "-netdev" "bridge,id=lan,br=$BR_LAN,helper=$HELPER" \
  210. "-device" "virtio-net-pci,id=devlan,netdev=lan,mac=$MAC_LAN" \
  211. )
  212. }
  213. "$qemu_exe" -machine "$mach" -nographic \
  214. -kernel "$kernel" \
  215. "${o_qemu_extra[@]}"
  216. }
  217. start_qemu_x86() {
  218. local qemu_exe
  219. local kernel="$o_kernel"
  220. local rootfs="$o_rootfs"
  221. local mach="${o_mach:-pc}"
  222. [ -n "$rootfs" ] || {
  223. rootfs="$o_bindir/openwrt-$o_target-${o_subtarget%-*}-combined-ext4.img"
  224. if [ ! -f "$rootfs" -a -s "$rootfs.gz" ]; then
  225. gunzip "$rootfs.gz"
  226. fi
  227. }
  228. #
  229. # generic: 32-bit, pentium4 (CONFIG_MPENTIUM4), kvm guest, virtio
  230. # legacy: 32-bit, i486 (CONFIG_M486)
  231. # 64: 64-bit, kvm guest, virtio
  232. #
  233. case "${o_subtarget%-*}" in
  234. legacy) qemu_exe="qemu-system-i386" ;;
  235. generic|64) qemu_exe="qemu-system-x86_64" ;;
  236. *)
  237. __errmsg "target $o_target: unknown subtarget $o_subtarget"
  238. return 1
  239. ;;
  240. esac
  241. [ -n "$kernel" ] && {
  242. o_qemu_extra+=( \
  243. "-kernel" "$kernel" \
  244. "-append" "root=/dev/vda console=ttyS0 rootwait" \
  245. )
  246. }
  247. [ -z "$o_network" ] || {
  248. o_qemu_extra+=( \
  249. "-netdev" "bridge,id=lan,br=$BR_LAN,helper=$HELPER" \
  250. "-device" "virtio-net-pci,id=devlan,netdev=lan,mac=$MAC_LAN" \
  251. "-netdev" "bridge,id=wan,br=$BR_WAN,helper=$HELPER" "-device" \
  252. "virtio-net-pci,id=devwan,netdev=wan,mac=$MAC_WAN" \
  253. )
  254. }
  255. case "${o_subtarget%-*}" in
  256. legacy)
  257. # use IDE (PATA) disk instead of AHCI (SATA). Refer to link
  258. # [1] for related discussions
  259. #
  260. # To use AHCI interface
  261. #
  262. # -device ich9-ahci,id=ahci \
  263. # -device ide-drive,drive=drv0,bus=ahci.0 \
  264. # -drive "file=$rootfs,format=raw,id=drv0,if=none" \
  265. #
  266. # [1] https://dev.openwrt.org/ticket/17947
  267. "$qemu_exe" -machine "$mach" -nographic \
  268. -device ide-drive,drive=drv0 \
  269. -drive "file=$rootfs,format=raw,id=drv0,if=none" \
  270. "${o_qemu_extra[@]}"
  271. ;;
  272. generic|64)
  273. "$qemu_exe" -machine "$mach" -nographic \
  274. -drive "file=$rootfs,format=raw,if=virtio" \
  275. "${o_qemu_extra[@]}"
  276. ;;
  277. esac
  278. }
  279. start_qemu() {
  280. case "$o_target" in
  281. armvirt) start_qemu_armvirt ;;
  282. malta) start_qemu_malta ;;
  283. x86) start_qemu_x86 ;;
  284. *)
  285. __errmsg "target $o_target is not supported yet"
  286. return 1
  287. ;;
  288. esac
  289. }
  290. parse_args "$@" \
  291. && check_setup \
  292. && start_qemu