-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmakeThin.ash
More file actions
166 lines (145 loc) · 6.74 KB
/
Copy pathmakeThin.ash
File metadata and controls
166 lines (145 loc) · 6.74 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
166
#-------------------------------------------------------------------------
# Copyright (C) 2010, 2011 Ruben Miguelez Garcia
# Copyright (C) 2013 terreActive AG
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#-------------------------------------------------------------------------
#
# The documentation of this program can be found on http://vmutils.blogspot.com/
#
# To start using this script, place this file on an ESX and run
# . /path/<name_of_this_file>
#
#
# v1 All basic functionality working perfectly
# v2 Improved code to extract the vmdks from the vmx instead of just try all vmdks
# v3 Improved code of cleanup to not ask when there is nothing to delete
# v4 Added functionality to pass as a parameter datastores to ignore during the checks (if desired)
# v5 Reduced complexity of entire program, as clonning never touches the CID and therefore there is no need to look for other vmdks pointing to the one we will convert.
# v5 Added functions to convert disks alone, find them and do both (find+convert)
# v5 Added checks agains RDMs
# v6 Cleaned up unnecessary functions and added computeThin
#
# Every time a pipe is used the second process may be executed in a subshell. If so, the keyboard redirection does not work
# For that reason some of these functions can not be used in other scripts.
#
#==== Auxiliary Functions ====================================================
SizeH () { ls -lh "$1" | awk '{ print $5 }'; }
#------------------
isThin () { result=`grep -i thinProvisioned "$1"` ; if [ "$result" = 'ddb.thinProvisioned = "1"' ] ; then echo yes; else echo no; fi; }
#------------------
isBaseDisk () {
# Ensure that the disk is both, base disk (no snapshot) and a virtual disk (not an RDM)
result=`grep -i parentCID "$1"`;
result2=`grep -i createType "$1"`;
if [ "$result" = "parentCID=ffffffff" -a "$result2" = 'createType="vmfs"' ] ; then echo "yes"; else echo "no"; fi; }
#============= cleanupSAFETMP =================================================
cleanupSAFETMP () {
# Usage: cleanupSAFETMP /path/
# Find SAFETMP*vmdk files recursively under the path specified and ask you for confirmation before deleting them.
# If no argument provided, then look where we are
if [ "$1" != "" ]; then LOCATION="$*"; else LOCATION="."; fi;
# Check if there is something to clean up.
RESULTLOOKINGTARGETS=`find $LOCATION -name "SAFETMP*vmdk"`;
# Decide
if [ "$RESULTLOOKINGTARGETS" = "" ] ; then
printf "\nNo SAFETMP*vmdk files found under the specified location (default is \".\"). Nothing to clean up. Exiting.\n\n";
else
printf "\n\tFiles found:\n";
echo "$RESULTLOOKINGTARGETS";
printf "\nDo you want to delete these files?\n\n";
ANSWER="0";
printf "Answer (y/n): ";
read ANSWER;
if [ "$ANSWER" = "Y" -o "$ANSWER" = "y" ] ; then
find $LOCATION -name "SAFETMP*vmdk" ! -name "*-flat.vmdk" ! -name "*-delta.vmdk" | while read TODELETE ; do
vmkfstools -U "$TODELETE" ; done;
printf "\nDone.\n\n";
else
printf "\nNothing deleted.\n\n";
fi;
fi;
}
#============ findBaseDisks ======================================================
findBaseDisks () {
# Easy query to find vmdks that have a -flat.vmdk and are supposed to be BaseDisks.
# Snapshots have a -delta.vmdk supporting the data instead.
# This only finds Base Disks that have BOTH files, file.vmdk and its file-flat.vmdk
# Get original location
ORIGINALLOCATION=`pwd`;
# If no argument provided, then look where we are
if [ "$1" != "" ]; then
LOCATION="$*";
else
LOCATION=".";
fi;
# Find the -flat.vmdk files and from them the .vmdk and display if it exists.
find $LOCATION -iname "*-flat.vmdk" | while read DISKPATH; do
DPATH=`dirname "$DISKPATH"`;
DNAME=`basename "$DISKPATH" "-flat.vmdk"`;
if [ -e "$DPATH/$DNAME.vmdk" ]; then echo "$DPATH/$DNAME.vmdk"; fi;
done; }
#============= findAndMakeDiskThin = makeThin ======================================
alias makeThin=findAndMakeDiskThin ;
findAndMakeDiskThin () {
# Usage: findAndMakeDiskThin /path/
# It will find Base Disks recursively under the path specified and one by one, convert them to thin provision ONLY if required and ONLY if you confirm the operation.
# To say YES you just need to press y or Y, without Enter.
# Any other key, including Enter will mean NO.
{ # Another pair of keys to allow keyboard redirection
# Get original location
ORIGINALLOCATION=`pwd`;
# If no argument provided, then look where we are
if [ "$1" != "" ]; then
LOCATION="$*";
else
LOCATION=".";
fi;
findBaseDisks "$LOCATION" | while read VDISK ; do
# Check it exists
if [ ! -e "$VDISK" ]; then echo "$VDISK file not found."; else
printf "\n-----------------------------------------------------------------------------\n\n"
# Check that the disk is thin before continuing
if [ "`isThin \"$VDISK\"`" = "yes" ]; then echo "$VDISK is thinProvisioned, skipping." ; else
# Check that the disk is a Base Disk (not snapshot/RDM) before continuing
if [ "`isBaseDisk \"$VDISK\"`" = "no" ]; then echo "$VDISK is a snapshot and/or an RDM, skipping." ; else
# Display its size and free space on Datastore
DPATH=`dirname "$VDISK"`;
DNAME=`basename "$VDISK" ".vmdk"`;
DNAMEFLAT="$DNAME-flat.vmdk";
printf "\nWorking with $VDISK. Maximum space needed: "; SizeH "$DPATH/$DNAMEFLAT"; echo "";
vdf -h "$DPATH";
# Ask for confirmation
printf "\n-- Convert to thin? (y/n): "; INPUT="0"; read INPUT <&6; if [ "$INPUT" = "Y" -o "$INPUT" = "y" ] ; then
echo ""; #echo -e "yes do it\n" ;
# Rename source
echo "Will run: vmkfstools -E \"$VDISK\" \"$DPATH/SAFETMP$DNAME.vmdk\""
vmkfstools -E "$VDISK" "$DPATH/SAFETMP$DNAME.vmdk"
# Clone to thin
echo "Will run: vmkfstools -i \"$DPATH/SAFETMP$DNAME.vmdk\" -d thin \"$VDISK\""
vmkfstools -i "$DPATH/SAFETMP$DNAME.vmdk" -d thin "$VDISK"
# Verify
printf "\n Visual verification\n"
ls -l "$DPATH/SAFETMP$DNAME.vmdk" "$DPATH/SAFETMP$DNAME-flat.vmdk" "$DPATH/$DNAME.vmdk" "$DPATH/$DNAME-flat.vmdk"
else
#echo "DON'T do it" ;
echo ""
fi; # Confirmation to convert
fi; # is BaseDisk
fi; # isThin
fi; # file exist
done;
} 6>&0 # for redirection of keyboard
echo "";
};