xref: /DragonOS/tools/bootstrap.sh (revision 2f6f547ae05c19871138e558ba6943ff07f4c68c)
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
49    if [ -z "$(which docker)" ] && [ -n ${dockerInstall} ]; then
50        echo "正在安装docker..."
51        sudo apt install -y docker.io docker-compose
52    elif [ -z ${dockerInstall} ]; then
53		echo "您传入--no-docker参数生效, 安装docker步骤被跳过."
54	elif [ -n "$(which docker)" ]; then
55        echo "您的计算机上已经安装了docker"
56    fi
57
58    if [ -z "$(which qemu-system-x86_64)" ]; then
59        echo "正在安装QEMU虚拟机..."
60        sudo $1 install -y qemu-system qemu-kvm
61    else
62        echo "QEMU已经在您的电脑上安装!"
63    fi
64
65}
66
67install_archlinux_pkg()
68{
69    pkgman="pacman"
70    echo "检测到 ArchLinux"
71    echo "正在更新包管理器的列表..."
72    sudo "${pkgman}" -Sy
73    echo "正在安装所需的包..."
74    sudo "${pkgman}" -S --needed --noconfirm \
75	curl wget bridge-utils dnsmasq \
76        diffutils pkgconf which unzip util-linux dosfstools \
77        gcc make flex texinfo gmp mpfr qemu-base \
78        libmpc libssl-dev
79
80}
81
82install_osx_pkg()
83{
84    echo "Detected OSX! 暂不支持Mac OSX的一键安装!"
85    exit 1
86}
87
88####################################################################################
89# This function takes care of everything associated to rust, and the version manager
90# That controls it, it can install rustup and uninstall multirust as well as making
91# sure that the correct version of rustc is selected by rustup
92####################################################################################
93rustInstall() {
94	# Check to see if multirust is installed, we don't want it messing with rustup
95	# In the future we can probably remove this but I believe it's good to have for now
96	if [ -e /usr/local/lib/rustlib/uninstall.sh ] ; then
97		echo "您的系统上似乎安装了multirust。"
98		echo "该工具已被维护人员弃用,并将导致问题。"
99		echo "如果您愿意,此脚本可以从系统中删除multirust"
100		printf "卸载 multirust (y/N):"
101		read multirust
102		if echo "$multirust" | grep -iq "^y" ;then
103			sudo /usr/local/lib/rustlib/uninstall.sh
104		else
105			echo "请手动卸载multistrust和任何其他版本的rust,然后重新运行bootstrap.sh"
106			exit
107		fi
108	fi
109	# If rustup is not installed we should offer to install it for them
110	if [ -z "$(which rustup)" ]; then
111		echo "您没有安装rustup,"
112		echo "我们强烈建议使用rustup, 是否要立即安装?"
113		echo "*WARNING* 这将会发起这样的一个命令 'curl | sh' "
114		printf "(y/N): "
115		read rustup
116		if echo "$rustup" | grep -iq "^y" ;then
117			#install rustup
118			curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightly
119			# You have to add the rustup variables to the $PATH
120			echo "export PATH=\"\$HOME/.cargo/bin:\$PATH\"" >> ~/.bashrc
121			# source the variables so that we can execute rustup commands in the current shell
122			source ~/.cargo/env
123			source "$HOME/.cargo/env"
124		else
125			echo "Rustup will not be installed!"
126		fi
127	fi
128	#
129	if [ -z "$(which rustc)" ]; then
130		echo "Rust 还未被安装"
131		echo "请再次运行脚本,接受rustup安装"
132		echo "或通过以下方式手动安装rustc(不推荐):"
133		echo "curl -sSf https://static.rust-lang.org/rustup.sh | sh -s -- --channel=nightly"
134		exit
135	else
136        echo "是否为Rust换源为国内镜像源?(Tuna)"
137		echo "如果您在国内,我们推荐您这样做,以提升网络速度。"
138		echo "*WARNING* 这将会替换原有的镜像源设置。"
139		printf "(y/N): "
140		read change_src
141		if echo "$change_src" | grep -iq "^y" ;then
142			touch ~/.cargo/config
143			bash change_rust_src.sh
144		else
145			echo "取消换源,您原有的配置不会被改变。"
146		fi
147        echo "正在安装DragonOS所需的rust组件...首次安装需要一些时间来更新索引,请耐心等待..."
148        cargo install cargo-binutils
149        rustup toolchain install nightly
150        rustup default nightly
151		rustup toolchain install nightly-2023-01-21-x86_64-unknown-linux-gnu
152		rustup toolchain install nightly-2023-08-15-x86_64-unknown-linux-gnu
153		rustup component add rust-src --toolchain nightly-2023-01-21-x86_64-unknown-linux-gnu
154		rustup component add rust-src --toolchain nightly-2023-08-15-x86_64-unknown-linux-gnu
155        rustup component add rust-src
156		rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
157        rustup component add llvm-tools-preview
158		rustup target add x86_64-unknown-none
159
160		echo "Rust已经成功的在您的计算机上安装!请运行 source ~/.cargo/env 以使rust在当前窗口生效!"
161	fi
162}
163
164
165############# 脚本开始 ##############
166# 读取参数及选项,使用 -help 参数查看详细选项
167while true; do
168	if [ -z "$1" ]; then
169		break;
170	fi
171	echo "repeat"
172	case "$1" in
173		"--no-docker")
174			dockerInstall=""
175		;;
176		"--help")
177			echo "--no-docker(not install docker): 该参数表示执行该脚本的过程中不单独安装docker."
178			exit 0
179		;;
180		*)
181			echo "无法识别参数$1, 请传入 --help 参数查看提供的选项."
182		;;
183	esac
184	shift 1
185done
186
187############ 开始执行 ###############
188banner 			# 开始横幅
189
190if [ "Darwin" == "$(uname -s)" ]; then
191	install_osx_pkg "$emulator" || exit 1
192else
193	# Here we will use package managers to determine which operating system the user is using.
194
195	# Suse and derivatives
196	if hash 2>/dev/null zypper; then
197		suse "$emulator" || exit 1
198	# Debian or any derivative of it
199	elif hash 2>/dev/null apt-get; then
200		install_ubuntu_debian_pkg "$defpackman"  || exit 1
201	# Fedora
202	elif hash 2>/dev/null dnf; then
203		fedora "$emulator" || exit 1
204	# Gentoo
205	elif hash 2>/dev/null emerge; then
206		gentoo "$emulator" || exit 1
207	# SolusOS
208	elif hash 2>/dev/null eopkg; then
209		solus "$emulator" || exit 1
210	# Arch linux
211	elif hash 2>/dev/null pacman; then
212		install_archlinux_pkg || exit 1
213	# FreeBSD
214	elif hash 2>/dev/null pkg; then
215		freebsd "$emulator" || exit 1
216	# Unsupported platform
217	else
218    	printf "\e[31;1mFatal error: \e[0;31mUnsupported platform, please open an issue\[0m" || exit 1
219	fi
220fi
221
222rustInstall     # 安装rust
223
224# 安装dadk
225cargo install dadk || exit 1
226
227# 创建磁盘镜像
228bash create_hdd_image.sh
229# 编译安装GCC交叉编译工具链
230bash build_gcc_toolchain.sh -cs
231# 编译安装grub
232bash grub_auto_install.sh
233
234# 解决kvm权限问题
235USR=$USER
236sudo adduser $USR kvm
237sudo chown $USR /dev/kvm
238
239congratulations
240