#!/bin/bash

sgdisk -e /dev/vda
total_vda_sector_size=`cat /sys/block/vda/size`

total_partition_num=`lsblk /dev/vda |grep -c part`

vda_last_partitioin_start_sector_size=`cat /sys/block/vda/vda${total_partition_num}/start`

vda_last_partitioin_size=`cat /sys/block/vda/vda${total_partition_num}/size`

end_vda_last_partition_sector_size=$((vda_last_partitioin_start_sector_size+vda_last_partitioin_size-1))

if ((total_vda_sector_size == end_vda_last_partition_sector_size + 1))
then
  echo "eq"
else
  new_disk_start_sector=$((end_vda_last_partition_sector_size+34))
  new_disk_end_sector=$((total_vda_sector_size-34))
  new_disk_partition_num=$((total_partition_num+1))
  vg_name=`vgs --noheadings -o vg_name`
  vg_name_str=$(echo "$vg_name" | tr -d ' ')

  lvs_fullname=`lvs --noheadings -o lv_fullname|grep -v swap`
  lvs_fullname_str=$(echo "$lvs_fullname" | tr -d ' ')

  parted /dev/vda mkpart primary ${new_disk_start_sector}s ${new_disk_end_sector}s

  pvcreate /dev/vda${new_disk_partition_num}

  vgextend ${vg_name_str} /dev/vda${new_disk_partition_num}

  lvextend -l +100%FREE /dev/${lvs_fullname_str}

  resize2fs /dev/${lvs_fullname_str}
fi
