Skip to content

Instantly share code, notes, and snippets.

@ehab2012
Last active Nov 5, 2022
Embed
What would you like to do?
#!/bin/sh -e
# apt-get install curl wget xz-utils apt-utils
# Converts OpenVZ VPS to Alpine Linux
# WARNING: This script will wipe any data in your VPS!
# GPLv2; Partly based on https://gitlab.com/drizzt/vps2arch
# https://gist.githubusercontent.com/pexcn/dc3ebe60393cc4ede40b30e180d40ac5/raw/8c2439d5d583dd40d5af3e570949b9f0974357ba/alpine.sh
version="3.16"
server=http://images.linuxcontainers.org
path=$(wget -O- ${server}/meta/1.0/index-system | grep -v $version | awk '-F;' '($1=="alpine" && $3=="amd64") {print $NF}' | tail -1)
# http://uk.lxd.images.canonical.com//images/alpine/3.15/amd64/default/20220101_13:00
#path="images/alpine/3.15/amd64/default/20220105_13:00"
cd /
mkdir -p /x
wget ${server}/${path}/rootfs.tar.xz
tar -C /x -xf rootfs.tar.xz
sed -i '/getty/d' /x/etc/inittab
sed -i 's/rc_sys="lxc"/rc_sys="openvz"/' /x/etc/rc.conf
# 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=venet0
ip=$(ip addr show dev $dev | grep global | awk '($1=="inet") {print $2}' | cut -d/ -f1 | head -1)
hostname=$(hostname)
cat > /x/etc/network/interfaces << EOF
auto lo
iface lo inet loopback
auto $dev
iface $dev inet static
address $ip
netmask 255.255.255.255
up ip route add default dev $dev
hostname $hostname
EOF
cp /etc/resolv.conf /x/etc/resolv.conf
# remove all old files and replace with alpine rootfs
find / \( ! -path '/dev/*' -and ! -path '/proc/*' -and ! -path '/sys/*' -and ! -path '/x/*' \) -delete || true
/x/lib/ld-musl-x86_64.so.1 /x/bin/busybox cp -a /x/* /
export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
rm -rf /x
apk update && apk upgrade
apk add bash util-linux python3 iproute2 curl mc nano rsync htop vnstat iperf3 nfs-utils e2fsprogs-extra wireguard-tools mtr ncdu mlocate
apk add gcc py3-pip python3-dev libc-dev linux-headers sysstat
apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/main --repository http://dl-cdn.alpinelinux.org/alpine/edge/community dropbear dropbear-scp dropbear-ssh
apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing wireguard-go
rc-update add dropbear default
rc-update add vnstatd default
rc-update add mdev sysinit
rc-update add devfs sysinit
rc-update add sysctl default
rc-update add iptables sysinit
sync
reboot -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment