@captainfunk try this
#!/bin/bash
#
# Usage:
# To install and set up: ./waydroid_shared_folder.sh --install
# To perform mount only: ./waydroid_shared_folder.sh --mount (used by cron)
# To perform chown only: ./waydroid_shared_folder.sh --chown (used by cron)
# To uninstall: ./waydroid_shared_folder.sh --uninstall
# To display help: ./waydroid_shared_folder.sh --help
#
# --- Configuration ---
# Define the paths for the shared folders.
# ~/shared is the folder on your Ubuntu Touch host that you will use.
SHARED_DIR_HOST="$HOME/shared"
# ~/.local/share/waydroid/data/media/0/shared is the corresponding folder inside Waydroid.
SHARED_DIR_WAYDROID="$HOME/.local/share/waydroid/data/media/0/shared"
# The primary user on Ubuntu Touch.
USER_PHABLET="phablet"
# The name of this script, used for cron job identification.
SCRIPT_NAME=$(basename "$0")
# The absolute path to this script, essential for cron to execute it correctly.
SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)/$SCRIPT_NAME"
# --- Functions ---
# Function for logging messages to stdout with a timestamp.
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# Function to create the necessary shared directories if they don't exist.
create_directories() {
log "Creating shared directories..."
# Create the host shared directory.
mkdir -p "$SHARED_DIR_HOST"
# Create the Waydroid internal shared directory path.
mkdir -p "$SHARED_DIR_WAYDROID"
if [ $? -eq 0 ]; then
log "Directories created successfully."
else
log "Error creating directories. Please check permissions or disk space. Exiting."
exit 1
fi
}
# Function to perform the bind mount operation.
perform_bind_mount() {
log "Attempting to bind mount '$SHARED_DIR_HOST' to '$SHARED_DIR_WAYDROID'..."
# Check if the Waydroid shared folder is already mounted to avoid errors.
if mountpoint -q "$SHARED_DIR_WAYDROID"; then
log "Folder '$SHARED_DIR_WAYDROID' is already mounted. Skipping bind mount."
else
# Use sudo to perform the bind mount, linking the host folder to the Waydroid folder.
sudo mount --bind "$SHARED_DIR_HOST" "$SHARED_DIR_WAYDROID"
if [ $? -eq 0 ]; then
log "Bind mount successful."
# Immediately set initial permissions after a successful mount.
set_folder_permissions
else
log "Error: Bind mount failed. This could be due to incorrect paths, Waydroid not running, or insufficient sudo permissions."
log "Ensure Waydroid is running and '$SHARED_DIR_WAYDROID' exists inside its 'data/media/0' structure."
exit 1
fi
fi
}
# Function to unmount the shared folder.
unmount_folder() {
log "Attempting to unmount '$SHARED_DIR_WAYDROID'..."
# Check if the folder is currently mounted before attempting to unmount.
if mountpoint -q "$SHARED_DIR_WAYDROID"; then
# Use sudo to unmount the folder.
sudo umount "$SHARED_DIR_WAYDROID"
if [ $? -eq 0 ]; then
log "Unmount successful."
else
log "Error unmounting folder. This may happen if files inside are in use."
log "You might need to manually unmount or reboot if it persists."
exit 1
fi
else
log "Folder '$SHARED_DIR_WAYDROID' is not mounted. Skipping unmount."
fi
}
# Function to set the ownership of the shared folder and its contents.
set_folder_permissions() {
log "Setting permissions for '$SHARED_DIR_WAYDROID' to '$USER_PHABLET:$USER_PHABLET'..."
# Use sudo and the -R (recursive) flag to ensure all files and subdirectories
# within the mounted shared folder are owned by the 'phablet' user.
# This directly addresses the issue of Waydroid changing ownership.
sudo chown -R "$USER_PHABLET":"$USER_PHABLET" "$SHARED_DIR_WAYDROID"
if [ $? -eq 0 ]; then
log "Permissions set successfully."
else
log "Error setting permissions. Check sudo permissions or ensure the path '$SHARED_DIR_WAYDROID' is valid."
exit 1
fi
}
# Function to add cron jobs for persistence and continuous permission management.
add_cron_jobs() {
log "Adding cron jobs for persistence and continuous permission management..."
# Get current crontab, add new entries, sort, remove duplicates, then set new crontab.
# @reboot: Executes the script with '--mount' option every time the device reboots.
# * * * * *: Executes the script with '--chown' option every minute to correct permissions.
(crontab -l 2>/dev/null; \
echo "@reboot /bin/bash \"$SCRIPT_PATH\" --mount"; \
echo "* * * * * /bin/bash \"$SCRIPT_PATH\" --chown" \
) | sort | uniq | crontab -
if [ $? -eq 0 ]; then
log "Cron jobs added successfully."
log "They will run from this script's current location: $SCRIPT_PATH"
log "IMPORTANT: Do NOT move or rename this script after installation, or the cron jobs will fail."
log "You can verify the cron jobs by running: crontab -l"
else
log "Error adding cron jobs. This could be a cron service issue."
exit 1
fi
}
# Function to remove cron jobs added by this script.
remove_cron_jobs() {
log "Removing cron jobs related to this script..."
# List current cron jobs, filter out lines containing this script's path, and set the new crontab.
crontab -l 2>/dev/null | grep -v "$SCRIPT_PATH" | crontab -
if [ $? -eq 0 ]; then
log "Cron jobs removed successfully. You can verify by running: crontab -l"
else
log "Error removing cron jobs. You may need to manually edit your crontab (crontab -e)."
exit 1
fi
}
# Function to display the help message.
display_help() {
echo "Usage: $SCRIPT_NAME [OPTION]"
echo ""
echo "This script automates setting up and managing a shared folder between Ubuntu Touch and Waydroid."
echo ""
echo "Options:"
echo " --install Performs initial setup: creates directories, mounts the folder,"
echo " sets initial permissions, and adds cron jobs for persistence and"
echo " continuous permission handling. Run this command once to set up."
echo ""
echo " --mount Performs only the bind mount operation and sets initial permissions."
echo " This option is primarily used by the '@reboot' cron job for persistence."
echo ""
echo " --chown Recursively sets permissions for the shared folder and its contents"
echo " to 'phablet:phablet'. This option is used by the periodic cron job"
echo " to automatically correct permissions changed by Waydroid."
echo ""
echo " --uninstall Removes the cron jobs added by this script and unmounts the shared folder."
echo " This cleans up the setup."
echo ""
echo " --help Display this help message."
echo ""
echo "Important Notes:"
echo " - This script assumes you are running it as the 'phablet' user on Ubuntu Touch."
echo " - It relies on 'sudo' for mount and chown commands. The 'phablet' user typically has"
echo " passwordless sudo access configured by default on Ubuntu Touch."
echo " - After running '--install', a reboot is recommended to ensure the @reboot cron job fires."
echo " - Do not move or rename this script after installation, as the cron jobs rely on its path."
echo " - While '/etc/fstab' is a common Linux method for persistence, it's generally not recommended"
echo " for user-level modifications on Ubuntu Touch due to its read-only root filesystem and OTA updates."
echo " Using user-specific cron jobs provides a safer and more robust solution here."
}
# --- Main Logic ---
# Check if the script is being run as root. It should be run as the regular user (phablet).
if [[ $EUID -eq 0 ]]; then
log "Error: This script should NOT be run directly with 'sudo'."
log "Please run it as the '$USER_PHABLET' user (e.g., just './$SCRIPT_NAME --install')."
log "The script will use 'sudo' internally where necessary."
exit 1
fi
# Check for essential commands required by the script.
if ! command -v crontab &> /dev/null; then
log "Error: 'crontab' command not found. Please ensure cron is installed and available."
exit 1
fi
if ! command -v mountpoint &> /dev/null; then
log "Error: 'mountpoint' command not found. Please ensure it's installed (usually part of 'util-linux')."
exit 1
fi
# Process command-line arguments.
case "$1" in
--install)
log "Initiating installation process..."
create_directories # Create the required host and Waydroid directories.
perform_bind_mount # Perform the initial bind mount.
add_cron_jobs # Set up cron jobs for persistence and auto-chown.
log "Installation complete. Please reboot your Ubuntu Touch device for all changes to take full effect."
log "The shared folder will now be persistent across reboots, and permissions will be automatically corrected."
;;
--mount)
log "Executing mount operation (likely from @reboot cron job)..."
create_directories # Ensure directories exist (in case they were deleted).
perform_bind_mount # Perform the bind mount.
;;
--chown)
log "Executing chown operation (likely from periodic cron job)..."
# Only attempt chown if the target directory exists and is mounted.
if [ -d "$SHARED_DIR_WAYDROID" ] && mountpoint -q "$SHARED_DIR_WAYDROID"; then
set_folder_permissions # Correct the permissions recursively.
else
log "Shared Waydroid directory '$SHARED_DIR_WAYDROID' not found or not mounted. Skipping chown for now."
log "This might happen if Waydroid is not running or if the mount failed."
fi
;;
--uninstall)
log "Initiating uninstallation process..."
remove_cron_jobs # Remove the cron jobs.
unmount_folder # Unmount the shared folder.
log "Uninstallation complete. You may manually remove the '$SHARED_DIR_HOST' directory if no longer needed."
;;
--help)
display_help # Show the help message.
;;
*)
log "Invalid option: '$1'"
display_help
exit 1
;;
esac
log "Script execution finished."