Skip to content

Instantly share code, notes, and snippets.

@KraXen72
Last active July 25, 2024 17:13
Show Gist options
  • Save KraXen72/24d8e58b1b7a921c0df1dd5524af0f5c to your computer and use it in GitHub Desktop.
Save KraXen72/24d8e58b1b7a921c0df1dd5524af0f5c to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if jmtpfs is installed
if ! command -v jmtpfs &> /dev/null; then
echo "The command 'jmtpfs' is not found."
echo "Please install 'jmtpfs' using your distribution's package manager."
echo "For example, on Debian-based systems (e.g., Ubuntu): sudo apt install jmtpfs"
echo "On Red Hat-based systems: sudo yum install jmtpfs"
exit 1
fi
# Check if the mount directory argument is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <mount-dir>"
exit 1
fi
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
RESET="\e[0m"
# Set the mount directory from the argument
mount_dir="$1"
# Global array to store process IDs
declare -a occupying_processes
# Function to split a string into an array by word boundaries
split_string() {
local input_string="$1"
local -n result_array=$2
# Use `grep` with regex to split the string into words
mapfile -t result_array < <(echo "$input_string" | grep -oP '\b\w+\b')
}
# Function to find the MTP device and extract bus and device ID
find_mtp_device() {
local mtp_device=$(lsusb | grep -i 'mtp')
declare -a parts
split_string "$mtp_device" parts
mtp_bus=${parts[1]}
mtp_id=${parts[3]}
echo "$mtp_device, bus:$mtp_bus id:$mtp_id"
}
# Function to find processes occupying the MTP device
find_occupying_processes() {
if [ -z "$mtp_bus" ] || [ -z "$mtp_id" ]; then
echo "MTP bus or device ID is not set. Cannot find occupying processes."
return 1
fi
device_path="/dev/bus/usb/$mtp_bus/$mtp_id"
echo "Finding processes using $device_path"
# Use fuser to find processes using the device path
occupying_processes=($(fuser "$device_path" 2>/dev/null))
if [ ${#occupying_processes[@]} -eq 0 ]; then
echo "No processes are using the device."
else
echo "Processes using the device:"
for pid in "${occupying_processes[@]}"; do
local process_info=$(ps -fp "$pid" | tail -n +2) # Skip the header line
echo "$process_info"
done
fi
}
# Function to kill processes using the MTP device
kill_occupying_processes() {
if [ ${#occupying_processes[@]} -eq 0 ]; then
echo "No processes to kill."
return 1
fi
echo "Killing processes:"
for pid in "${occupying_processes[@]}"; do
echo "Killing process $pid"
kill -9 "$pid"
done
}
# Function to create the mount directory in the user's home if it doesn't exist
make_mount_dir() {
# Ensure directory creation and unmounting are done as root
fusermount -uz "$mount_dir" 2>/dev/null
if [ ! -d "$mount_dir" ]; then
echo "Creating mount directory $mount_dir"
mkdir -p "$mount_dir"
else
echo "Mount directory $mount_dir already exists."
fi
}
# Function to mount the device and handle potential errors
mount_device() {
local attempt=0
local max_attempts=5
local jmtpfs_output
while [ $attempt -lt $max_attempts ]; do
echo "Attempting to mount device with jmtpfs..."
jmtpfs_output=$(jmtpfs "$mount_dir" 2>&1)
echo "$mount_dir $jmtpfs_output"
# Check if the output indicates the device is already in use
if echo "$jmtpfs_output" | grep -q 'device is busy'; then
echo -e "${YELLOW}Device is already in use. Attempting to find and kill occupying processes...${RESET}"
find_occupying_processes
kill_occupying_processes
attempt=$((attempt + 1))
elif echo "$jmtpfs_output" | grep -q 'Transport endpoint is not connected'; then
echo -e "${RED}Transport endpoint error. Retrying...${RESET}"
sudo fusermount -uz "$mount_dir"
attempt=$((attempt + 1))
elif [ -z "$jmtpfs_output" ]; then
# If there's no output, assume success
echo -e "${GREEN}Device mounted successfully.${RESET}"
break
else
# If there is some other error output, print it and retry
echo -e "${RED}Error mounting device: ${jmtpfs_output}${RESET}"
attempt=$((attempt + 1))
fi
if [ $attempt -ge $max_attempts ]; then
echo -e "${RED}Failed to mount the device after $max_attempts attempts.${RESET}"
exit 1
fi
done
}
# Main script execution
main() {
find_mtp_device
make_mount_dir
mount_device
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment