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