#!/bin/bash
read -p "please input hostname: " hostname

if [ -z "$hostname" ]; then
  echo "error, hostname can not empty！"
  exit 1
fi

sudo hostnamectl set-hostname "$hostname"

if [ $? -eq 0 ]; then
  echo "hostname: $hostname"
else
  echo "error, failed set hostname！"
  exit 1
fi


# Disk format
uuid1=""
uuid2=""
uuid3=""
mkdir -p /Data1 /Data2 /BackUp
disks=$(lsblk -b -n -o NAME,SIZE -d | awk '$2 >= 300000000000 {print $1}')
if [ -z "$disks" ]; then
        echo "No disks meet the criteria (size greater than 300GB)"
        exit 1
fi
for disk in $disks; do
        echo "Formatting disk $disk as ext4 filesystem..."
        echo "y" | mkfs.ext4 "/dev/$disk"
        if [ $? -eq 0 ]; then
                 echo "Disk $disk formatted successfully as ext4 filesystem."
        else
                echo "Failed to format disk $disk."
        fi
done
large_device=$(lsblk -b -n -o NAME,SIZE -d | awk '$2 >= 3000000000000 {print $1}' | head -n 1)
if [ -n "$large_device" ]; then
        uuid3=$(blkid -s UUID -o value "/dev/$large_device")
        if [ -n "$uuid3" ]; then
                echo "($large_device): $uuid3"
        fi
fi
data_count=1
for device in $(lsblk -b -n -o NAME,SIZE -d | awk '$2 >= 300000000000 && $2 < 2500000000000 {print $1}'); do
        uuid=$(blkid -s UUID -o value "/dev/$device")
        if [ "$data_count" -eq 1 ]; then
                uuid1="$uuid"
                echo "($device): $uuid1"
        elif [ "$data_count" -eq 2 ]; then
                uuid2="$uuid"
                echo "($device): $uuid2"
        fi
        ((data_count++))
        if [ "$data_count" -gt 2 ]; then
                break
        fi
done
echo "UUID=$uuid1 /Data1 ext4 defaults 0 0" >> /etc/fstab
echo "UUID=$uuid2 /Data2 ext4 defaults 0 0" >> /etc/fstab
echo "UUID=$uuid3 /BackUp ext4 defaults 0 0" >> /etc/fstab
mount -a
mkdir -p /Data1/vms
mkdir -p /Data2/vms
echo "Disk format ................................. Done!"

# check disk size
data1_threshold=$((800  1024  1024))  # 800 GB
data2_threshold=$((800  1024  1024))  # 800 GB
backup_threshold=$((3  1024  1024 * 1024))  # 3 TB

data1_size=$(df -k /Data1 | awk 'NR==2 {print $4}')
data2_size=$(df -k /Data2 | awk 'NR==2 {print $4}')
backup_size=$(df -k /BackUp | awk 'NR==2 {print $4}')

if [ $data1_size -gt $data1_threshold ]; then
    echo "/Data1 directory size is greater than 800 GB."
else
    echo "/Data1 directory size is less than 800 GB."
fi

if [ $data2_size -gt $data2_threshold ]; then
    echo "/Data2 directory size is greater than 800 GB."
else
    echo "/Data2 directory size is less than 800 GB."
fi

if [ $backup_size -gt $backup_threshold ]; then
    echo "/BackUp directory size is greater than 3 TB."
else
    echo "/BackUp directory size is less than 3 TB."
fi


cp /templates/base.tar.gz /Data1
cd /Data1
tar -zxvf  base.tar.gz
rm base.tar.gz
