qemustart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. [ -z $o_nonetwork ] || 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. <subtarget> will default to "generic" and must be specified if
  82. <extra-qemu-options> are present
  83. e.g. <subtarget> for malta can be le, be, le64, be64, le-glibc, le64-glibc, etc
  84. <kernel>, <rootfs> can be required or optional arguments to qemu depending on
  85. the actual <target> in use. They will default to files under bin/targets/
  86. Examples
  87. $SELF x86 64
  88. $SELF x86 64 --machine q35,accel=kvm -device virtio-balloon-pci
  89. $SELF x86 64 -incoming tcp:0:4444
  90. $SELF x86 64-glibc
  91. $SELF malta be -m 64
  92. $SELF malta le64
  93. $SELF malta be-glibc
  94. $SELF armvirt 32 \\
  95. --machine virt,highmem=off \\
  96. --kernel bin/targets/armvirt/32/openwrt-armvirt-32-zImage \\
  97. --rootfs bin/targets/armvirt/32/openwrt-armvirt-32-root.ext4
  98. EOF
  99. }
  100. rand_mac() {
  101. hexdump -n 3 -e '"52:54:00" 3/1 ":%02x"' /dev/urandom
  102. }
  103. parse_args() {
  104. o_qemu_extra=()
  105. while [ "$#" -gt 0 ]; do
  106. case "$1" in
  107. --no-network|-n) o_nonetwork=1; shift ;;
  108. --kernel) o_kernel="$2"; shift 2 ;;
  109. --rootfs) o_rootfs="$2"; shift 2 ;;
  110. --machine|-machine|-M) o_mach="$2"; shift 2 ;;
  111. --help|-h)
  112. usage
  113. exit 0
  114. ;;
  115. *)
  116. if [ -z "$o_target" ]; then
  117. o_target="$1"
  118. elif [ -z "$o_subtarget" ]; then
  119. o_subtarget="$1"
  120. else
  121. o_qemu_extra+=("$1")
  122. fi
  123. shift
  124. ;;
  125. esac
  126. done
  127. MAC_LAN="$(rand_mac)"
  128. MAC_WAN="$(rand_mac)"
  129. [ -n "$o_target" ] || {
  130. usage
  131. return 1
  132. }
  133. [ -n "$o_subtarget" ] || o_subtarget="generic"
  134. o_bindir="bin/targets/$o_target/$o_subtarget"
  135. }
  136. start_qemu_armvirt() {
  137. local kernel="$o_kernel"
  138. local rootfs="$o_rootfs"
  139. local mach="${o_mach:-virt}"
  140. local cpu
  141. local qemu_exe
  142. case "${o_subtarget%-*}" in
  143. 32)
  144. qemu_exe="qemu-system-arm"
  145. cpu="cortex-a15"
  146. [ -n "$kernel" ] || kernel="$o_bindir/openwrt-$o_target-${o_subtarget%-*}-zImage-initramfs"
  147. ;;
  148. 64)
  149. qemu_exe="qemu-system-aarch64"
  150. cpu="cortex-a57"
  151. [ -n "$kernel" ] || kernel="$o_bindir/openwrt-$o_target-${o_subtarget%-*}-Image-initramfs"
  152. ;;
  153. *)
  154. __errmsg "target $o_target: unknown subtarget $o_subtarget"
  155. return 1
  156. ;;
  157. esac
  158. [ -z "$rootfs" ] || {
  159. if [ ! -f "$rootfs" -a -s "$rootfs.gz" ]; then
  160. gunzip "$rootfs.gz"
  161. fi
  162. o_qemu_extra+=( \
  163. "-drive" "file=$rootfs,format=raw,if=virtio" \
  164. "-append" "root=/dev/vda rootwait" \
  165. )
  166. }
  167. [ -n $o_nonetwork ] || {
  168. o_qemu_extra+=( \
  169. "-netdev" "bridge,id=lan,br=$BR_LAN,helper=$HELPER" \
  170. "-device" "virtio-net-pci,id=devlan,netdev=lan,mac=$MAC_LAN" \
  171. "-netdev" "bridge,id=wan,br=$BR_WAN,helper=$HELPER" "-device" \
  172. "virtio-net-pci,id=devwan,netdev=wan,mac=$MAC_WAN" \
  173. )
  174. }
  175. "$qemu_exe" -machine "$mach" -cpu "$cpu" -nographic \
  176. -kernel "$kernel" \
  177. "${o_qemu_extra[@]}"
  178. }
  179. start_qemu_malta() {
  180. local is64
  181. local isel
  182. local qemu_exe
  183. local rootfs="$o_rootfs"
  184. local kernel="$o_kernel"
  185. local mach="${o_mach:-malta}"
  186. # o_subtarget can be le, be, le64, be64, le-glibc, le64-glibc, etc..
  187. is64="$(echo $o_subtarget | grep -o 64)"
  188. [ "$(echo "$o_subtarget" | grep -o '^..')" = "le" ] && isel="el"
  189. qemu_exe="qemu-system-mips$is64$isel"
  190. [ -n "$kernel" ] || kernel="$o_bindir/openwrt-malta-${o_subtarget%-*}-vmlinux-initramfs.elf"
  191. [ -z "$rootfs" ] || {
  192. if [ ! -f "$rootfs" -a -s "$rootfs.gz" ]; then
  193. gunzip "$rootfs.gz"
  194. fi
  195. o_qemu_extra+=( \
  196. "-drive" "file=$rootfs,format=raw" \
  197. "-append" "root=/dev/sda rootwait" \
  198. )
  199. }
  200. # NOTE: order of wan, lan -device arguments matters as it will affect which
  201. # one will be actually used as the wan, lan network interface inside the
  202. # guest machine
  203. [ -n $o_nonetwork ] || {
  204. o_qemu_extra+=( \
  205. "-netdev" "bridge,id=wan,br=$BR_WAN,helper=$HELPER" "-device" \
  206. "virtio-net-pci,id=devwan,netdev=wan,mac=$MAC_WAN" \
  207. "-netdev" "bridge,id=lan,br=$BR_LAN,helper=$HELPER" \
  208. "-device" "virtio-net-pci,id=devlan,netdev=lan,mac=$MAC_LAN" \
  209. )
  210. }
  211. "$qemu_exe" -machine "$mach" -nographic \
  212. -kernel "$kernel" \
  213. "${o_qemu_extra[@]}"
  214. }
  215. start_qemu_x86() {
  216. local qemu_exe
  217. local kernel="$o_kernel"
  218. local rootfs="$o_rootfs"
  219. local mach="${o_mach:-pc}"
  220. [ -n "$rootfs" ] || {
  221. rootfs="$o_bindir/openwrt-$o_target-${o_subtarget%-*}-combined-ext4.img"
  222. if [ ! -f "$rootfs" -a -s "$rootfs.gz" ]; then
  223. gunzip "$rootfs.gz"
  224. fi
  225. }
  226. #
  227. # generic: 32-bit, pentium4 (CONFIG_MPENTIUM4), kvm guest, virtio
  228. # legacy: 32-bit, i486 (CONFIG_M486)
  229. # 64: 64-bit, kvm guest, virtio
  230. #
  231. case "${o_subtarget%-*}" in
  232. legacy) qemu_exe="qemu-system-i386" ;;
  233. generic|64) qemu_exe="qemu-system-x86_64" ;;
  234. *)
  235. __errmsg "target $o_target: unknown subtarget $o_subtarget"
  236. return 1
  237. ;;
  238. esac
  239. [ -n "$kernel" ] && {
  240. o_qemu_extra+=( \
  241. "-kernel" "$kernel" \
  242. "-append" "root=/dev/vda console=ttyS0 rootwait" \
  243. )
  244. }
  245. [ -n $o_nonetwork ] || {
  246. o_qemu_extra+=( \
  247. "-netdev" "bridge,id=lan,br=$BR_LAN,helper=$HELPER" \
  248. "-device" "virtio-net-pci,id=devlan,netdev=lan,mac=$MAC_LAN" \
  249. "-netdev" "bridge,id=wan,br=$BR_WAN,helper=$HELPER" "-device" \
  250. "virtio-net-pci,id=devwan,netdev=wan,mac=$MAC_WAN" \
  251. )
  252. }
  253. case "${o_subtarget%-*}" in
  254. legacy)
  255. # use IDE (PATA) disk instead of AHCI (SATA). Refer to link
  256. # [1] for related discussions
  257. #
  258. # To use AHCI interface
  259. #
  260. # -device ich9-ahci,id=ahci \
  261. # -device ide-drive,drive=drv0,bus=ahci.0 \
  262. # -drive "file=$rootfs,format=raw,id=drv0,if=none" \
  263. #
  264. # [1] https://dev.openwrt.org/ticket/17947
  265. "$qemu_exe" -machine "$mach" -nographic \
  266. -device ide-drive,drive=drv0 \
  267. -drive "file=$rootfs,format=raw,id=drv0,if=none" \
  268. "${o_qemu_extra[@]}"
  269. ;;
  270. generic|64)
  271. "$qemu_exe" -machine "$mach" -nographic \
  272. -drive "file=$rootfs,format=raw,if=virtio" \
  273. "${o_qemu_extra[@]}"
  274. ;;
  275. esac
  276. }
  277. start_qemu() {
  278. case "$o_target" in
  279. armvirt) start_qemu_armvirt ;;
  280. malta) start_qemu_malta ;;
  281. x86) start_qemu_x86 ;;
  282. *)
  283. __errmsg "target $o_target is not supported yet"
  284. return 1
  285. ;;
  286. esac
  287. }
  288. parse_args "$@" \
  289. && check_setup \
  290. && start_qemu