Last active
August 26, 2024 03:58
-
-
Save varunsridharan/682b3d80075d9622a33818e81c34c83d to your computer and use it in GitHub Desktop.
USB Mount
This file contains hidden or 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
# Use an official lightweight Linux distribution as a base image | |
FROM ubuntu:latest | |
# Update package lists and install necessary packages | |
RUN apt-get update && apt-get install -y \ | |
sudo \ | |
util-linux \ | |
curl \ | |
mount \ | |
&& rm -rf /var/lib/apt/lists/* | |
# Create a user to avoid running as root | |
RUN useradd -ms /bin/bash scriptuser && echo 'scriptuser ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers | |
USER scriptuser | |
# Set the working directory inside the container | |
WORKDIR /home/scriptuser | |
# Download the script from the provided URL | |
RUN curl -o mount_script.sh https://gist.githubusercontent.com/varunsridharan/682b3d80075d9622a33818e81c34c83d/raw/d7ae933d8dbf36cabae66ecc27bbb4c68e49da6d/mount_script.sh | |
# Make the script executable | |
RUN chmod +x mount_script.sh | |
# Set the entry point to run the script | |
#ENTRYPOINT ["./mount_script.sh"] | |
CMD ./mount_script.sh && tail -f /dev/null |
This file contains hidden or 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 | |
# Get block device information using sudo blkid | |
blkid_output=$(sudo blkid) | |
mount_path="/mnt/usb" | |
# Check if the parent directory exists | |
if [ ! -d "$mount_path" ]; then | |
echo "The specified directory does not exist: $mount_path" | |
else | |
# Find all subdirectories (non-recursive) inside the parent directory | |
subdirectories=$(find "$mount_path" -mindepth 1 -maxdepth 1 -type d) | |
# Check if there are any subdirectories | |
if [ -z "$subdirectories" ]; then | |
echo "No subdirectories found in $mount_path" | |
else | |
# Iterate through each subdirectory and attempt to unmount it | |
for subdirectory in $subdirectories; do | |
echo "Attempting to unmount $subdirectory" | |
# Attempt to unmount the subdirectory | |
sudo umount "$subdirectory" | |
# Check if the unmount was successful | |
if [ $? -eq 0 ]; then | |
echo "Successfully unmounted $subdirectory" | |
else | |
echo "Failed to unmount $subdirectory. It may not be mounted." | |
fi | |
done | |
fi | |
fi | |
# Check if any block devices are available | |
if [ -z "$blkid_output" ]; then | |
echo "No block devices found. Exiting." | |
exit 1 | |
fi | |
# Display available block devices | |
echo "Available block devices:" | |
echo "$blkid_output" | |
echo " " | |
echo " " | |
# Define UUIDs to be excluded | |
excluded_uuids=("5DF9-E225" "91FE-7499" "3b614a3f-4a65-4480-876a-8a998e01ac9b" "56f80fa2-e005-4cca-86e6-19da1069914d") | |
# Function to check if a given UUID is in the exclusion list | |
uuid_in_exclude_list() { | |
local uuid_to_check="$1" | |
for uuid in "${excluded_uuids[@]}"; do | |
if [ "${uuid}" == "${uuid_to_check}" ]; then | |
return 0 # Match found | |
fi | |
done | |
return 1 # No match found | |
} | |
# Iterate through each line of the blkid output | |
while read -r line; do | |
# Extract device path, UUID, and label from the line | |
device=$(echo "$line" | cut -d':' -f1) | |
uuid=$(echo "$line" | grep -oP ' UUID="\K[^"]+') | |
label=$(echo "$line" | grep -oP ' LABEL="\K[^"]+') | |
# Check if the device has a label and is not in the exclusion list | |
if [ -n "$label" ] && ! uuid_in_exclude_list "$uuid"; then | |
# Create mount point with the label | |
mount_point="$mount_path/$label" | |
# Create the mount point if it doesn't exist | |
mkdir -p "$mount_point" | |
# Mount the device to the specified mount point | |
sudo mount "$device" "$mount_point" | |
# Check if the mount was successful | |
if [ $? -eq 0 ]; then | |
echo "Mount successful for $device at $mount_point" | |
else | |
echo "Error: Failed to mount $device. Skipping." | |
fi | |
elif [ -z "$label" ]; then | |
echo "Warning: No label found for $device. Skipping." | |
elif uuid_in_exclude_list "$uuid"; then | |
echo "Skipping $device (UUID: $uuid) based on exclusion list." | |
fi | |
done <<< "$blkid_output" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment