-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrun-v2v.sh
executable file
·244 lines (218 loc) · 5.95 KB
/
run-v2v.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
#!/bin/bash -x
set -e
QUAY_ORG=kubevirt
VM_NAME=${1}
USER=${2}
PASS=${3}
URI=${4}
NS=${5}
OS=${6:-linux}
TYPE=${7:-vm}
die() { echo $@ >&2 ; exit 1 ; }
urlencode() {
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:${pos}:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}"
}
[[ "$VM_NAME" ]] || die "No vm name given"
[[ "$USER" ]] || die "No username given"
[[ "$PASS" ]] || die "No password given"
[[ "$URI" ]] || die "No source uri given"
DOMXML=$VM_NAME.xml
ENCODED_USER=$(urlencode "$USER")
# create libvirt auth file
tee libvirt.auth <<EOF
[credentials-vmware]
authname=$USER
password=$PASS
[auth-esx-default]
credentials=vmware
EOF
# get vm domxml
export LIBVIRT_AUTH_FILE=libvirt.auth
virsh -c 'vpx://'$ENCODED_USER'@'$URI'?no_verify=1' dumpxml $VM_NAME >> $DOMXML
rm -f libvirt.auth
if [ ! -f $DOMXML ];
then
die "Requested vm do not exists"
fi
# verify domxml for one disk and one nic
if [[ $(xmllint --xpath "count(//disk[@type='file']/source)" $DOMXML) -gt 1 ]];
then
die "Only one disk per VM is supported ATM"
fi
if [[ $(xmllint --xpath "count(//interface[@type='bridge']/source)" $DOMXML) -gt 1 ]];
then
die "Only one interface per VM is supported"
fi
# get disk location from domxml
dcPath=$(xmllint --xpath "/domain/*[local-name()='datacenterpath']/text()" $DOMXML)
diskSource=($(xmllint --xpath "string(//disk/source/@file)" $DOMXML))
rm -f $DOMXML
dsName=${diskSource[0]:1:-1}
fileName=$(echo ${diskSource[1]} | sed 's/\./-flat./g')
host=$(echo $URI | cut -d'/' -f-1)
# build disk uri
diskUri='https://'$host'/folder/'$fileName'?dcPath='$dcPath'&dsName='$dsName
# authenticate with vmware
curl --cookie-jar cookies.txt -q --max-redirs '5' --globoff --head --url $diskUri --user $USER':'$PASS --insecure
cookie=$(tail -n 1 cookies.txt | cut -f 7 | sed 's/"/\\"/g')
rm -f cookies.txt
# get disk size
qemu-img info 'json: { "file.cookie": "vmware_soap_session='$cookie'", "file.sslverify": "off", "file.driver": "https", "file.url": "'$diskUri'", "file.timeout": 2000 }' > output.txt
size=$(grep -Eow "[0-9]+[\.]?[0-9]+[MG]" output.txt)'i'
rm -f output.txt
# we support only one disk, it needs to be aligned with DNS-1123
PVCNAME=$(echo $VM_NAME-disk-01 | sed -r 's/[_.]+/-/g')
VMNAME=$(echo $VM_NAME | sed -r 's/[_.]+/-/g')
ENCODED_PASS=$(echo -n $PASS | base64)
# Create job template
tee template.yaml <<EOY
apiVersion: v1
kind: Template
metadata:
name: v2v-job-template
annotations:
openshift.io/display-name: "KubeVirt v2v $VM_NAME Import"
description: |
A template to trigger a v2v job in order to import a VM from a remote
vmware source into KubeVirt.
Example
libvirt $VM_NAME vpx://$USER@$URI?no_verify=1
parameters:
- name: SOURCE_TYPE
description: "The VM source for this job (libvirt) see man virt-v2v"
value: "ova"
- name: SOURCE_NAME
decsription: "The name of the VM to import (name or URL)"
value: "http://192.168.42.1/my.ova"
- name: SOURCE_URI
description: "(Optional) The URI to connect to the remote instance"
- name: OS_TYPE
description: "(Optional) OS type of the VM about to be imported"
- name: IMAGE_TYPE
description: "(Optional) Specify whether to import VM as a VM or a template"
objects:
- apiVersion: v1
kind: Secret
metadata:
name: v2v-secret
type: Opaque
data:
password: $ENCODED_PASS
- apiVersion: v1
kind: ServiceAccount
metadata:
name: kubevirt-privileged
- apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
name: kubevirt-v2v
labels:
kubevirt.io: ""
rules:
- apiGroups:
- kubevirt.io
resources:
- virtualmachines
verbs:
- get
- list
- watch
- delete
- update
- create
- deletecollection
- apiVersion: authorization.openshift.io/v1
kind: ClusterRoleBinding
metadata:
name: v2v-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kubevirt-v2v
subjects:
- kind: ServiceAccount
name: kubevirt-privileged
namespace: $NS
- kind: Job
apiVersion: batch/v1
metadata:
name: v2v-$VMNAME
spec:
backoffLimit: 1
template:
spec:
serviceAccountName: kubevirt-privileged
restartPolicy: Never
containers:
- name: v2v
image: quay.io/$QUAY_ORG/v2v-job
args: ["/v2v-dst",
"\${SOURCE_TYPE}",
"\${SOURCE_NAME}",
"\${SOURCE_URI}",
"\${OS_TYPE}",
"\${IMAGE_TYPE}",
"\${size}]",]
env:
- name: "DEBUG"
value: "1"
- name: SOURCE_PASSWORD
valueFrom:
secretKeyRef:
name: v2v-secret
key: password
securityContext:
privileged: true
volumeMounts:
- name: kvm
mountPath: /dev/kvm
- name: volume-1
mountPath: /v2v-dst
- name: volume-2
mountPath: /var/tmp
volumes:
- name: kvm
hostPath:
path: /dev/kvm
- name: volume-1
persistentVolumeClaim:
claimName: $PVCNAME
- name: volume-2
persistentVolumeClaim:
claimName: temp
- kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: $PVCNAME
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: $size
- kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: temp
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1100Mi
EOY
# create the job
oc process --local -f template.yaml -p SOURCE_TYPE=libvirt -p SOURCE_NAME=$VM_NAME -p SOURCE_URI=vpx://$ENCODED_USER@$URI?no_verify=1 -p OS_TYPE=$OS -p IMAGE_TYPE=$TYPE | oc apply -f -
rm -f template.yaml