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