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