-
Notifications
You must be signed in to change notification settings - Fork 0
/
v-optimize.sh
137 lines (109 loc) · 4.03 KB
/
v-optimize.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
133
134
135
136
137
#!/bin/sh
# shellcheck disable=1090
# ------------------
# Basic settings
_SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
_SCRIPT_NAME=$(basename "$0")
. "${_SCRIPT_DIR}/.v-common.rc"
[ "${_LOG_PREFIX}" ] || _LOG_PREFIX="[${_SCRIPT_NAME}] "
# ------------------
# Default values
optDefrag="yes"
optRebound="yes"
optZero="yes"
optWorkspace="z:"
optPartition=1
# ------------------
# Help
if [ $# -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
cat << _EOF_
Usage: [VARIABLES] ${_SCRIPT_NAME} [OPTIONS] v-FILE
Defrag, rebound(shrink & extend), zero-out free space and then compact an expandable vdisk file.
v-FILE Path to the vdisk file.
OPTIONS:
-d,--no-defrag Do not defrag.
-r,--no-rebound Do not rebound.
-z,--no-zero Do not zero-out free space.
-w,--workspace SPACE A drive or a directory to mount the vdisk while working.
It should not already exist and will be removed afterwards. Default is "z:".
-p,--partition N Compact the N'th partition. Default is 1.
${_HELP_VARIABLES}
Example:
${_SCRIPT_NAME} x:/some/vdisk.vhdx
_VERBOSE=4 _NO_COLOR=0 ${_SCRIPT_NAME} -w t:/workspace x:/some/vdisk.vhdx
_EOF_
exit
fi
# ------------------
# Parse arguments
# Ref: https://www.tutorialspoint.com/unix_commands/getopt.htm
GETOPT=$(getopt -o drzw:p: -l no-defrag,no-zero,no-rebound,workspace:,partition: -- "$@")
eval set -- "$GETOPT"
while true; do
case "$1" in
-d|--no-defrag)
optDefrag="no"; shift;;
-r|--no-rebound)
optRebound="no"; shift;;
-z|--no-zero)
optZero="no"; shift;;
-w|--workspace)
optWorkspace="$2"; shift 2;;
-p|--partition)
optPartition=$2; shift 2;;
--)
shift; break;;
*)
echo "getopt error"; exit 1;;
esac
done
# ------------------
# Preparation
_check_file_exist "$1"
_check_dir_not_exist "${optWorkspace}/"
vdiskFile=$(realpath "$1")
# ------------------
# Action
if [ "${optDefrag}" = "yes" ]; then
_log_highlight "Defraging '${optWorkspace}' ..."
_LOG_PREFIX="${_LOG_PREFIX}${_LOG_PREFIX_INDENT}" "${_SCRIPT_DIR}/v-mount.sh" -p "${optPartition}" -m "${vdiskFile}" "${optWorkspace}"
# ${_LOG_INFO_FD} is 3 (default), and >&3 is POSIX compliant.
# shellcheck disable=2039,2086
defrag "${optWorkspace}" /U >&${_LOG_INFO_FD}
_LOG_PREFIX="${_LOG_PREFIX}${_LOG_PREFIX_INDENT}" "${_SCRIPT_DIR}/v-umount.sh" "${vdiskFile}"
_log_empty_line
fi
if [ "${optRebound}" = "yes" ]; then
_log_highlight "Rebounding '${optWorkspace}' ..."
_LOG_PREFIX="${_LOG_PREFIX}${_LOG_PREFIX_INDENT}" "${_SCRIPT_DIR}/v-shrink.sh" -p "${optPartition}" -w "${optWorkspace}" "${vdiskFile}"
_log_empty_line
_LOG_PREFIX="${_LOG_PREFIX}${_LOG_PREFIX_INDENT}" "${_SCRIPT_DIR}/v-extend.sh" -p "${optPartition}" -w "${optWorkspace}" "${vdiskFile}"
_log_empty_line
fi
if [ "${optZero}" = "yes" ]; then
_log_highlight "Zeroing-out free space on '${optWorkspace}' ..."
_LOG_PREFIX="${_LOG_PREFIX}${_LOG_PREFIX_INDENT}" "${_SCRIPT_DIR}/v-mount.sh" -p "${optPartition}" -m "${vdiskFile}" "${optWorkspace}"
# ${_LOG_INFO_FD} is 3 (default), and >&3 is POSIX compliant.
# shellcheck disable=2039,2086
dd if=/dev/zero of="${optWorkspace}/.zeros" bs=4k 2>/dev/null 1>&${_LOG_INFO_FD}
rm "${optWorkspace}/.zeros"
_LOG_PREFIX="${_LOG_PREFIX}${_LOG_PREFIX_INDENT}" "${_SCRIPT_DIR}/v-umount.sh" "${vdiskFile}"
_log_empty_line
fi
# Remove the workspace if it is a directory.
[ "$(str_right "${optWorkspace}" 1)" = ":" ] || rmdir "${optWorkspace}"
# ----- Compact
vdiskFile=$(toWindowsPath "${vdiskFile}")
diskpartScript=$(cat << _EOF_
select vdisk file="${vdiskFile}"
compact vdisk
_EOF_
)
_log_highlight "Compacting '${vdiskFile}' ..."
_log_info "--- Diskpart script: begin"
_LOG_PREFIX="" _log_info "${diskpartScript}"
_log_info "--- Diskpart script: end"
_log_info "Running diskpart ..."
# ${_LOG_INFO_FD} is 3 (default), and >&3 is POSIX compliant.
# shellcheck disable=2039,2086
echo "${diskpartScript}" | ${_DISKPART} >&${_LOG_INFO_FD}