bootstrap.sh 11 KB

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