bootstrap.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "| make run-docker -j 你的cpu核心数 |"
  21. echo "| |"
  22. echo "|------------------------------------------|"
  23. }
  24. ####################################
  25. # 当检测到ubuntu或Debian时,执行此函数 #
  26. # 参数:第一个参数为包管理器 #
  27. ####################################
  28. install_ubuntu_debian_pkg()
  29. {
  30. echo "检测到 Ubuntu/Debian"
  31. echo "正在更新包管理器的列表..."
  32. sudo "$1" update
  33. echo "正在安装所需的包..."
  34. sudo "$1" install -y \
  35. ca-certificates \
  36. curl \
  37. gnupg \
  38. lsb-release \
  39. llvm-dev libclang-dev clang gcc-multilib \
  40. gcc build-essential fdisk dosfstools
  41. if [ -z "$(which docker)" ] && [ -n ${dockerInstall} ]; then
  42. echo "正在安装docker..."
  43. sudo mkdir -p /etc/apt/keyrings
  44. curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  45. echo \
  46. "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  47. $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  48. sudo $1 update
  49. sudo "$1" install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
  50. elif [ -z ${dockerInstall} ]; then
  51. echo "您传入--no-docker参数生效, 安装docker步骤被跳过."
  52. elif [ -n "$(which docker)" ]; then
  53. echo "您的计算机上已经安装了docker"
  54. fi
  55. if [ -z "$(which qemu-system-x86_64)" ]; then
  56. echo "正在安装QEMU虚拟机..."
  57. sudo $1 install -y qemu qemu-system qemu-kvm
  58. else
  59. echo "QEMU已经在您的电脑上安装!"
  60. fi
  61. }
  62. install_osx_pkg()
  63. {
  64. echo "Detected OSX! 暂不支持Mac OSX的一键安装!"
  65. exit 1
  66. }
  67. ####################################################################################
  68. # This function takes care of everything associated to rust, and the version manager
  69. # That controls it, it can install rustup and uninstall multirust as well as making
  70. # sure that the correct version of rustc is selected by rustup
  71. ####################################################################################
  72. rustInstall() {
  73. # Check to see if multirust is installed, we don't want it messing with rustup
  74. # In the future we can probably remove this but I believe it's good to have for now
  75. if [ -e /usr/local/lib/rustlib/uninstall.sh ] ; then
  76. echo "您的系统上似乎安装了multirust。"
  77. echo "该工具已被维护人员弃用,并将导致问题。"
  78. echo "如果您愿意,此脚本可以从系统中删除multirust"
  79. printf "卸载 multirust (y/N):"
  80. read multirust
  81. if echo "$multirust" | grep -iq "^y" ;then
  82. sudo /usr/local/lib/rustlib/uninstall.sh
  83. else
  84. echo "请手动卸载multistrust和任何其他版本的rust,然后重新运行bootstrap.sh"
  85. exit
  86. fi
  87. fi
  88. # If rustup is not installed we should offer to install it for them
  89. if [ -z "$(which rustup)" ]; then
  90. echo "您没有安装rustup,"
  91. echo "我们强烈建议使用rustup, 是否要立即安装?"
  92. echo "*WARNING* 这将会发起这样的一个命令 'curl | sh' "
  93. printf "(y/N): "
  94. read rustup
  95. if echo "$rustup" | grep -iq "^y" ;then
  96. #install rustup
  97. curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightly
  98. # You have to add the rustup variables to the $PATH
  99. echo "export PATH=\"\$HOME/.cargo/bin:\$PATH\"" >> ~/.bashrc
  100. # source the variables so that we can execute rustup commands in the current shell
  101. source ~/.cargo/env
  102. source "$HOME/.cargo/env"
  103. else
  104. echo "Rustup will not be installed!"
  105. fi
  106. fi
  107. #
  108. if [ -z "$(which rustc)" ]; then
  109. echo "Rust 还未被安装"
  110. echo "请再次运行脚本,接受rustup安装"
  111. echo "或通过以下方式手动安装rustc(不推荐):"
  112. echo "curl -sSf https://static.rust-lang.org/rustup.sh | sh -s -- --channel=nightly"
  113. exit
  114. else
  115. echo "是否为Rust换源为Gitee镜像源?"
  116. echo "如果您在国内,我们推荐您这样做,以提升网络速度。"
  117. echo "*WARNING* 这将会替换原有的镜像源设置。"
  118. printf "(y/N): "
  119. read change_src
  120. if echo "$change_src" | grep -iq "^y" ;then
  121. touch ~/.cargo/config
  122. bash change_rust_src.sh
  123. else
  124. echo "取消换源,您原有的配置不会被改变。"
  125. fi
  126. echo "正在安装DragonOS所需的rust组件...首次安装需要一些时间来更新索引,请耐心等待..."
  127. cargo install cargo-binutils
  128. rustup toolchain install nightly
  129. rustup default nightly
  130. rustup component add rust-src
  131. rustup component add llvm-tools-preview
  132. rustup target add x86_64-unknown-none
  133. echo "Rust已经成功的在您的计算机上安装!请运行 source ~/.cargo/env 以使rust在当前窗口生效!"
  134. fi
  135. }
  136. ############# 脚本开始 ##############
  137. # 读取参数及选项,使用 -help 参数查看详细选项
  138. while true; do
  139. if [ -z "$1" ]; then
  140. break;
  141. fi
  142. echo "repeat"
  143. case "$1" in
  144. "--no-docker")
  145. dockerInstall=""
  146. ;;
  147. "--help")
  148. echo "--no-docker(not install docker): 该参数表示执行该脚本的过程中不单独安装docker."
  149. exit 0
  150. ;;
  151. *)
  152. echo "无法识别参数$1, 请传入 --help 参数查看提供的选项."
  153. ;;
  154. esac
  155. shift 1
  156. done
  157. ############ 开始执行 ###############
  158. banner # 开始横幅
  159. rustInstall # 安装rust
  160. if [ "Darwin" == "$(uname -s)" ]; then
  161. install_osx_pkg "$emulator" || exit 1
  162. else
  163. # Here we will use package managers to determine which operating system the user is using.
  164. # Suse and derivatives
  165. if hash 2>/dev/null zypper; then
  166. suse "$emulator" || exit 1
  167. # Debian or any derivative of it
  168. elif hash 2>/dev/null apt-get; then
  169. install_ubuntu_debian_pkg "$defpackman" || exit 1
  170. # Fedora
  171. elif hash 2>/dev/null dnf; then
  172. fedora "$emulator" || exit 1
  173. # Gentoo
  174. elif hash 2>/dev/null emerge; then
  175. gentoo "$emulator" || exit 1
  176. # SolusOS
  177. elif hash 2>/dev/null eopkg; then
  178. solus "$emulator" || exit 1
  179. # Arch linux
  180. elif hash 2>/dev/null pacman; then
  181. archLinux "$emulator" || exit 1
  182. # FreeBSD
  183. elif hash 2>/dev/null pkg; then
  184. freebsd "$emulator" || exit 1
  185. # Unsupported platform
  186. else
  187. printf "\e[31;1mFatal error: \e[0;31mUnsupported platform, please open an issue\[0m" || exit 1
  188. fi
  189. fi
  190. # 创建磁盘镜像
  191. bash create_hdd_image.sh
  192. # 编译安装GCC交叉编译工具链
  193. bash build_gcc_toolchain.sh
  194. # 编译安装grub
  195. bash grub_auto_install.sh
  196. # 解决kvm权限问题
  197. USR=$USER
  198. sudo adduser $USR kvm
  199. sudo chown $USR /dev/kvm
  200. congratulations