-
Notifications
You must be signed in to change notification settings - Fork 43
/
OsMutation.sh
269 lines (234 loc) · 9.79 KB
/
OsMutation.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/bash
# Reinstall Any OpenVZ/LXC VPS to Debian/CentOS/Alpine
# Author: Lloyd@nodeseek.com
# WARNING: A fresh system will be installed and all old data will be wiped.
# License: GPLv3; Partly based on https://gist.github.com/trimsj/c1fefd650b5f49ceb8f3efc1b6a1404d
function print_help(){
echo -ne "\e[1;32m"
cat <<- EOF
██████╗ ███████╗███╗ ███╗██╗ ██╗████████╗ █████╗ ████████╗██╗ ██████╗ ███╗ ██╗
██╔═══██╗██╔════╝████╗ ████║██║ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██║██╔═══██╗████╗ ██║
██║ ██║███████╗██╔████╔██║██║ ██║ ██║ ███████║ ██║ ██║██║ ██║██╔██╗ ██║
██║ ██║╚════██║██║╚██╔╝██║██║ ██║ ██║ ██╔══██║ ██║ ██║██║ ██║██║╚██╗██║
╚██████╔╝███████║██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ██║╚██████╔╝██║ ╚████║
╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝
Reinstall Any OpenVZ/LXC VPS to Debian/CentOS/Alpine;
[warning] A fresh system will be installed and all old data will be wiped!
Author: Lloyd@nodeseek.com
EOF
echo -ne "\e[m"
}
function read_virt_tech(){
cttype=$(virt-what | sed -n 1p)
if [[ $cttype == "lxc" || $cttype == "openvz" ]]; then
[[ $cttype == "lxc" ]] && echo -e '\e[1;33mYour container type: lxc\e[m' || echo -e '\e[1;33mYour container type: openvz\e[m'
else
while [ "$cttype" != 'lxc' -a "$cttype" != 'openvz' -a "$cttype" != 'kvm' ] ; do
echo -ne "\e[1;33mplease input container type (lxc/openvz/kvm):\e[m"
read cttype < /dev/tty
done
fi
}
function install(){
if [ -n "$(command -v apt)" ] ; then
cmd1="apt-get"
cmd2="apt-get install -y"
elif [ -n "$(command -v yum)" ] ; then
cmd1="yum"
cmd2="yum install -y"
elif [ -n "$(command -v dnf)" ] ; then
cmd1="dnf"
cmd2="dnf install -y"
elif [ -n "$(command -v apk)" ] ; then
cmd1="apk"
cmd2="apk add"
else
echo "Error: Not Supported Os"
exit 1
fi
$cmd1 update
$cmd2 "$@"
}
function read_lxc_template(){
last_lxc_version=$(curl -Ls "https://api.github.com/repos/LloydAsp/OsMutation/releases/latest" | grep "LXC" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [[ -n $last_lxc_version ]]; then
image_list=$(curl -Ls "https://api.github.com/repos/LloydAsp/OsMutation/releases/latest" | grep "LXC" | grep '"browser_download_url":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ "$(uname -m)" == "aarch64" ] ; then
image_list="$(echo "$image_list" | grep arm64)"
else
image_list="$(echo "$image_list" | grep -v arm64)"
fi
os_list=$(echo "$image_list" | sed "s/https\:\/\/github.com\/LloydAsp\/OsMutation\/releases\/download\/${last_lxc_version}\///g" | sed "s/\.tar\.gz//g")
echo "$os_list" | nl
while [ -z "${os_index##*[!0-9]*}" ]; do
echo -ne "\e[1;33mplease select os (input number):\e[m"
read os_index < /dev/tty
done
download_link=$(echo "$image_list" | head -n $os_index | tail -n 1)
else
server=http://images.linuxcontainers.org
path=$(wget -qO- ${server}/meta/1.0/index-system | \
grep -v edge | grep default | \
awk '-F;' '(( $1=="debian" || $1=="centos" || $1=="alpine") && ( $3=="amd64" || $3=="i386")) {print $NF}')
if [ "$(uname -m)" == "aarch64" ] ; then
path="$(echo $path | grep arm64)"
else
path="$(echo $path | grep -v arm64)"
fi
os_list=$( echo "$path" | sed -E 's%/images/(.*)/default/.*/%\1%g' | sed 's%/%-%g' )
echo "$os_list" | nl
while [ -z "${os_index##*[!0-9]*}" ]; do
echo -ne "\e[1;33mplease select os (input number):\e[m"
read os_index < /dev/tty
done
path=$( echo "$path" | head -n $os_index | tail -n 1)
os_selected=$(echo "$os_list" | head -n $os_index | tail -n 1 )
download_link=${server}/${path}/rootfs.tar.xz
fi
}
function read_openvz_template(){
releasetag="v0.0.1"
os_list=$(wget -qO- "https://github.com/LloydAsp/OsMutation/releases/expanded_assets/v0.0.1" | \
sed -nE '/tar.gz/s/.*>([^<>]+)\.tar\.gz.*/\1/p' | \
grep -E "(debian)|(centos)|(alpine)" )
echo "$os_list" | nl
while [ -z "${os_index##*[!0-9]*}" ];
do
echo -n "please select os (input number):"
read os_index < /dev/tty
done
os_selected=$( echo "$os_list" | head -n $os_index | tail -n 1)
download_link="https://github.com/LloydAsp/OsMutation/releases/download/${releasetag}/${os_selected}.tar.gz"
}
function download_rootfs(){
cd /
mkdir /x
if [ "$cttype" == 'lxc' ] ; then
#rootfs.tar.xz
wget -qO- $download_link | tar -C /x -xJv
elif [ "$cttype" == 'openvz' ] ; then
#rootfs.tar.gz
wget -qO- $download_link | tar -C /x -xzv
elif [ "$cttype" == 'kvm' ] ; then
echo "kvm is not supported by this script"
exit 1
fi
}
function migrate_configuration(){
# save root password and ssh directory
sed -i '/^root:/d' /x/etc/shadow
grep '^root:' /etc/shadow >> /x/etc/shadow
[ -d /root/.ssh ] && cp -a /root/.ssh /x/root/
# save network configuration
dev=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
[ -d /x/etc/network/ ] || mkdir -p /x/etc/network/
ipaddr_with_mask=$(ip addr show dev $dev | sed -nE '/global/s/.*inet (.+) brd.*$/\1/p' | head -n 1)
hostname=$(hostname)
route_part="$(ip route show default 0.0.0.0/0 | sed -E 's/^(.*dev [^ ]+).*$/\1/')"
gateway_line="up ip route add $route_part"
# manual save network
if [ -f /etc/network/interfaces ] && grep static /etc/network/interfaces > /dev/null ; then
cp -rf /etc/network/interfaces /x/etc/network/interfaces
else
cat > /x/etc/network/interfaces <<- EOF
auto lo
iface lo inet loopback
auto $dev
iface $dev inet static
address $ipaddr_with_mask
$gateway_line
hostname $hostname
EOF
fi
rm /x/etc/resolv.conf
cat > /x/etc/resolv.conf <<- EOF
nameserver 8.8.8.8
nameserver 2001:4860:4860::8888
EOF
}
function install_requirement(){
# prevent no access on ipv6 only vps
ping -c 3 api.github.com || echo "nameserver 2a00:1098:2c::1" > /etc/resolv.conf
if [ -n "$(command -v apk)" ] ; then
install curl sed gawk wget gzip xz tar virt-what
else
install curl sed gawk wget gzip xz-utils virt-what
fi
}
function chroot_run(){
if grep -qi alpine /x/etc/issue; then
chroot "/x/" sh -c "[ -f /bin/bash ] || apk add bash"
fi
chroot "/x/" /bin/bash -c "$*"
}
function replace_os(){
mkdir /x/oldroot
mount --bind / /x/oldroot
chroot_run 'cd /oldroot; '`
`'rm -rf $(ls /oldroot | grep -vE "(^dev|^proc|^sys|^run|^x)") ; '`
`'cd /; '`
`'mv -f $(ls / | grep -vE "(^dev|^proc|^sys|^run|^oldroot)") /oldroot'
umount /x/oldroot
}
function post_install(){
export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
if grep -qi alpine /etc/issue; then
install openssh bash
rc-update add sshd default
rc-update add mdev sysinit
rc-update add devfs sysinit
if [ "$cttype" == 'lxc' ] ; then
apk add ifupdown-ng
rc-update add networking default
sed -i 's/--auto/-a/' /etc/init.d/networking # fix bug in networking script of lxc
fi
elif grep -qi debian /etc/issue; then
install ssh
if [ "$cttype" == 'lxc' ] ; then
install ifupdown
systemctl disable systemd-networkd.service
fi
elif grep -qi centos /etc/issue; then
install openssh
if [ "$cttype" == 'lxc' ] ; then
install ifupdown
systemctl disable systemd-networkd.service
# To-Do: Network service of CentOS need modify
fi
fi
echo PermitRootLogin yes >> /etc/ssh/sshd_config
rm -rf /x
sync
while [ "$reboot_ans" != 'yes' -a "$reboot_ans" != 'no' ] ; do
echo -ne "\e[1;33mreboot now? (yes/no):\e[m"
read reboot_ans < /dev/tty
done
if [ "$reboot_ans" == 'yes' ] ; then
reboot -f
fi
}
function main(){
print_help
echo -e '\e[1;32minstall requirement...\e[m'
install_requirement
read_virt_tech
if [ "$cttype" == 'openvz' ] ; then
read_openvz_template
elif [ "$cttype" == 'lxc' ] ; then
read_lxc_template
elif [ "$cttype" == 'kvm' ] ; then
curl -qo OsMutationKvm.sh https://raw.githubusercontent.com/LloydAsp/OsMutation/main/OsMutationKvm.sh
chmod u+x OsMutationKvm.sh
./OsMutationKvm.sh
exit 0
fi
echo -e '\e[1;32mdownloading template...\e[m'
download_rootfs
echo -e '\e[1;32mmigrating configuration\e[m'
migrate_configuration
echo -e '\e[1;32mreplacing old files\e[m'
replace_os
echo -e '\e[1;32mpost processing...\e[m'
post_install
}
main 2>&1 | tee reinstall.log