-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxmox-change-vmid.sh
executable file
·132 lines (115 loc) · 3.69 KB
/
proxmox-change-vmid.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
#!/usr/bin/env bash
set -o errtrace
set -o functrace
set -o nounset
set -o pipefail
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
nocolor="\033[0m"
red="\033[0;31m"
green="\033[0;32m"
yellow="\033[0;33m"
blue="\033[0;34m"
magenta="\033[0;35m"
function log {
local -r level="$1"
local -r message="$2"
local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local -r script_name="$(basename "$0")"
>&2 echo -e "${timestamp} [${level}] [$script_name] ${message}"
}
function log_info {
local -r message="$1"
log "${green}INFO${nocolor}" "$message"
}
function log_warn {
local -r message="$1"
log "${yellow}WARN${nocolor}" "$message"
}
function log_error {
local -r message="$1"
log "${red}ERROR${nocolor}" "$message"
}
function status {
exit_code=$?
local -r message="$1"
if ! [[ ${exit_code} == 0 ]] ; then
log_info "${magenta}All went well with ${message}${nocolor}\n"
else
log_error "${red}Error: cannot complete ${message}${nocolor}\n"
fi
}
read -p "Enter current VM ID: " ORIGINAL_ID
read -p "Enter new VM ID: " NEW_ID
function vm_status {
local -r STATUS=$(qm status ${ORIGINAL_ID} | grep "stopped" | wc -l 2>/dev/null)
log_info "${magenta}Checking VM status${nocolor}"
if [[ ! -f /etc/pve/nodes/proxmox/qemu-server/${ORIGINAL_ID}.conf ]] ; then
log_error "${red}VM does not exist${nocolor}"
return 1
if [[ -f /etc/pve/nodes/proxmox/qemu-server/${NEW_ID}.conf ]] ; then
log_error "${red}It looks that new ID is already in use, please choose another one${nocolor}"
return 1
fi
elif [[ "$STATUS" == 1 ]] ; then
log_info "${green}VM exists and it is stopped, proceeding${nocolor}"
else
log_warn "${yellow}VM is running, stopping it${nocolor}"
qm stop ${ORIGINAL_ID} ; sleep 5
fi
}
function main {
if ! [[ -z ${ORIGINAL_ID} ]] && ! [[ -z ${NEW_ID} ]] ; then
cd /dev/pve && \
log_info "${magenta}Changing disk ID${nocolor}"
log_info "`mv -v {vm-${ORIGINAL_ID}-disk-1,vm-${NEW_ID}-disk-1}`"
if [[ ! -z /dev/pve/vm-${NEW_ID}-disk-1 ]] ; then
log_info "${green}OK, disk ID has been changed${nocolor}\n"
else
log_error "${red}Disk ID has not been changed, please recheck${nocolor}\n" && exit 0
fi
cd /etc/pve/nodes/proxmox/qemu-server && \
log_info "${magenta}Changing .conf file${nocolor}"
log_info "`mv -v {${ORIGINAL_ID}.conf,${NEW_ID}.conf}`"
if [[ -f /etc/pve/nodes/proxmox/qemu-server/${NEW_ID}.conf ]] ; then
log_info "${green}OK, conf file has been changed, changing virtio device id:${nocolor}"
sed -i 's/vm-'$ORIGINAL_ID'/vm-'$NEW_ID'/' ${NEW_ID}.conf
log_info "`cat ${NEW_ID}.conf | grep "vm-$NEW_ID"` --> `echo -e "${green}OK${nocolor}"`"
else
log_error "${red}Not OK, conf file hasn't been changed${nocolor}" && exit 0
fi
if [[ $(qm status ${NEW_ID} | grep "status" | wc -l) -eq 1 ]] ; then
log_info "${magenta}Seems like everything is good${nocolor}\n"
else
log_error "${red}Something went wrong, cannot check VM status${nocolor}\n" && exit 1
fi
fi
}
function post_action {
read -p "Would you like to start VM? [yes|no] " start
for answer in $start
do
case "$answer" in
y|yes|Yes )
qm start ${NEW_ID}
log_info "${green}Ok, starting VM${nocolor} `sleep 5` --> `qm status ${NEW_ID}`"
return 0
;;
no|n|No )
log_info "${yellow}Alright, got it, later then!${nocolor}\n"
return 0
;;
* )
log_warn "${red}Please answer yes or no!${nocolor}"
post_action
;;
esac
done
}
function script {
vm_status
status "reviewing VM status"
main
post_action
status "making necessary changes"
}
script "$@"