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