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