-
Notifications
You must be signed in to change notification settings - Fork 283
/
xe-edit-bootloader
executable file
·252 lines (218 loc) · 6.22 KB
/
xe-edit-bootloader
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
245
246
247
248
249
250
251
252
#!/bin/bash
#
# Copyright (c) Citrix Systems 2008. All rights reserved.
#
# Script which lets you edit the grub.conf in a Linux guest virtual disk.
if [ ! -e @INVENTORY@ ]; then
echo Must run on a XenServer host.
exit 1
fi
. @INVENTORY@
XE="@BINDIR@/xe"
device_number=
grub_conf=
default_file_list="/boot/grub/menu.lst /grub/menu.lst /boot/grub/grub.cfg /grub/grub.cfg"
function usage {
echo "Usage: $0 [-u <VM UUID>] [-n <VM name>]"
echo " [-p <partition number>] [-f <filename>]"
echo
echo " -u: UUID of the VM boot disk you wish to edit"
echo " -n: Name label of the VM boot disk you wish to edit"
echo " -p: Partition number to mount (default: none)"
echo " -f: Location of bootloader config file to edit"
echo " (defaults: ${default_file_list})"
echo
echo "Either -u or -n must be supplied to specify the VM to edit"
echo "Set the EDITOR environment variable if desired (default: nano)"
exit 1
}
while getopts "hu:n:p:f:" opt ; do
case $opt in
h) usage ;;
u) vm_uuid=${OPTARG} ;;
n) vm_name="${OPTARG}" ;;
p) device_number=p${OPTARG} ;;
f) default_file_list="${OPTARG}" ;;
*) echo "Invalid option"; usage ;;
esac
done
# determine a VM UUID
IFS=,
if [ "${vm_name}" != "" ]; then
vm_uuids=$(${XE} vm-list name-label="${vm_name}" params=uuid --minimal)
num_vm_uuids=0
for vm in ${vm_uuids}; do
num_vm_uuids=$(( ${num_vm_uuids} + 1 ))
done
if [ ${num_vm_uuids} -eq 0 ]; then
echo Unable to find VMs matching name-label: ${vm_name}
exit 1
elif [ ${num_vm_uuids} -gt 1 ]; then
echo Found multiple VMs with name-label: ${vm_name}
echo ${vm_uuids}
echo Please specify one of the UUIDs using the -u option
exit 1
fi
vm_uuid=${vm_uuids}
fi
# verify that the VM UUID is valid
if [ "${vm_uuid}" = "" ]; then usage; fi
checkuuid=$(${XE} vm-list uuid="${vm_uuid}" params=uuid --minimal)
if [ "${checkuuid}" = "" ]; then
echo Specified VM UUID "${vm_uuid}" does not exist.
exit 1
fi
# select nano as the default editor
if [ "${EDITOR}" = "" ]; then
EDITOR=nano
fi
mnt=
function cleanup {
if [ ! -z "${mnt}" ]; then
umount ${mnt} >/dev/null 2>&1
rmdir ${mnt}
fi
kpartx -d ${device}
if [ ! -z "${vbd_uuid}" ]; then
echo -n "Unplugging VBD: "
${XE} vbd-unplug uuid=${vbd_uuid} timeout=20
# poll for the device to go away if we know its name
if [ "${device}" != "" ]; then
device_gone=0
for ((i=0; i<10; i++)); do
echo -n "."
if [ ! -b ${device} ]; then
echo " done"
device_gone=1
break
fi
sleep 1
done
if [ ${device_gone} -eq 0 ]; then
echo " failed"
echo Please destroy VBD ${vbd_uuid} manually.
else
${XE} vbd-destroy uuid=${vbd_uuid}
fi
fi
fi
}
# Find the bootable VBD for the VM
IFS=","
bootable_vbds=$(${XE} vbd-list vm-uuid=${vm_uuid} bootable=true params=uuid --minimal)
num_bootable_vbds=0
for vbd in ${bootable_vbds}; do
num_bootable_vbds=$(( ${num_bootable_vbds} + 1 ))
done
if [ ${num_bootable_vbds} -eq 0 ]; then
echo Unable to find a bootable disk for VM.
echo There are no VBDs marked with bootable=true.
exit 1
elif [ ${num_bootable_vbds} -gt 1 ]; then
echo Found ${num_bootable_vbds} for VM, but only 1 is allowed.
echo Please remove the bootable=true flag from some VBDs to fix this.
exit 1
fi
# we now know that there is only one bootable VBD, so use that
vbd_uuid=${bootable_vbds}
vdi_uuid=$(${XE} vbd-param-get uuid=${vbd_uuid} param-name=vdi-uuid)
if [ "${vdi_uuid}" = "" ]; then
echo Unable to determine VDI associated with VBD ${vbd_uuid}
exit 1
fi
# check that the VDI is not currently in use
vbd_uuids=$(${XE} vbd-list vdi-uuid=${vdi_uuid} params=uuid --minimal)
if [ $? -ne 0 ]; then
echo Error in vbd-list call
exit 1
fi
for vbd in ${vbd_uuids}; do
attached=$(${XE} vbd-list params=currently-attached uuid=${vbd} --minimal)
case ${attached} in
true)
vm_uuid=$(${XE} vbd-list params=vm-uuid uuid=${vbd} --minimal)
echo The VDI is currently in use by VBD:
echo ${vbd}
echo The associated VM is:
echo ${vm_uuid}
echo
${XE} vm-list uuid=${vm_uuid}
echo Please shut down any running VMs which are currently using the disk.
exit 1
;;
false)
;;
*)
echo Internal error: unknown currently-attached result: ${attached}
;;
esac
done
echo -n "Creating dom0 VBD: "
vbd_uuid=$(${XE} vbd-create vm-uuid=${CONTROL_DOMAIN_UUID} vdi-uuid=${vdi_uuid} device=autodetect)
echo ${vbd_uuid}
if [ $? -ne 0 -o -z "${vbd_uuid}" ]; then
echo error creating VBD for dom0 block attach
cleanup
exit 1
fi
echo -n "Plugging VBD: "
${XE} vbd-plug uuid=${vbd_uuid}
echo ${device}
device=/dev/$(${XE} vbd-param-get uuid=${vbd_uuid} param-name=device)
if [ ! -b ${device} ]; then
echo ${device}: not a block special
cleanup
exit 1
fi
kpartx -av ${device}
mapped_device="/dev/mapper/${vdi_uuid}"
echo -n "Waiting for ${mapped_device}${device_number}: "
found_device=0
for ((i=0; i<5; i++)); do
echo -n '.'
if [ -b ${mapped_device}${device_number} ]; then
found_device=1
echo ' done'
break
fi
sleep 1
done
if [ ${found_device} -eq 0 ]; then
echo Device ${mapped_device}${device_number} not found.
echo You must specify the correct partition number with -p
cleanup
exit 1
fi
echo -n "Mounting filesystem: "
mnt=/var/run/fix-grub-${vdi_uuid}
mkdir -p ${mnt}
mount ${mapped_device}${device_number} ${mnt} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo " failed"
echo Partitions in the VDI are:
echo
ls -la ${mapped_device}*
echo
echo You can use the -p option to specify a partition number to mount.
cleanup
exit 1
fi
echo " done"
IFS=" "
if [ "${grub_conf}" = "" ]; then
for f in ${default_file_list}; do
if [ -e "${mnt}${f}" ]; then
grub_conf="${f}"
break;
fi
done
fi
if [ "${grub_conf}" = "" ]; then
echo Unable to find a valid bootloader config file in:
echo " ${default_file_list}"
echo Please specify one with the -f option
cleanup
exit 1
fi
${EDITOR} ${mnt}${grub_conf}
cleanup