Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update user-data.sh #54

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."