Skip to content

Commit

Permalink
fix: Update user-data.sh (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
13archit authored Aug 28, 2023
1 parent 5ac1f1a commit 8610ee3
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions _example/complete/user-data.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
#!/bin/bash
sleep 60
DEVICE=/dev/$(lsblk -n | awk '$NF != "/" {print $1}'| tail -n 1 )
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
FS_TYPE=$(file -s $DEVICE | awk '{print $2}')
MOUNT_POINT=/data

# If no FS, then this output contains "data"
if [ "$FS_TYPE" = "data" ]
then
echo "Creating file system on $DEVICE"
mkfs -t ext4 $DEVICE
fi
### Mountig ebs volume

mkdir $MOUNT_POINT
mount $DEVICE $MOUNT_POINT
# Specify the target directory where you want to mount the devices
mount_point="/data"

# Device to skip
device_to_skip="xvda"

# Filesystem type
filesystem_type="ext4" # Change this to the appropriate filesystem type

# Create the mount point directory if it doesn't exist
sudo mkdir -p "$mount_point"

# Use lsblk to list block devices, filter by type "disk" (whole disks)
# and exclude read-only filesystems (ro)
block_devices=$(lsblk -o NAME,TYPE,RO -r -n | awk '$2 == "disk" && $3 == "0" {print $1}')

# Iterate through the block devices, skip the specified device, and attempt to mount the rest
for device in $block_devices; do
if [ "$device" != "$device_to_skip" ]; then
echo "Mounting $device at $mount_point/$device"
sudo mkdir -p "$mount_point/$device"
sudo mkfs -t "$filesystem_type" "/dev/$device" # Format the device with the specified filesystem
sudo mount "/dev/$device" "$mount_point/$device"
if [ $? -eq 0 ]; then
echo "Mounting successful."
else
echo "Failed to mount $device."
fi
else
echo "Skipping $device."
fi
done
echo "Mounting complete."

0 comments on commit 8610ee3

Please sign in to comment.