-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-docker-proxmox-ct.sh
180 lines (168 loc) · 4.18 KB
/
convert-docker-proxmox-ct.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
#!/bin/bash
#####################################################
#
# Convert Docker container to Proxmox LCX CT
#
# Required
# TODO - auto install
# apt install skopeo umoci jq
#####################################################
# Settings
#----------------------------------------------------
# TODO - make this a command line
# first part of docker
USER=homeassistant
# sub docker
NAME=home-assistant
# version
VER=latest
# base os
BASEOS=
#
# container settings
#----------------------------------------------------
# Get the next guest VM/LXC ID
CTID=$(pvesh get /cluster/nextid)
# contiaer root passowrd
CTPASS=changeme
# Storage location of the container
STORAGE=local
# Bridge network
BR=vmbr0
# Container Memory size in megs
CTMEMORY=256
# Container Disk size in gigs
CTDISKSZ=10
# container network
#----------------------------------------------------
# ip of container
IP=192.168.1.40
# name server for the container
NS=192.168.1.1
# gateway for the container
GW=192.168.1.1
#Network settings for the container
NET=${IP}/24,gw=${GW}
# or NET=DHCP
CTNETLINE0="-net0 name=eth0,bridge=${BR},ip=${NET}"
#####################################################
# No need to below
#####################################################
# some color settings for script
R='\033[0;31m'
G='\033[0;32m'
B='\033[0;34m'
Y='\033[0;33m'
C='\033[0m' # No Color
# Internal vars
L=~/docker-pct.log
BACKUPDIRN=/var/lib/vz/template/cache
BACKUPLXCD=/var/lib/lxc/${NAME}
BACKUPNAME=${NAME}_template.tar.gz
CTTEMPLATE=${BACKUPDIRN}/${BACKUPNAME}
#check if defined and format correctly
[[ ! -z ${USER} ]] && USER=${USER}/
[[ ! -z ${BASEOS} ]] && CTOSTYPE=--OSTYPE ${BASEOS}
#####################################################
# Start
#####################################################
# clean log
rm ${L}
pct destroy ${CTID}
lxc-destroy ${CTID}
rm ${CTTEMPLATE}
#----------------------------------------------------
# look for existing container template
echo -e ${B}Checking for CT Template${c}
if [[ ! -f "${CTTEMPLATE}" ]]
then
# no template lets go
echo -e ${G}No CT Template found${C}
# look for lxc docker image
echo -e ${B}Checking for Standard LXC${C}
if [[ ! -d ${BACKUPLXCD} ]]
then
echo -e ${G}No Standard LXC Found${C}
#ok create lxc from docker
echo -e ${R}CREATE Standard LXC -- Please wait --${C}
echo -en ${Y}
echo lxc-create ${NAME} -t oci -- --url docker://${USER}${NAME}:${VER}
echo -en ${C}
lxc-create ${NAME} -t oci -- --url docker://${USER}${NAME}:${VER} >> $L 2>&1
#error check
if [ ! $? -eq 0 ]; then
echo -e ${R}ERRROR${C}
exit 2
fi
else
echo -e ${G}Found Standard LXC${C}
fi
# docker lxc now exists ready to backup
echo -e ${R}CREATE Proxmox LXC Template${C}
EXCLUDES=(
--exclude='sys'
--exclude='dev'
--exclude='run'
--exclude='proc'
--exclude='*.log'
--exclude='*.log*'
--exclude='*.gz'
--exclude='*.sql'
--exclude='swap.img'
)
# backup
echo -en ${Y}
echo tar -czf "${CTTEMPLATE}" -C "${BACKUPLXCD}/rootfs/" "${EXCLUDES[@]}" .
echo -en ${C}
tar -czf "${CTTEMPLATE}" -C "${BACKUPLXCD}/rootfs/" "${EXCLUDES[@]}" . >> $L 2>&1
#check backup
if [ $? -eq 0 ]; then
# backup made remove the docker lxc
echo -e ${G}CREATED ${CTTEMPLATE}
echo -e ${B}REMOVING Standard LXC${C}
echo -en ${Y}
echo lxc-destroy ${NAME}
echo -en ${C}
lxc-destroy ${NAME} >> $L 2>&1
# TODO - error check - assume ok
else
# backup failed
echo -e ${R}FAIL${C}
exit 2
fi
#
else
# found proxmox lxc template
echo -e ${R}FOUND TAR${C}: ${CTTEMPLATE}
fi
# proxmox lxc template exists now
# now make a proxmox lxc from the template we just made
echo -e ${R}CREATE Proxmox LXC${C}: ID = ${CTID}
PCTCREATE="
${CTID}
${BACKUPDIRN}/${BACKUPNAME}
${CTOSTYPE}
-description ${NAME}
-hostname ${NAME}
-nameserver ${NS}
${CTNETLINE0}
--rootfs ${CTDISKSZ} -memory ${CTMEMORY}
-storage ${STORAGE}
--features nesting=1
-password=${CTPASS}
"
echo -en ${Y}
echo pct create ${PCTCREATE}
echo -en ${C}
pct create ${PCTCREATE} >> $L 2>&1
# check to see if proxmox added the container
if [ $? -eq 0 ]; then
echo -e ${G}CREATED Proxmox LXC${C}: ${CTID}
# start container
pct start ${CTID} >> $L 2>&1
else
echo -e ${R}FAIL${C}
exit 2
fi
# connect to container console
lxc-attach -n ${CTID}