| 1 | #!/bin/bash |
| 2 | # Author: Steven Shiau <steven _at_ clonezilla org> |
| 3 | # License: GPL |
| 4 | # Check if the harddrive space is enough for DRBL environment |
| 5 | # input: the client no |
| 6 | # output: $total_avail_space $drbl_need $common_root_space $per_node_space |
| 7 | # if space is enough, the return code is 0, if no, return code is 1 |
| 8 | |
| 9 | # Load DRBL setting and functions |
| 10 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 11 | |
| 12 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 13 | |
| 14 | check_if_root |
| 15 | |
| 16 | # |
| 17 | usage() { |
| 18 | echo "Check if the space is enough to install DRBL" |
| 19 | echo "Usage: `basename $0` [OPTION]" |
| 20 | echo "Options:" |
| 21 | echo "-v, --verbose: verbose mode." |
| 22 | echo "-n, --client-no CLIENT_NO: The DRBL client number." |
| 23 | echo "If space is enough for setup, nothing will be shown, and the return code is 0. If not enough space, return code is 1, and the output number are: total_avail_space drbl_need_space common_root_space per_node_space" |
| 24 | echo "A estimation will be made by current necessary space multiplying ratio $buffer_ratio_for_client_space" |
| 25 | } |
| 26 | # |
| 27 | # main |
| 28 | while [ $# -gt 0 ]; do |
| 29 | case "$1" in |
| 30 | -n|--client-no) |
| 31 | shift; client_no="$1" |
| 32 | shift |
| 33 | ;; |
| 34 | -v|--verbose) |
| 35 | shift; verbose="on" |
| 36 | ;; |
| 37 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 38 | usage >& 2 |
| 39 | exit 2 ;; |
| 40 | *) break ;; |
| 41 | esac |
| 42 | done |
| 43 | |
| 44 | [ -z "$client_no" ] && usage && exit 1 |
| 45 | |
| 46 | # The extra space in front of /tftpboot is to exclude something like /mnt/tftpboot |
| 47 | tftpboot_space=$(LC_ALL=C df --block-size 1M 2>/dev/null | awk -F" " '/ \/tftpboot/ {print $4}') |
| 48 | [ -z "$tftpboot_space" ] && tftpboot_space=0 |
| 49 | # we will overwrite the necessary directories in /tftpboot/, the space |
| 50 | # will be released, so add it to $total_avail_space |
| 51 | tftpboot_current_used=$(LC_ALL=C du -smc /tftpboot/ 2>/dev/null | cut -f1 | tail -n1) |
| 52 | [ -z "$tftpboot_current_used" ] && tftpboot_current_used=0 |
| 53 | |
| 54 | # use $(NF-2) is safer than $4, because longer device name will use 2 lines like |
| 55 | # df -h / |
| 56 | # Filesystem Size Used Avail Use% Mounted on |
| 57 | # /dev/mapper/VolGroup00-LogVol00 |
| 58 | # Then it's $3, not $3. |
| 59 | root_space=$(LC_ALL=C df --block-size 1M / 2>/dev/null | awk -F" " '/\/$/ { print $(NF-2)}') |
| 60 | [ -z "$root_space" ] && root_space=0 |
| 61 | |
| 62 | total_avail_space=$(expr $tftpboot_space + $root_space + $tftpboot_current_used) |
| 63 | common_root_space=$(du -smc $common_root_dir 2>/dev/null | cut -f1 | tail -n1) |
| 64 | [ -z "$common_root_space" ] && common_root_space=0 |
| 65 | |
| 66 | # for /var/lib, we need to exclude the /var/lib/dpkg or /var/lib/rpm |
| 67 | per_node_space=$(du -smc /etc /var/lib 2>/dev/null | cut -f1 | tail -n1) |
| 68 | # Exclude the dir assigned in varlib_NOT_2_be_copied_2_each_client |
| 69 | varlib_pkg_space_all=0 |
| 70 | for iv in $varlib_NOT_2_be_copied_2_each_client; do |
| 71 | if [ -d /var/lib/$iv ]; then |
| 72 | varlib_pkg_space=$(du -smc /var/lib/$iv 2>/dev/null | cut -f1 | tail -n1) |
| 73 | if [ -n "$varlib_pkg_space" ]; then |
| 74 | varlib_pkg_space_all=$(( $varlib_pkg_space_all + $varlib_pkg_space)) |
| 75 | fi |
| 76 | fi |
| 77 | done |
| 78 | per_node_space=$(($per_node_space - $varlib_pkg_space_all)) |
| 79 | drbl_need=$(expr $client_no "*" $per_node_space + $common_root_space) |
| 80 | drbl_need=$(echo "scale=0; $drbl_need * $buffer_ratio_for_client_space" | bc -l) |
| 81 | # truncate the $drbl_need to be integer |
| 82 | drbl_need=$(echo "$drbl_need / 1" | bc) |
| 83 | if [ "$drbl_need" -gt "$total_avail_space" ]; then |
| 84 | if [ "$verbose" = "on" ]; then |
| 85 | echo "Warning!!! The system space maybe not enough for DRBL environment!!!" |
| 86 | echo "Estimated necessary space for each DRBL client (MB): $DRBL_CLIENT_SPACE_PER_NODE" |
| 87 | echo "Total DRBL clients: $client_no" |
| 88 | echo "Available space (MB): $total_avail_space" |
| 89 | echo "DRBL need space (MB): $drbl_need" |
| 90 | fi |
| 91 | echo $total_avail_space $drbl_need $common_root_space $per_node_space |
| 92 | exit 1 |
| 93 | fi |
| 94 | exit 0 |