1
0

qemustart 7.5 KB

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