This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixuuidfstab.sh
90 lines (66 loc) · 2.92 KB
/
fixuuidfstab.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
#!/usr/bin/env bash
#
# You have to be logged in to AzCli to run this.
# az login
# The script will iterate through all subscriptions, identify which VMs are
# running Linux and have disks attached; if the VMs are deallocated, the script
# will check if the VM is generalized (prepared to become an image). If the VM
# is not generalized, the script will start the VM, apply the fix than stop the
# VM back.
# For the VMs that were already powered ON, it will only apply the fix.
# To run this, just:
# chmod +x fixuuidfstab.sh && ./fixuuidfstab.sh
# Function to trigger the fix
function fix_uuids(){
az vm extension set \
--resource-group $vmRGName \
--vm-name $vmName \
--name customScript \
--publisher Microsoft.Azure.Extensions \
--protected-settings '{"fileUris": ["https://raw.githubusercontent.com/marinnedea/scripts/master/uuidfstab.bash"],"commandToExecute": "chmod +x uuidfstab.bash && ./uuidfstab.bash /etc/fstab"}'
echo "Updated fstab entries in VM: $vmName"
}
# List all your subscriptions
declare -a sidarray="$(az account list --query [].id -o tsv)"
if [ -z "$sidarray" ]; then
echo "No subscriptions IDs available. Stopping here."
exit 0
else
#for each subscription
for i in ${sidarray[@]}; do
sID=$i;
# Set subscriptionID:
az account set -s $sID
echo "Subscription: $sID set."
# List all VMs in subscription that are running Linux and have data disks attached
declare -a vmsdetails="$(az vm list --query "[?not_null(storageProfile.dataDisks[])]|[?storageProfile.osDisk.osType=='Linux'].{Name:name, Power:powerState, RG:resourceGroup}" -d -o tsv)"
if [ ! -z "$vmsdetails" ]; then
printf '%s\n' "${vmsdetails[*]}" | while read line; do
vmName="$(echo $line | awk '{print $1}')"
vmpowerState="$(echo $line | awk '{print $3}')"
vmRGName="$(echo $line | awk '{print $4}')"
# echo "Found VM $vmName in Resource Group $vmRGName."
# echo "The Vm Power State is: $vmpowerState."
echo "Finding out if VM $vmName is generalized."
generalizedStat="$(az vm get-instance-view -g $vmRGName -n $vmName --query 'instanceView.statuses[0].displayStatus' -o tsv)"
if [[ "$generalizedStat" != "VM generalized" ]] ; then
echo "VM $vmName not generalized"
if [[ "$vmpowerState" != "running" ]] ; then
echo "VM $vmName powered-off. Starting it up."
az vm start -g $vmRGName -n $vmName
fi
echo "VM $vmName power state is: running. Applying fix."
#Calling the function to run the fix UUIDs script:
fix_uuids
if [[ "$vmpowerState" != "running" ]] ; then
echo "VM $vmName reverted back to deallocated status."
az vm stop -g $vmRGName -n $vmName
fi
else
echo "$vmName is generalized. No operations possible on it. Skipping!"
fi
done
fi
done
fi
exit 0