bootstrap.sh 9.7 KB

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