vm_slots.sh 625 B

12345678910111213141516171819
  1. # sourced: take_slot blocks until one of MAXVMS (default 5) flock files in /tmp/lwe-vm-slots is free and keeps it in
  2. # SLOT_FD, release_slot gives it back. Shared by every script that boots VMs, so they never run more than MAXVMS together.
  3. take_slot() {
  4. local n
  5. mkdir -p /tmp/lwe-vm-slots
  6. while :; do
  7. for n in $(seq "${MAXVMS:-5}"); do
  8. exec {SLOT_FD}>"/tmp/lwe-vm-slots/slot$n" 2>/dev/null || continue
  9. flock -n "$SLOT_FD" && return 0
  10. exec {SLOT_FD}>&-
  11. done
  12. sleep 10
  13. done
  14. }
  15. release_slot() {
  16. [ -n "$SLOT_FD" ] && exec {SLOT_FD}>&-
  17. SLOT_FD=
  18. }