bootstrap.sh 11 KB

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