Total Pageviews

Friday, 5 October 2018

在OpenVZ vps上,Alpine Linux的一键安装脚本

Alpine Linux是一个社区开发的面向安全应用的轻量级Linux发行版操作系统,占用资源很少,初始状态基本只占用几M内存和几十M硬盘,而且还很稳定,适合很多小型服务器和设备使用,这里就分享一个OVZ的Alpine脚本,几秒安装完成。

安装

说明:脚本在CentOSDebianUbuntu大多数版本上测试通过,且仅支持OpenVZ.
脚本代码:
#!/bin/sh -e
# 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

server=http://images.linuxcontainers.org
path=$(wget -O- ${server}/meta/1.0/index-system | \
grep -v edge | awk '-F;' '($1=="alpine" && $3=="amd64") {print $NF}' | tail -1)

cd /
mkdir /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 add openssh
echo PermitRootLogin yes >> /etc/ssh/sshd_config
rc-update add sshd default
rc-update add mdev sysinit
rc-update add devfs sysinit
#sh # (for example, run `passwd`)

sync
reboot -f
复制以上代码在VPS创建一个alpine.sh文件粘贴进去然后保存,然后执行bash alpine.sh
等待几秒钟完成,然后使用原密码进入Alpine系统。
下面的截图为系统资源占用情况,可以说很低了。
如果服务器不停的修改/etc/inittab并添加一堆重新生成的getty,则运行命令:
apk add e2fsprogs-extra
chattr +i /etc/inittab
--------

Converts your OpenVZ VPS to Alpine Linux

For some reasons, I still have to manage a handful of OpenVZ VPS. But I don't like the popular OSes that most providers offer, because they could be messy, heavy and outdated. I used to use vps2arch, which wipes out the original OS, and installs Arch Linux. But now that Arch Linux no longer supports the old VZ kernels, Alpine Linux seems to be a sensible choice.
So I made a script to install the latest Alpine Linux on OpenVZ VPS.
NOTE: The script will wipe all your data in the VPS! It only supports OpenVZ, because there usually are better ways to install your favourite OS on KVM. IPv6 is not supported, and should be manually configured. Vanilla Alpine Linux uses about 8MB RAM and 36MB disk, and it is systemd-free.
I tested it on vanilla 64-bit CentOS 7, Debian 8, and Ubuntu 16.04 images on an NAT OpenVZ VPS provided by @Cam (thanks!). It may or may not work on your OpenVZ however, so be prepared to reinstall OS and improvise。
from https://www.lowendtalk.com/discussion/149318/converts-your-openvz-vps-to-alpine-linux

No comments:

Post a Comment