Last active
August 7, 2024 15:31
-
-
Save oazabir/20d75df0a13b03859054a5b667a5edbe to your computer and use it in GitHub Desktop.
Azure: Mount data disks attached to a VM on boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DISK=$1 | |
VG=$2 | |
VOL=$3 | |
MOUNTPOINT=$4 | |
# Wait for x disks to be available | |
while [ `ls -l $DISK | grep lun | wc -l` -lt 1 ]; do | |
echo "Waiting on disks..."; | |
sleep 5 | |
done | |
DISK_PARTITION="${DISK}-part1" | |
# Check if the disk already has a partition | |
if ! ls "${DISK_PARTITION}"; then | |
# Partition disk if no partition exists | |
sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk $DISK | |
n | |
p | |
1 | |
w | |
EOF | |
DISK_PARTITION=$(ls ${DISK}* | tail -n 1) | |
# Create physical volume | |
pvcreate $DISK_PARTITION | |
fi | |
# Create or extend volume group | |
if ! vgs | grep -q $VG; then | |
vgcreate $VG $DISK_PARTITION | |
else | |
vgextend $VG $DISK_PARTITION | |
fi | |
# Create logical volume | |
if [[ -z $SIZE ]]; then | |
SIZE="100%FREE" | |
fi | |
if ! lvdisplay | grep -q "/dev/$VG/$VOL"; then | |
lvcreate -l $SIZE -n $VOL $VG | |
# Create filesystem | |
mkfs.ext3 -m 0 /dev/$VG/$VOL | |
fi | |
# Check if the entry already exists in fstab | |
if ! grep -q "/dev/$VG/$VOL" /etc/fstab; then | |
echo "/dev/$VG/$VOL $MOUNTPOINT ext3 defaults 0 2" >> /etc/fstab | |
fi | |
# Create mount point | |
mkdir -p $MOUNTPOINT | |
# Mount | |
mount $MOUNTPOINT | |
chmod ugo+rwx $MOUNTPOINT | |
touch $MOUNTPOINT/mounted.txt | |
echo "Mount done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment