run 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env bash
  2. set -e
  3. # CLI handling.
  4. arch=x86_64
  5. debug_qemu=''
  6. kgdb=false
  7. nographic=false
  8. # norandmaps: Don't use address space randomization. Equivalent to echo 0 > /proc/sys/kernel/randomize_va_space.
  9. # printk.time=y: log in format: "[time ] msg" for all printk messages.
  10. # nokaslr: https://unix.stackexchange.com/questions/397939/turning-off-kaslr-to-debug-linux-kernel-using-qemu-and-gdb
  11. # Turned on by default since v4.12
  12. extra_append='nokaslr norandmaps printk.devkmsg=on printk.time=y'
  13. extra_flags=''
  14. initrd=false
  15. root=''
  16. while getopts a:de:iknqt:x OPT; do
  17. case "$OPT" in
  18. a)
  19. arch="$OPTARG"
  20. ;;
  21. d)
  22. extra_flags="$extra_flags -S -s"
  23. ;;
  24. e)
  25. extra_append="$extra_append $OPTARG"
  26. ;;
  27. k)
  28. extra_append="$extra_append kgdbwait"
  29. # For those who want to try KDB.
  30. #extra_append="$extra_append kgdbwait kgdboc=kbd"
  31. extra_flags="$extra_flags -serial tcp::1234,server,nowait"
  32. kgdb=true
  33. ;;
  34. i)
  35. initrd=true
  36. ;;
  37. n)
  38. extra_append="$extra_append console=ttyS0"
  39. extra_flags="$extra_flags -nographic"
  40. nographic=true
  41. ;;
  42. q)
  43. debug_qemu='gdb -q -ex start --args'
  44. ;;
  45. esac
  46. done
  47. shift "$(($OPTIND - 1))"
  48. extra_flags="$extra_flags $@"
  49. buildroot_out_dir="./buildroot/output.${arch}~"
  50. images_dir="$buildroot_out_dir/images"
  51. qemu_common="\
  52. $debug_qemu \
  53. $buildroot_out_dir/host/usr/bin/qemu-system-${arch} \
  54. -m 128M \
  55. -monitor telnet::45454,server,nowait \
  56. -netdev user,hostfwd=tcp::45455-:45455,id=net0 \
  57. -smp 1 \
  58. "
  59. if $initrd; then
  60. extra_flags="$extra_flags -initrd '${images_dir}/rootfs.cpio'"
  61. fi
  62. # The base QEMU commands are found under board/qemu/*/readme.tx
  63. case "$arch" in
  64. x86_64)
  65. if $kgdb; then
  66. extra_append="$extra_append kgdboc=ttyS0,115200"
  67. fi
  68. if ! $initrd; then
  69. root='root=/dev/vda'
  70. extra_flags="$extra_flags -drive file='${images_dir}/rootfs.ext2.qcow2,if=virtio,format=qcow2'"
  71. fi
  72. cmd="$qemu_common \
  73. -M pc \
  74. -append '$root nopat $extra_append' \
  75. -device edu \
  76. -device lkmc_pci_min \
  77. -device virtio-net-pci,netdev=net0 \
  78. -kernel ${images_dir}/bzImage \
  79. $extra_flags \
  80. "
  81. ;;
  82. arm)
  83. if $kgdb; then
  84. extra_append="$extra_append kgdboc=ttyAMA0,115200"
  85. fi
  86. if ! $initrd; then
  87. root='root=/dev/sda'
  88. extra_flags="$extra_flags -drive file='${images_dir}/rootfs.ext2.qcow2,if=scsi,format=qcow2'"
  89. fi
  90. cmd="$qemu_common \
  91. -M versatilepb \
  92. -append '$root $extra_append' \
  93. -device rtl8139,netdev=net0 \
  94. -dtb ${images_dir}/versatile-pb.dtb \
  95. -kernel ${images_dir}/zImage \
  96. -serial stdio \
  97. $extra_flags \
  98. "
  99. ;;
  100. aarch64)
  101. if $kgdb; then
  102. extra_append="$extra_append kgdboc=ttyAMA0,115200"
  103. fi
  104. cmd="$qemu_common \
  105. -M virt \
  106. -append 'root=/dev/sda $extra_append' \
  107. -cpu cortex-a57 \
  108. -device virtio-net-device,netdev=net0 \
  109. -kernel ${images_dir}/Image \
  110. -nographic \
  111. -serial stdio \
  112. $extra_flags \
  113. "
  114. ;;
  115. mips64)
  116. if ! $initrd; then
  117. root='root=/dev/hda'
  118. extra_flags="$extra_flags -drive file='${images_dir}/rootfs.ext2.qcow2,format=qcow2'"
  119. fi
  120. cmd="$qemu_common \
  121. -M malta \
  122. -append 'root=/dev/hda $extra_append' \
  123. -cpu I6400 \
  124. -device pcnet \
  125. -kernel ${images_dir}/vmlinux \
  126. -nographic \
  127. $extra_flags \
  128. "
  129. ;;
  130. esac
  131. echo "$cmd"
  132. eval "$cmd"