├── LICENSE ├── README.md └── vzdump_hook.sh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 RobHost GmbH 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # proxmox-scripts 2 | 3 | Some little helpers for Proxmox Virtual Enviroment 4 | 5 | ## vzdump_hook.sh 6 | 7 | Hook script for Proxmox VZDump utility. It removes or changes disk 8 | throttles before the dump starts and restores the original throttle 9 | after the dump has finished. 10 | -------------------------------------------------------------------------------- /vzdump_hook.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Proxmox VZDump hook script for managing disk throttles for backups. 4 | # 5 | # Author: RobHost 6 | # License: MIT 7 | # Version: 0.2, reldate 2016-08-31 8 | # Repo: https://github.com/robhost/proxmox-scripts 9 | # 10 | # This script is intended to be used as a hook script for the Proxmox 11 | # VZDump utility. It applies throttling for backup or removes all disk 12 | # throttles before the dump starts and restores the original config 13 | # after the dump has finished. 14 | # 15 | # In order to use it, use the configuration directive "script" of the 16 | # vzdump utility. This can be done for scheduled backups by putting 17 | # "script: /path/to/this/script" in /etc/vzdump.conf. Don't forget to 18 | # set executable permission for the script file. 19 | # 20 | # If you don't put a vzdump_hook.conf next to the script, all throttles 21 | # will be removed. If you want to raise the throttle for the backup, 22 | # put the desired config options into the conf file, for instance: 23 | # 24 | # iops_rd=1000 25 | # iops_rd_max=10000 26 | # 27 | # 28 | # This script has been tested and used on Proxmox 4.3 and 4.2. 29 | # 30 | # Changelog: 31 | # 0.3, reldate 2017-02-10 - RobHost 32 | # - add support for backup throttle 33 | # 0.2, reldate 2016-08-31 - RobHost 34 | # - add noop output 35 | # 0.1, reldate 2016-08-30 - RobHost 36 | # - hello world 37 | 38 | declare -a BACKUP_THROTTLE 39 | 40 | CONFFIGPATH="$(dirname "$(realpath "$BASH_SOURCE")")/vzdump_hook.conf" 41 | 42 | if [[ -r $CONFFIGPATH ]] 43 | then 44 | BACKUP_THROTTLE=($(< "$CONFFIGPATH")) 45 | fi 46 | 47 | 48 | # Find config directives of throttled storage for the VM with the given 49 | # vmid. Echoes a line with spaces removed for each found directive. 50 | 51 | _get_throttled_storage() { 52 | local vmid="$1" 53 | 54 | while read -r cid opts 55 | do 56 | [[ "$cid" =~ ^(ide|sata|scsi|virtio)[0-9]+ ]] || continue 57 | [[ "$opts" =~ (iops|mbps) ]] || continue 58 | echo "${cid}$opts" 59 | done < <(qm config "$vmid" -current) 60 | } 61 | 62 | # Remove throttleing options in the given storage config string. This 63 | # can be a multiline string with a storage config directive per line 64 | # with spaces removed, as retuned by _get_throttled_storage. 65 | # Echoes in the same format with throttling options removed. 66 | 67 | _apply_backup_throttle() { 68 | local storage="$1" 69 | 70 | for storage in $storage 71 | do 72 | local -a storage_a=(${storage/:/ }) 73 | local sid=${storage_a[0]} 74 | local -a sopts_a=(${storage_a[1]//,/ }) 75 | 76 | for i in ${!sopts_a[@]} 77 | do 78 | [[ "${sopts_a[$i]}" =~ ^(iops|mbps) ]] || continue 79 | unset sopts_a[$i] 80 | done 81 | 82 | for j in ${BACKUP_THROTTLE[@]} 83 | do 84 | [[ ! ${sopts_a[@]} =~ \ ${j%=*}= ]] || continue 85 | sopts_a+=("$j") 86 | done 87 | 88 | local sopts="${sopts_a[*]}" 89 | echo "$sid:${sopts// /,}" 90 | done 91 | } 92 | 93 | # Run qm set for the given vmid. All further arguments are handled as 94 | # configuration directives in the format : as given by 95 | # _get_throttled_storage and _remove_throttle. VM locks are skipped. 96 | # Echoes the output of qm set. 97 | 98 | _update_config() { 99 | local vmid=$1 100 | local conf_a=("${@:2}") 101 | local opts="" 102 | 103 | for conf in "${conf_a[@]}" 104 | do 105 | opts+="-${conf/:/ } " 106 | done 107 | 108 | qm set "$vmid" -skiplock 1 $opts 109 | } 110 | 111 | # Remove and restore storage throttles for a VM. First argument is the 112 | # action to do (remove or restore). Secoand argument is the vmid. 113 | # Returns 1 if invalid action. 114 | 115 | storage_throttle() { 116 | local action=$1 117 | local vmid=$2 118 | local script_name=$(basename $0) 119 | 120 | # Store config in a persistent location, so it is not lost is case of 121 | # an unexpected reboot of the host. 122 | local storageconfpath="/var/tmp/${script_name}/storageconf_${vmid}" 123 | 124 | case "$action" in 125 | remove) 126 | local storage=$(_get_throttled_storage "$vmid") 127 | 128 | if [[ -z "$storage" ]] 129 | then 130 | echo "no throttled disks found to unthrottle" 131 | return 0 132 | fi 133 | 134 | mkdir -p "$(dirname "$storageconfpath")" 135 | echo "$storage" > "$storageconfpath" 136 | storage=$(_apply_backup_throttle "$storage") 137 | _update_config "$vmid" $storage 138 | ;; 139 | restore) 140 | if [[ ! -e "$storageconfpath" ]] 141 | then 142 | echo "no stored throttled disk configs found" 143 | return 0 144 | fi 145 | 146 | local storage="$(< "$storageconfpath")" 147 | _update_config "$vmid" $storage 148 | rm -f "$storageconfpath" 149 | ;; 150 | *) 151 | echo "invalid action '$action'" 152 | return 1 153 | ;; 154 | esac 155 | } 156 | 157 | # Process arguments and environment variables as received from and set 158 | # by vzdump. Output of commands is not redirected. 159 | 160 | vzdump_hook() { 161 | local phase="$1" # (job|backup)-(start|end|abort)/log-end/pre-(stop|restart)/post-restart 162 | local dumpdir="$DUMPDIR" 163 | local storeid="$STOREID" 164 | 165 | case "$phase" in 166 | # set variables for the phases 167 | job-start|job-end|job-abort) 168 | ;;& 169 | backup-start|backup-end|backup-abort|log-end|pre-stop|pre-restart|post-restart) 170 | local mode="$2" # stop/suspend/snapshot 171 | local vmid="$3" 172 | local vmtype="$VMTYPE" # openvz/qemu 173 | local hostname="$HOSTNAME" 174 | ;;& 175 | backup-end) 176 | local tarfile="$TARFILE" 177 | ;;& 178 | log-end) 179 | local logfile="$LOGFILE" 180 | ;;& 181 | 182 | # do work 183 | job-start) 184 | ;; 185 | job-end) 186 | ;; 187 | job-abort) 188 | ;; 189 | backup-start) 190 | storage_throttle remove "$vmid" 191 | ;; 192 | backup-end) 193 | storage_throttle restore "$vmid" 194 | ;; 195 | backup-abort) 196 | storage_throttle restore "$vmid" 197 | ;; 198 | log-end) 199 | ;; 200 | pre-stop) 201 | ;; 202 | pre-restart) 203 | ;; 204 | post-restart) 205 | ;; 206 | *) 207 | echo "unknown phase '$phase'" 208 | return 1 209 | ;; 210 | esac 211 | } 212 | 213 | 214 | # If this script is executed, run main function with the given 215 | # command line arguments. Otherwise do nothing. This makes it possible 216 | # to source this script for testing purposes or use of the functions 217 | # from other hook scripts. 218 | [[ "$BASH_SOURCE" != "$0" ]] || vzdump_hook "$@" 219 | --------------------------------------------------------------------------------