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