-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrejoin_pve_node.sh
More file actions
165 lines (140 loc) · 4.95 KB
/
Copy pathrejoin_pve_node.sh
File metadata and controls
165 lines (140 loc) · 4.95 KB
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
#!/usr/bin/env bash
#
# Replace MASTER_IP with the Master node's IP address
#
set -euo pipefail
############################################
# CONFIG
############################################
MASTER_IP="MASTERNODEIPADDRESS"
BACKUP_DIR="/root/pve_vm_backup"
LOGFILE="/var/log/pve-rejoin.log"
MASTER_USER="root"
############################################
# FUNCTIONS
############################################
log() {
echo "[$(date '+%F %T')] $*" | tee -a "$LOGFILE"
}
die() {
log "ERROR: $*"
exit 1
}
check_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Required command '$1' not found."
}
############################################
# PRECHECKS
############################################
if [[ $EUID -ne 0 ]]; then
die "This script must be run as root."
fi
check_cmd systemctl
check_cmd pvecm
check_cmd pmxcfs
check_cmd mkdir
check_cmd mv
check_cmd rm
check_cmd ssh-keyscan
# sshpass is optional
HAS_SSHPASS=0
if command -v sshpass >/dev/null 2>&1; then
HAS_SSHPASS=1
fi
if ping -c1 -W2 "$MASTER_IP" >/dev/null 2>&1; then
log "Master $MASTER_IP is reachable."
else
log "WARNING: Master $MASTER_IP is NOT reachable by ping. Proceeding anyway."
fi
############################################
# STOP SERVICES
############################################
log "Stopping pve-cluster and corosync..."
systemctl stop pve-cluster corosync || log "Warning: failed to stop one of the services."
############################################
# KILL pmxcfs IF RUNNING
############################################
if pgrep -x pmxcfs >/dev/null 2>&1; then
log "Killing existing pmxcfs..."
killall pmxcfs || log "Warning: killall pmxcfs failed, continuing."
else
log "pmxcfs not running, OK."
fi
############################################
# START pmxcfs IN LOCAL MODE (foreground daemonizes itself)
############################################
log "Starting pmxcfs in local mode..."
pmxcfs -l
sleep 2
if ! pgrep -x pmxcfs >/dev/null 2>&1; then
die "pmxcfs did not start in local mode."
fi
log "pmxcfs (local) is running."
############################################
# CLEAN corosync.conf
############################################
if [[ -f /etc/pve/corosync.conf ]]; then
log "Removing existing /etc/pve/corosync.conf ..."
rm -f /etc/pve/corosync.conf || die "Failed to remove /etc/pve/corosync.conf"
else
log "/etc/pve/corosync.conf not found, skipping."
fi
############################################
# BACKUP VM/LXC CONFIGS
############################################
log "Ensuring backup dir exists at $BACKUP_DIR ..."
mkdir -p "$BACKUP_DIR" || die "Failed to create backup dir $BACKUP_DIR"
QEMU_GLOB="/etc/pve/nodes/*/qemu-server/*"
LXC_GLOB="/etc/pve/nodes/*/lxc/*"
if compgen -G "$QEMU_GLOB" >/dev/null 2>&1; then
log "Moving VM configs to backup dir..."
mv $QEMU_GLOB "$BACKUP_DIR"/ || die "Failed to move qemu configs"
else
log "No VM configs found under /etc/pve/nodes/*/qemu-server/"
fi
if compgen -G "$LXC_GLOB" >/dev/null 2>&1; then
log "Moving LXC configs to backup dir..."
mv $LXC_GLOB "$BACKUP_DIR"/ || die "Failed to move lxc configs"
else
log "No LXC configs found under /etc/pve/nodes/*/lxc/"
fi
############################################
# STOP LOCAL pmxcfs AGAIN (cleanup)
############################################
if pgrep -x pmxcfs >/dev/null 2>&1; then
log "Stopping local pmxcfs..."
killall pmxcfs || log "Warning: failed to kill pmxcfs."
fi
############################################
# START pve-cluster
############################################
log "Starting pve-cluster..."
systemctl start pve-cluster || die "Failed to start pve-cluster"
# leave corosync stopped before join
log "Ensuring corosync is stopped before join..."
systemctl stop corosync || log "Warning: corosync stop failed."
############################################
# PRE-ACCEPT SSH HOST KEY (this answers the 'yes' part)
############################################
log "Fetching SSH host key from $MASTER_IP to avoid interactive fingerprint prompt..."
ssh-keyscan -H "$MASTER_IP" >> /root/.ssh/known_hosts 2>/dev/null || log "Warning: ssh-keyscan failed, join may ask for fingerprint."
############################################
# JOIN CLUSTER
############################################
log "About to join cluster at $MASTER_IP ..."
if [[ $HAS_SSHPASS -eq 1 ]]; then
# Non-interactive join
read -r -s -p "Enter root password for $MASTER_IP: " MASTER_PASS
echo
log "Using sshpass for non-interactive pvecm add ..."
# pvecm add internally uses ssh; sshpass will inject the password
# --force is optional; keep it clean
SSH_ASKPASS_REQUIRE=force \
sshpass -p "$MASTER_PASS" pvecm add "$MASTER_IP" || die "pvecm add failed."
else
# Interactive fallback
log "sshpass not found. Running interactive 'pvecm add $MASTER_IP' now..."
log "You may be asked for root password on $MASTER_IP."
pvecm add "$MASTER_IP" || die "pvecm add failed."
fi
log "Join finished. Run 'pvecm status' to confirm."