diff options
Diffstat (limited to 'ezjail-admin')
-rwxr-xr-x | ezjail-admin | 109 |
1 files changed, 107 insertions, 2 deletions
diff --git a/ezjail-admin b/ezjail-admin index 835bd33..986294e 100755 --- a/ezjail-admin +++ b/ezjail-admin | |||
@@ -8,6 +8,7 @@ ezjail_etc="${ezjail_prefix}/etc" | |||
8 | ezjail_share="${ezjail_prefix}/share/ezjail" | 8 | ezjail_share="${ezjail_prefix}/share/ezjail" |
9 | ezjail_examples="${ezjail_prefix}/share/examples/ezjail" | 9 | ezjail_examples="${ezjail_prefix}/share/examples/ezjail" |
10 | ezjail_jailcfgs="${ezjail_etc}/ezjail" | 10 | ezjail_jailcfgs="${ezjail_etc}/ezjail" |
11 | ezjail_snap_date_format="%Y%m%d%H%M" | ||
11 | 12 | ||
12 | # read user config | 13 | # read user config |
13 | [ -f "${ezjail_etc}/ezjail.conf" ] && . "${ezjail_etc}/ezjail.conf" | 14 | [ -f "${ezjail_etc}/ezjail.conf" ] && . "${ezjail_etc}/ezjail.conf" |
@@ -40,7 +41,7 @@ ezjail_basesystem="base" | |||
40 | case `uname -p` in amd64) ezjail_dirlist="${ezjail_dirlist} usr/lib32"; ezjail_basesystem="${ezjail_basesystem} lib32";; esac | 41 | case `uname -p` in amd64) ezjail_dirlist="${ezjail_dirlist} usr/lib32"; ezjail_basesystem="${ezjail_basesystem} lib32";; esac |
41 | 42 | ||
42 | # Synopsis messages | 43 | # Synopsis messages |
43 | ezjail_usage_ezjailadmin="${ezjail_admin} v3.3\nUsage: ${ezjail_admin} [archive|config|console|create|delete|install|list|restore|update] {params}" | 44 | ezjail_usage_ezjailadmin="${ezjail_admin} v3.3\nUsage: ${ezjail_admin} [archive|config|console|create|delete|install|list|restore|snapshot|update] {params}" |
44 | ezjail_usage_install="Usage: ${ezjail_admin} install [-mMpPsS] [-h host] [-r release]" | 45 | ezjail_usage_install="Usage: ${ezjail_admin} install [-mMpPsS] [-h host] [-r release]" |
45 | ezjail_usage_create="Usage: ${ezjail_admin} create [-xbi] [-f flavour] [-r jailroot] [-s size] [-c bde|eli|zfs] [-C args] [-a archive] [-z parentzfs] jailname jailip" | 46 | ezjail_usage_create="Usage: ${ezjail_admin} create [-xbi] [-f flavour] [-r jailroot] [-s size] [-c bde|eli|zfs] [-C args] [-a archive] [-z parentzfs] jailname jailip" |
46 | ezjail_usage_delete="Usage: ${ezjail_admin} delete [-wf] jailname" | 47 | ezjail_usage_delete="Usage: ${ezjail_admin} delete [-wf] jailname" |
@@ -441,6 +442,70 @@ ensure_jailzfs() { | |||
441 | /sbin/zfs create -o mountpoint=${ezjail_jaildir} ${ezjail_zfs_properties} ${ezjail_jailzfs} | 442 | /sbin/zfs create -o mountpoint=${ezjail_jaildir} ${ezjail_zfs_properties} ${ezjail_jailzfs} |
442 | } | 443 | } |
443 | 444 | ||
445 | # implement snapshot retentions | ||
446 | filteroldsnapshots() { | ||
447 | local win repeat bottom in_window snap_id snap_del first_round_done max_diff | ||
448 | bottom=`date +%s` | ||
449 | |||
450 | unset snap_del first_round_done | ||
451 | snap_id=0 | ||
452 | for win in ${ezjail_retention_policy}; do | ||
453 | # split repeat count from window chunk | ||
454 | case ${win} in *x*) repeat=${win%x*}; win=${win#*x};; *) repeat=1;; KEEP) return;; esac | ||
455 | |||
456 | # check for correct value | ||
457 | case ${win} in [0-9]);; [0-9]*[0-9mhdwy]);; *) echo "Unknown window length declaration ${win}"; return ;; esac | ||
458 | |||
459 | # values default to minutes | ||
460 | case ${win} in *h) m=60;; *d) m=1440;; *w) m=10080;; *y) m=*525600;; *) m=1;; esac | ||
461 | win=$((${win%[mhdwy]}*m*60)) | ||
462 | max_diff=$(( 3 * win / 4 )) | ||
463 | |||
464 | # innerloop $repeats over windows | ||
465 | while [ $(( repeat-=1 )) -ge 0 ]; do | ||
466 | # Shift bottom of window | ||
467 | bottom=$(( ${bottom} - ${win} )) | ||
468 | |||
469 | # now loop over parameters | ||
470 | in_window=YES; while [ "${in_window}" ]; do | ||
471 | |||
472 | # When snap_id is required, pop one if possible | ||
473 | if [ "${snap_id}" -eq 0 -a $# -gt 0 ]; then | ||
474 | snap_id=`date -j -f ${ezjail_snap_date_format} $1 +%s` | ||
475 | shift | ||
476 | fi | ||
477 | |||
478 | # is next snapshot before this window? test next window | ||
479 | if [ ${snap_id} -lt ${bottom} ]; then | ||
480 | [ "${first_round_done}" ] || echo /sbin/zfs snapshot -r ${ezjail_zfs}@ez-autosnap-`date +${ezjail_snap_date_format}` | ||
481 | [ "${first_round_done}" ] || /sbin/zfs snapshot -r ${ezjail_zfs}@ez-autosnap-`date +${ezjail_snap_date_format}` | ||
482 | |||
483 | # Zero marks end of snaps list | ||
484 | [ "${snap_id}" -eq 0 ] && return | ||
485 | |||
486 | unset snap_del in_window | ||
487 | else | ||
488 | if [ "${snap_del}" -a $(( snap_del - snap_id )) -lt ${max_diff} ]; then | ||
489 | echo /sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_del} +${ezjail_snap_date_format}` | ||
490 | /sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_del} +${ezjail_snap_date_format}` | ||
491 | fi | ||
492 | snap_del="${snap_id}" | ||
493 | snap_id=0 | ||
494 | fi | ||
495 | first_round_done="YES" | ||
496 | |||
497 | done | ||
498 | done | ||
499 | done | ||
500 | |||
501 | # if out of windows, default to delete the remainder of snaps | ||
502 | for snap_del in ${snap_id} $*; do | ||
503 | echo /sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_del} +${ezjail_snap_date_format}` | ||
504 | /sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_del} +${ezjail_snap_date_format}` | ||
505 | done | ||
506 | |||
507 | } | ||
508 | |||
444 | ############################# | 509 | ############################# |
445 | # End of function definitions | 510 | # End of function definitions |
446 | # " | 511 | # " |
@@ -1156,7 +1221,47 @@ console) | |||
1156 | 1221 | ||
1157 | exec ${_setfib} jexec ${ezjail_id} ${ezjail_default_execute} | 1222 | exec ${_setfib} jexec ${ezjail_id} ${ezjail_default_execute} |
1158 | ;; | 1223 | ;; |
1159 | ######################## ezjail-admin ARCHIVE ######################## | 1224 | ######################## ezjail-admin SNAPSHOT ######################## |
1225 | snapshot) | ||
1226 | shift | ||
1227 | if [ $# -eq 0 ]; then | ||
1228 | cd ${ezjail_jailcfgs} && ezjail_list=`ls -A` | ||
1229 | else | ||
1230 | ezjail_list=$* | ||
1231 | fi | ||
1232 | |||
1233 | for ezjail in ${ezjail_list}; do | ||
1234 | fetchjailinfo ${ezjail%.norun} | ||
1235 | |||
1236 | # Check for existence of jail in our records | ||
1237 | [ "${ezjail_config}" ] || exerr "Error: Nothing known about jail ${ezjail_name}." | ||
1238 | |||
1239 | # Snapshots only work with zfs jails | ||
1240 | [ "${ezjail_imagetype}" = "zfs" -o -n "${ezjail_zfs_datasets}" ] || continue | ||
1241 | |||
1242 | [ "${ezjail_imagetype}" = "zfs" ] && ezjail_zfs_datasets="${ezjail_parentzfs}/${ezjail_hostname} ${ezjail_zfs_datasets}" | ||
1243 | |||
1244 | # Use global retention policy, if none set | ||
1245 | : ${ezjail_retention_policy=${ezjail_default_retention_policy}} | ||
1246 | |||
1247 | for ezjail_zfs in ${ezjail_zfs_datasets}; do | ||
1248 | check_for_zfs_exist ${ezjail_zfs} || continue | ||
1249 | zfs_retention_policy=`/sbin/zfs get -H -o value ezjail:autosnap_retention ${ezjail_zfs}` | ||
1250 | [ "${zfs_retention_policy}" = "-" ] || ezjail_retention_policy="${zfs_retention_policy}" | ||
1251 | echo RP: ${ezjail_retention_policy} | ||
1252 | if [ -z "${ezjail_retention_policy}" ]; then | ||
1253 | /sbin/zfs snapshot ${ezjail_zfs}@ez-autosnap-`date +${ezjail_snap_date_format}` | ||
1254 | else | ||
1255 | snap_list=`/sbin/zfs list -H -t snapshot -o name -S creation -r ${ezjail_zfs} | \ | ||
1256 | grep ^${ezjail_zfs}@ez-autosnap- | cut -d '@' -f 2 | cut -d '-' -f 3` | ||
1257 | echo SN: ${snap_list} | ||
1258 | filteroldsnapshots ${snap_list} | ||
1259 | fi | ||
1260 | done | ||
1261 | |||
1262 | done | ||
1263 | ;; | ||
1264 | ######################## ezjail-admin ARCHIVE ######################## | ||
1160 | archive) | 1265 | archive) |
1161 | # Clean variables, prevent pollution | 1266 | # Clean variables, prevent pollution |
1162 | unset ezjail_archive ezjail_archive_tag ezjail_force ezjail_archivealljails ezjail_addfiles | 1267 | unset ezjail_archive ezjail_archive_tag ezjail_force ezjail_archivealljails ezjail_addfiles |