From 8610ee3c11ce11960191371dfaf40078bc77feb3 Mon Sep 17 00:00:00 2001 From: Archit Chopra <87892837+13archit@users.noreply.github.com> Date: Mon, 28 Aug 2023 22:59:35 +0530 Subject: [PATCH] fix: Update user-data.sh (#54) --- _example/complete/user-data.sh | 47 ++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/_example/complete/user-data.sh b/_example/complete/user-data.sh index 3fe0961..3cbefc3 100644 --- a/_example/complete/user-data.sh +++ b/_example/complete/user-data.sh @@ -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." \ No newline at end of file