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