bootstrap.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #!/bin/bash
  2. if test -n "$ZSH_VERSION"; then
  3. CURRENT_SHELL=zsh
  4. elif test -n "$BASH_VERSION"; then
  5. CURRENT_SHELL=bash
  6. elif test -n "$KSH_VERSION"; then
  7. CURRENT_SHELL=ksh
  8. elif test -n "$FCEDIT"; then
  9. CURRENT_SHELL=ksh
  10. elif test -n "$PS3"; then
  11. CURRENT_SHELL=unknown
  12. else
  13. CURRENT_SHELL=sh
  14. fi
  15. source "$HOME/.$CURRENT_SHELL"rc
  16. emulator="qemu"
  17. defpackman="apt-get"
  18. dockerInstall="true"
  19. DEFAULT_INSTALL="false"
  20. export RUSTUP_DIST_SERVER=${RUSTUP_DIST_SERVER:-https://rsproxy.cn}
  21. export RUSTUP_UPDATE_ROOT=${RUSTUP_UPDATE_ROOT:-https://rsproxy.cn/rustup}
  22. export RUST_VERSION="${RUST_VERSION:-nightly-2024-11-05}"
  23. export RUST_VERSION_OLD="${RUST_VERSION:-nightly-2024-07-23}"
  24. banner()
  25. {
  26. echo "|------------------------------------------|"
  27. echo "| Welcome to the DragonOS bootstrap |"
  28. echo "|------------------------------------------|"
  29. }
  30. # 因为编码原因, 只有在vim打开该文件的时候对齐才是真的对齐
  31. congratulations()
  32. {
  33. echo "|-----------Congratulations!---------------|"
  34. echo "| |"
  35. echo "| 你成功安装了DragonOS所需的依赖项! |"
  36. echo "| |"
  37. echo "| 请[关闭]当前终端, 并[重新打开]一个终端 |"
  38. echo "| 然后通过以下命令运行: |"
  39. echo "| |"
  40. echo "| make run |"
  41. echo "| |"
  42. echo "|------------------------------------------|"
  43. }
  44. ####################################
  45. # 当检测到ubuntu或Debian时,执行此函数 #
  46. # 参数:第一个参数为包管理器 #
  47. ####################################
  48. install_ubuntu_debian_pkg()
  49. {
  50. echo "检测到 Ubuntu/Debian"
  51. echo "正在更新包管理器的列表..."
  52. sudo "$1" update
  53. echo "正在安装所需的包..."
  54. sudo "$1" install -y \
  55. ca-certificates \
  56. curl wget \
  57. unzip \
  58. gnupg \
  59. lsb-release \
  60. llvm-dev libclang-dev clang gcc-multilib \
  61. gcc build-essential fdisk dosfstools dnsmasq bridge-utils iptables libssl-dev pkg-config \
  62. python3-sphinx make git
  63. # 必须分开安装,否则会出现错误
  64. sudo "$1" install -y \
  65. gcc-riscv64-unknown-elf gcc-riscv64-linux-gnu gdb-multiarch
  66. # 如果python3没有安装
  67. if [ -z "$(which python3)" ]; then
  68. echo "正在安装python3..."
  69. sudo apt install -y python3 python3-pip
  70. fi
  71. if [ -z "$(which docker)" ] && [ "${dockerInstall}" = "true" ]; then
  72. echo "正在安装docker..."
  73. sudo apt install -y docker.io docker-compose
  74. sudo groupadd docker
  75. sudo usermod -aG docker $USER
  76. sudo systemctl restart docker
  77. elif [ -z ${dockerInstall} ]; then
  78. echo "您传入--no-docker参数生效, 安装docker步骤被跳过."
  79. elif [ -n "$(which docker)" ]; then
  80. echo "您的计算机上已经安装了docker"
  81. fi
  82. if [ -z "$(which qemu-system-x86_64)" ]; then
  83. echo "正在安装QEMU虚拟机..."
  84. sudo $1 install -y qemu-system qemu-kvm
  85. else
  86. echo "QEMU已经在您的电脑上安装!"
  87. fi
  88. }
  89. ####################################
  90. # 当检测到gentoo时,执行此函数 #
  91. ####################################
  92. gentoo()
  93. {
  94. pkgman="emerge"
  95. echo "检测到Gentoo发行版"
  96. echo "正在更新包管理器的列表..."
  97. sudo "${pkgman}" --sync
  98. echo "正在安装所需的包..."
  99. sudo "${pkgman}" net-misc/curl net-misc/wget net-misc/bridge-utils net-dns/dnsmasq sys-apps/diffutils dev-util/pkgconf sys-apps/which app-arch/unzip sys-apps/util-linux sys-fs/dosfstools sys-devel/gcc dev-build/make sys-devel/flex sys-apps/texinfo dev-libs/gmp dev-libs/mpfr app-emulation/qemu dev-libs/mpc dev-libs/openssl
  100. }
  101. install_archlinux_pkg()
  102. {
  103. pkgman="pacman"
  104. echo "检测到 ArchLinux"
  105. echo "正在更新包管理器的列表..."
  106. sudo "${pkgman}" -Sy
  107. echo "正在安装所需的包..."
  108. sudo "${pkgman}" -S --needed --noconfirm \
  109. curl wget bridge-utils dnsmasq \
  110. diffutils pkgconf which unzip util-linux dosfstools \
  111. gcc make flex texinfo gmp mpfr qemu-base \
  112. libmpc openssl
  113. }
  114. install_centos_pkg()
  115. {
  116. echo "检测到 Centos/Fedora/RHEL 8"
  117. echo "正在更新包管理器的列表..."
  118. sudo dnf update -y
  119. echo "正在安装所需的包"
  120. echo "正在安装Development Tools..."
  121. sudo dnf groupinstall -y "Development Tools"
  122. echo "正在安装LLVM和Clang..."
  123. sudo dnf install -y llvm-devel clang-devel
  124. echo "正在安装Clang和GCC..."
  125. sudo dnf install -y clang gcc-c++
  126. echo "正在安装QEMU和KVM..."
  127. sudo dnf install -y qemu qemu-kvm qemu-system-x86
  128. echo "正在安装fdisk和redhat-lsb-core..."
  129. sudo dnf install -y util-linux redhat-lsb-core
  130. echo "正在安装Git..."
  131. sudo dnf install -y git
  132. echo "正在安装dosfstools..."
  133. sudo dnf install -y dosfstools
  134. echo "正在安装unzip..."
  135. sudo dnf install -y unzip
  136. echo "安装bridge utils"
  137. sudo dnf install -y bridge-utils || sudo rpm -ivh http://mirror.centos.org/centos/7/os/x86_64/Packages/bridge-utils-1.5-9.el7.x86_64.rpm #Centos8 需要直接安装Binary
  138. echo "安装dnsmasq"
  139. sudo dnf install -y dnsmasq
  140. }
  141. install_osx_pkg()
  142. {
  143. echo "Detected OSX! 暂不支持Mac OSX的一键安装!"
  144. exit 1
  145. }
  146. ####################################################################################
  147. # This function takes care of everything associated to rust, and the version manager
  148. # That controls it, it can install rustup and uninstall multirust as well as making
  149. # sure that the correct version of rustc is selected by rustup
  150. ####################################################################################
  151. rustInstall() {
  152. # Check to see if multirust is installed, we don't want it messing with rustup
  153. # In the future we can probably remove this but I believe it's good to have for now
  154. if [ -e /usr/local/lib/rustlib/uninstall.sh ] ; then
  155. echo "您的系统上似乎安装了multirust。"
  156. echo "该工具已被维护人员弃用,并将导致问题。"
  157. echo "如果您愿意,此脚本可以从系统中删除multirust"
  158. printf "卸载 multirust (y/N):"
  159. read multirust
  160. if echo "$multirust" | grep -iq "^y" ;then
  161. sudo /usr/local/lib/rustlib/uninstall.sh
  162. else
  163. echo "请手动卸载multistrust和任何其他版本的rust,然后重新运行bootstrap.sh"
  164. exit
  165. fi
  166. fi
  167. # If rustup is not installed we should offer to install it for them
  168. if [ -z "$(which rustup)" ]; then
  169. echo "正在安装Rust..."
  170. #install rustup
  171. curl https://sh.rustup.rs -sSf --retry 5 --retry-delay 5 | sh -s -- --default-toolchain ${RUST_VERSION} -y
  172. # You have to add the rustup variables to the $PATH
  173. echo "export PATH=\"\$HOME/.cargo/bin:\$PATH\"" >> ~/.bashrc
  174. # source the variables so that we can execute rustup commands in the current shell
  175. source ~/.cargo/env
  176. source "$HOME/.cargo/env"
  177. fi
  178. #
  179. if [ -z "$(which rustc)" ]; then
  180. echo "Rust 还未被安装"
  181. echo "请再次运行脚本,接受rustup安装"
  182. echo "或通过以下方式手动安装rustc(不推荐):"
  183. echo "curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain $RUST_VERSION -y"
  184. exit
  185. else
  186. local change_rust_src=""
  187. if [ "$DEFAULT_INSTALL" = "true" ]; then
  188. change_rust_src="true"
  189. else
  190. echo "是否为Rust换源为国内镜像源?(Tuna)"
  191. echo "如果您在国内,我们推荐您这样做,以提升网络速度。"
  192. echo "*WARNING* 这将会替换原有的镜像源设置。"
  193. printf "(y/N): "
  194. read change_src
  195. if echo "$change_src" | grep -iq "^y" ;then
  196. change_rust_src="true"
  197. else
  198. echo "取消换源,您原有的配置不会被改变。"
  199. fi
  200. fi
  201. if [ "$change_rust_src" = "true" ]; then
  202. echo "正在为rust换源"
  203. bash change_rust_src.sh --sparse
  204. fi
  205. echo "正在安装DragonOS所需的rust组件...首次安装需要一些时间来更新索引,请耐心等待..."
  206. cargo install cargo-binutils
  207. cargo install bpf-linker
  208. rustup toolchain install $RUST_VERSION-x86_64-unknown-linux-gnu
  209. rustup toolchain install $RUST_VERSION_OLD-x86_64-unknown-linux-gnu
  210. rustup component add rust-src --toolchain $RUST_VERSION-x86_64-unknown-linux-gnu
  211. rustup component add rust-src --toolchain $RUST_VERSION_OLD-x86_64-unknown-linux-gnu
  212. rustup target add x86_64-unknown-none --toolchain $RUST_VERSION-x86_64-unknown-linux-gnu
  213. rustup target add x86_64-unknown-none --toolchain $RUST_VERSION_OLD-x86_64-unknown-linux-gnu
  214. rustup target add x86_64-unknown-linux-musl --toolchain $RUST_VERSION-x86_64-unknown-linux-gnu
  215. rustup target add x86_64-unknown-linux-musl --toolchain $RUST_VERSION_OLD-x86_64-unknown-linux-gnu
  216. rustup toolchain install $RUST_VERSION-riscv64gc-unknown-linux-gnu --force-non-host
  217. rustup toolchain install $RUST_VERSION_OLD-riscv64gc-unknown-linux-gnu --force-non-host
  218. rustup target add riscv64gc-unknown-none-elf --toolchain $RUST_VERSION-riscv64gc-unknown-linux-gnu
  219. rustup target add riscv64imac-unknown-none-elf --toolchain $RUST_VERSION-riscv64gc-unknown-linux-gnu
  220. rustup target add riscv64gc-unknown-none-elf --toolchain $RUST_VERSION_OLD-riscv64gc-unknown-linux-gnu
  221. rustup target add riscv64imac-unknown-none-elf --toolchain $RUST_VERSION_OLD-riscv64gc-unknown-linux-gnu
  222. rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
  223. rustup component add rust-src
  224. rustup component add llvm-tools-preview
  225. rustup default $RUST_VERSION
  226. echo "Rust已经成功的在您的计算机上安装!请运行 source ~/.cargo/env 以使rust在当前窗口生效!"
  227. fi
  228. }
  229. install_python_pkg()
  230. {
  231. echo "正在安装python依赖项..."
  232. # 安装文档生成工具
  233. sh -c "cd ../docs && pip3 install -r requirements.txt"
  234. }
  235. ############# 脚本开始 ##############
  236. # 读取参数及选项,使用 -help 参数查看详细选项
  237. while true; do
  238. if [ -z "$1" ]; then
  239. break;
  240. fi
  241. echo "repeat"
  242. case "$1" in
  243. "--no-docker")
  244. dockerInstall=""
  245. ;;
  246. "--default")
  247. DEFAULT_INSTALL="true"
  248. dockerInstall=""
  249. ;;
  250. "--help")
  251. echo "--no-docker(not install docker): 该参数表示执行该脚本的过程中不单独安装docker."
  252. exit 0
  253. ;;
  254. *)
  255. echo "无法识别参数$1, 请传入 --help 参数查看提供的选项."
  256. ;;
  257. esac
  258. shift 1
  259. done
  260. ############ 开始执行 ###############
  261. banner # 开始横幅
  262. if [ "Darwin" == "$(uname -s)" ]; then
  263. install_osx_pkg "$emulator" || exit 1
  264. else
  265. # Here we will use package managers to determine which operating system the user is using.
  266. # Suse and derivatives
  267. if hash 2>/dev/null zypper; then
  268. suse "$emulator" || exit 1
  269. # Debian or any derivative of it
  270. elif hash 2>/dev/null apt-get; then
  271. install_ubuntu_debian_pkg "$defpackman" || exit 1
  272. # Fedora
  273. elif hash 2>/dev/null dnf; then
  274. install_centos_pkg || exit 1
  275. # Gentoo
  276. elif hash 2>/dev/null emerge; then
  277. gentoo "$emulator" || exit 1
  278. # SolusOS
  279. elif hash 2>/dev/null eopkg; then
  280. solus "$emulator" || exit 1
  281. # Arch linux
  282. elif hash 2>/dev/null pacman; then
  283. install_archlinux_pkg || exit 1
  284. # FreeBSD
  285. elif hash 2>/dev/null pkg; then
  286. freebsd "$emulator" || exit 1
  287. # Unsupported platform
  288. else
  289. printf "\e[31;1mFatal error: \e[0;31mUnsupported platform, please open an issue\[0m" || exit 1
  290. fi
  291. fi
  292. # 安装rust
  293. rustInstall
  294. install_python_pkg
  295. # 安装dadk
  296. cargo install dadk || exit 1
  297. bashpath=$(cd `dirname $0`; pwd)
  298. # 编译安装musl交叉编译工具链
  299. $SHELL ${bashpath}/install_musl_gcc.sh || (echo "musl交叉编译工具链安装失败" && exit 1)
  300. # 编译安装grub
  301. $SHELL ${bashpath}/grub_auto_install.sh || (echo "grub安装失败" && exit 1)
  302. # 解决kvm权限问题
  303. USR=$USER
  304. sudo groupadd kvm || echo "kvm组已存在"
  305. sudo usermod -aG kvm $USR
  306. sudo chown $USR /dev/kvm
  307. congratulations