-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxc-convert-from-vserver
122 lines (99 loc) · 2.77 KB
/
lxc-convert-from-vserver
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
#!/bin/bash
LANG=C
cmdname=`basename $0`
tmpdir=`mktemp -d /tmp/${cmdname}.XXXXXX`
spool="${tmpdir}/spool"
routes="${tmpdir}/routes"
touch $routes $spool
function cleanup () {
rm -rf "${newtmpdir}"
}
gateway=`ip a |grep "scope global" | awk '{print$2}'|grep-ip |grep -v "255" |grep -v "^10."`
br=lxcbr0
debian_version=`lsb_release -cs`
function usage() {
cat <<EOF
Usage: ${cmdname}
This script will convert v-server to LXC.
Very many thanks for Herbert Poetzl for his v-servers for many years!
It is time to move to LXC :(
EOF
}
trap 'cleanup' EXIT
trap 'cleanup' SIGTERM
total_mem=`free -g | grep -i mem |awk '{print$2}'`;
total_cpu=`cat /proc/cpuinfo |grep processor |tail -2 |head -1 | awk '{print$3}'`
if [ -d "/var/lib/vservers" ]
then
vs_dir="/var/lib/vservers"
else
if [ -d "/vservers" ]
then
vs_dir="/vservers"
fi
fi
[ "$vs_dir" == "" ] && echo "No vservers found in /var/lib/vservers and /vservers" && exit 1
echo "Available vservers:"
for vs in `ls $vs_dir |grep -v 'vservers'`; do echo $vs; done
cat <<EOF
Enter vserver name to convert:
EOF
read vserver
echo "You selected: $vserver"
echo "Memory total: $total_mem"
echo "Please enter memory ammount for this LXC in Gigabites 4 [default]: "
read memory
[ "$memory" == "" ] && memory="4"
echo "CPU total cores: 0-$total_cpu"
echo "Please enter LXC CPU cores 0-1 [default]: "
read cpu
[ "$cpu" == "" ] && cpu="0-1"
echo "Please enter LXC IP: "
read ip
[ "$ip" == "" ] && echo "Error: empty IP" && exit 1;
#ok go ahead
mkdir -p /var/lib/lxc/${vserver}
ln -s ${vs_dir}/${vserver} /var/lib/lxc/${vserver}/rootfs
cat > /var/lib/lxc/${vserver}/config <<EOF
lxc.start.auto = 1
lxc.uts.name = ${vserver}
lxc.cgroup.memory.limit_in_bytes = ${memory}
lxc.cgroup.cpuset.cpus = ${cpus}
lxc.rootfs.path = /var/lib/lxc/${vserver}/rootfs
EOF
if [ "${debian_version}" == "bullseye" ]
then
cat >> /var/lib/lxc/${vserver}/config <<EOF
lxc.include = /usr/share/lxc/config/common.conf
EOF
else
cat >> /var/lib/lxc/${vserver}/config <<EOF
lxc.include = /usr/share/lxc/config/debian.common.conf
EOF
fi
cat >> /var/lib/lxc/${vserver}/config <<EOF
lxc.arch = amd64
lxc.autodev = 1
lxc.apparmor.profile = unconfined
lxc.cgroup.devices.allow = a
lxc.cap.drop=
lxc.mount.auto=proc:rw sys:rw
linux.kernel_modules = ip_tables,ip6_tables,br_netfilter,netlink_diag,nf_nat,overlay
security.privileged = 1
security.nesting = 1
lxc.mount.entry = /backup/lxc/${vserver} backup none bind,create=dir 0 0
lxc.net.0.type = veth
lxc.net.0.flags = up
lxc.net.0.name = eth0
lxc.net.0.link = ${br}
lxc.net.0.ipv4.address = ${ip}/32
lxc.net.0.ipv4.gateway = ${gateway}
EOF
mkdir -p /backup/lxc/${vserver}
ip route add ${ip}/32 dev ${br}
cat >> /etc/network/interfaces <<EOF
up ip route add ${ip}/32 dev ${br}
EOF
cat >> /etc/rc.local <<EOF
ipi ip route add ${ip}/32 dev ${br}
EOF