Skip to content

Instantly share code, notes, and snippets.

@jgreat
Created October 17, 2022 14:59
Show Gist options
  • Save jgreat/fe0e6c20ff5b32b804dba738306d2513 to your computer and use it in GitHub Desktop.
Save jgreat/fe0e6c20ff5b32b804dba738306d2513 to your computer and use it in GitHub Desktop.
Azure VM wait for disk attachement.
#!/bin/bash
# wait for an azure disk attachment to happen.
set -e
disk="${1}"
disk_partition="${disk}-part1"
vg="${2}"
vol="${3}"
mount_point="${4}"
# Wait for x disks to be available
while ! ls "${disk}"
do
echo "waiting on disks..."
sleep 5
done
if [[ -L "${disk_partition}" ]]
then
echo "-- Found existing partition, will not format"
else
echo "-- Partition disk"
echo 'type=83' | sudo sfdisk /dev/disk/azure/scsi1/lun1
echo "-- Create physical volume"
while ! ls "${disk_partition}"
do
echo "waiting on partition..."
sleep 5
done
pvcreate "${disk_partition}"
echo "-- Create volume group"
if ! grep -q "$(vgs | grep "${vg}")"
then
vgcreate "${vg}" "${disk_partition}"
else
vgextend "${vg}" "${disk_partition}"
fi
echo "-- Create logical volume"
lvcreate -l "100%FREE" -n "${vol}" "${vg}"
echo "-- Create filesystem"
mkfs.ext3 -m 0 "/dev/${vg}/${vol}"
fi
echo "-- add to fstab"
echo "/dev/${vg}/${vol} ${mount_point} ext3 defaults 0 2" >> /etc/fstab
echo "-- Create mount point"
mkdir -p "${mount_point}"
echo "-- Mount volume"
mount "/dev/${vg}/${vol}" "${mount_point}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment