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