xref: /DragonOS/tools/bootstrap.sh (revision 236e88d5ef24aae4c5f6ad424404f7cbd57e42b3)
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-01-21-x86_64-unknown-linux-gnu
232		rustup toolchain install nightly-2023-08-15-x86_64-unknown-linux-gnu
233		rustup component add rust-src --toolchain nightly-2023-01-21-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-2023-01-21-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
239		rustup toolchain install nightly-2023-01-21-riscv64gc-unknown-linux-gnu --force-non-host
240		rustup toolchain install nightly-2023-08-15-riscv64gc-unknown-linux-gnu --force-non-host
241		rustup target add riscv64gc-unknown-none-elf --toolchain nightly-2023-01-21-riscv64gc-unknown-linux-gnu
242		rustup target add riscv64imac-unknown-none-elf --toolchain nightly-2023-01-21-riscv64gc-unknown-linux-gnu
243		rustup target add riscv64gc-unknown-none-elf --toolchain nightly-2023-08-15-riscv64gc-unknown-linux-gnu
244		rustup target add riscv64imac-unknown-none-elf --toolchain nightly-2023-08-15-riscv64gc-unknown-linux-gnu
245
246		rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
247		rustup component add rust-src
248        rustup component add llvm-tools-preview
249		rustup default nightly
250
251		echo "Rust已经成功的在您的计算机上安装!请运行 source ~/.cargo/env 以使rust在当前窗口生效!"
252	fi
253}
254
255####################################################################################
256# 初始化DragonOS的musl交叉编译工具链
257# 主要是把musl交叉编译工具链的rcrt1.o替换为crt1.o (因为rust的rcrt1.o会使用动态链接的解释器,但是DragonOS目前尚未把它加载进来)
258#
259# 为DragonOS开发应用的时候,请使用 `cargo +nightly-2023-08-15-x86_64-unknown-linux-gnu build --target x86_64-unknown-linux-musl` 来编译
260# 	这样编译出来的应用将能二进制兼容DragonOS
261####################################################################################
262initialize_userland_musl_toolchain()
263{
264	fork_toolchain_from="nightly-2023-08-15-x86_64-unknown-linux-gnu"
265	custom_toolchain="nightly-2023-08-15-x86_64-unknown-linux_dragonos-gnu"
266	custom_toolchain_dir="$(dirname $(rustc --print sysroot))/${custom_toolchain}"
267	# 如果目录为空
268	if [ ! -d "${custom_toolchain_dir}" ]; then
269		echo "Custom toolchain does not exist, creating..."
270		rustup toolchain install ${fork_toolchain_from}
271		rustup component add --toolchain ${fork_toolchain_from} rust-src
272		rustup target add --toolchain ${fork_toolchain_from} x86_64-unknown-linux-musl
273		cp -r $(dirname $(rustc --print sysroot))/${fork_toolchain_from} ${custom_toolchain_dir}
274		self_contained_dir=${custom_toolchain_dir}/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained
275		cp -f ${self_contained_dir}/crt1.o ${self_contained_dir}/rcrt1.o
276	else
277		echo "Custom toolchain already exists."
278	fi
279
280}
281
282
283install_python_pkg()
284{
285	echo "正在安装python依赖项..."
286	# 安装文档生成工具
287	sh -c "cd ../docs && pip3 install -r requirements.txt"
288}
289
290
291############# 脚本开始 ##############
292# 读取参数及选项,使用 -help 参数查看详细选项
293while true; do
294	if [ -z "$1" ]; then
295		break;
296	fi
297	echo "repeat"
298	case "$1" in
299		"--no-docker")
300			dockerInstall=""
301		;;
302		"--help")
303			echo "--no-docker(not install docker): 该参数表示执行该脚本的过程中不单独安装docker."
304			exit 0
305		;;
306		*)
307			echo "无法识别参数$1, 请传入 --help 参数查看提供的选项."
308		;;
309	esac
310	shift 1
311done
312
313############ 开始执行 ###############
314banner 			# 开始横幅
315
316if [ "Darwin" == "$(uname -s)" ]; then
317	install_osx_pkg "$emulator" || exit 1
318else
319	# Here we will use package managers to determine which operating system the user is using.
320
321	# Suse and derivatives
322	if hash 2>/dev/null zypper; then
323		suse "$emulator" || exit 1
324	# Debian or any derivative of it
325	elif hash 2>/dev/null apt-get; then
326		install_ubuntu_debian_pkg "$defpackman"  || exit 1
327	# Fedora
328	elif hash 2>/dev/null dnf; then
329		install_centos_pkg || exit 1
330	# Gentoo
331	elif hash 2>/dev/null emerge; then
332		gentoo "$emulator" || exit 1
333	# SolusOS
334	elif hash 2>/dev/null eopkg; then
335		solus "$emulator" || exit 1
336	# Arch linux
337	elif hash 2>/dev/null pacman; then
338		install_archlinux_pkg || exit 1
339	# FreeBSD
340	elif hash 2>/dev/null pkg; then
341		freebsd "$emulator" || exit 1
342	# Unsupported platform
343	else
344    	printf "\e[31;1mFatal error: \e[0;31mUnsupported platform, please open an issue\[0m" || exit 1
345	fi
346fi
347
348# 安装rust
349rustInstall
350
351
352#  初始化DragonOS的musl交叉编译工具链
353initialize_userland_musl_toolchain
354install_python_pkg
355
356# 安装dadk
357cargo install dadk || exit 1
358
359bashpath=$(cd `dirname $0`; pwd)
360
361# 创建磁盘镜像
362bash ${bashpath}/create_hdd_image.sh
363# 编译安装GCC交叉编译工具链
364bash ${bashpath}/build_gcc_toolchain.sh -cs -kb -kg || (echo "GCC交叉编译工具链安装失败" && exit 1)
365# 编译安装musl交叉编译工具链
366bash ${bashpath}/install_musl_gcc.sh || (echo "musl交叉编译工具链安装失败" && exit 1)
367# 编译安装grub
368bash ${bashpath}/grub_auto_install.sh || (echo "grub安装失败" && exit 1)
369
370# 解决kvm权限问题
371USR=$USER
372sudo adduser $USR kvm
373sudo chown $USR /dev/kvm
374
375congratulations
376