Playbooks to a new Lilik
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.3 KiB

  1. #!/bin/bash
  2. ## Detect the first available continuous allocation of subuids/subgids
  3. ## of a given size.
  4. ##
  5. ## Returns output in idmap format:
  6. ## <starting_sub[ug]id> <allocation_length>
  7. ## Author: Lorenzo Zolfanelli <zolfa@lilik.it>
  8. required_length=65536
  9. function find_interval {
  10. if [ ! -f "$1" ]; then
  11. echo "100000 ${required_length}"
  12. return 0
  13. fi
  14. min_id_list=(100000 $(sed -n -e 's/^[^:]\+:\([0-9]\+\):\([0-9]\+\)/\1/p' $1))
  15. inc_id_list=(0 $(sed -n -e 's/^[^:]\+:\([0-9]\+\):\([0-9]\+\)/\2/p' $1))
  16. rows=$(( ${#min_id_list[*]} - 1 ))
  17. if [ $rows -lt 1 ]; then
  18. echo "100000 ${required_length}"
  19. return 0
  20. fi
  21. for i in $(seq 0 $rows); do
  22. c_start=$(( ${min_id_list[i]} + ${inc_id_list[i]} ))
  23. c_end=$(( ${c_start} + ${required_length} - 1 ))
  24. overlap=0
  25. for j in $(seq 0 $rows); do
  26. j_start=${min_id_list[j]}
  27. j_end=$(( ${min_id_list[j]} + ${inc_id_list[j]} - 1 ))
  28. if ([ "${c_start}" -ge "${j_start}" ] && [ "${c_start}" -le "${j_end}" ]) || ([ "${c_end}" -ge "${j_start}" ] && [ "${c_end}" -le "${j_end}" ]); then
  29. overlap=1
  30. break
  31. fi
  32. done
  33. if [ "${overlap}" -eq "0" ]; then
  34. echo "${c_start} ${required_length}"
  35. return 0
  36. fi
  37. done
  38. }
  39. find_interval /etc/subuid
  40. find_interval /etc/subgid