Skip to content

Instantly share code, notes, and snippets.

@AlekEagle
Created August 9, 2019 23:35
Show Gist options
  • Save AlekEagle/0ef771053e858ae11fa28808e6bee10e to your computer and use it in GitHub Desktop.
Save AlekEagle/0ef771053e858ae11fa28808e6bee10e to your computer and use it in GitHub Desktop.
#!/bin/bash
user="${SUDO_USER:-$(whoami)}"
command -v dmenu > /dev/null 2>&1
if [ $? == 1 ]
then
echo "The command dmenu was not found please install it using \"sudo apt install dmenu\""
exit 1
else
drive="/dev/$(lsblk -o NAME,MOUNTPOINT | awk '{if(NR>1)print}' | sed '/\(loop\|\/$\)/d' | sed '/\//!d' | cut -c 7- | dmenu -l 35 | awk '{print $1;}')"
if [ $drive == "/dev/" ]
then
echo "You didn't specify a drive to enable plex access on. Exiting.."
exit 1
else
driveuuid=$(blkid $drive | sed -e 's!.* UUID=\"\(\([0-9a-zA-Z]\|-\)\+\)\".*!\1!g')
if [ -z $driveuuid ]
then
echo "The drive you specified either does not exist, or isn't detected my the computer. Please mount the disk and any partitions you want accessible to plex before continuing."
exit 1
else
read -p "What do you want to call the drive when the drive mounts to the filesystem in the media folder? ex: /media/$user/" name
read -p "Would you like to create a backup of the fstab file in case something goes terribly wrong? (Y/n)" backup
if [ "$backup" != "n" ]
then
printf $(cat /etc/fstab) > /tmp/fstabbackup
if [ $? == 1 ]
then
read -p "I was unable to successfully create the backup, it isn't recommended to continue further. Would you like to continue anyway? (y/N)" cont
if [ "$cont" != "y" ]
then
exit 1
else
echo "Continuing.."
fi
else
echo "Successfully backed up."
fi
fi
printf "$(cat /etc/fstab | sed "/$driveuuid/d")\n/dev/disk/by-uuid/$driveuuid /media/$user/$name auto nosuid,nodev,nofail 0 0" > /etc/fstab
if [ $? == 1 ]
then
echo "I wasn't able to update the fstab file with the proper data. Please make sure you run this script as sudo before continuing."
exit 1
else
umount $drive
umountpid=$!
if [ $? == 1 ]
then
echo "Some unknown error occurred while un-mounting the drive. Please make sure the drive is not in use while performing this operation."
exit 1
else
wait $umountpid
mkdir /media/$user/$name
mount /dev/disk/by-uuid/$driveuuid
if [ $? == 1 ]
then
echo "Some unknown error occurred while mounting the drive. We are unsure of what caused the error, but it could be because the fstab file might've become corrupted. Restoring backup if one was created."
tmpfstab=$(cat /etc/fstab)
printf $(cat /tmp/fstabbackup) > /etc/fstab
if [ $? == 2 ]
then
echo "No backup, keeping current fstab. The script will now exit."
printf $tmpfstab > /etc/fstab
else
echo "Successfully restored the backup. The script will now exit."
exit 1
fi
else
echo "Successfully re-mounted the drive. The script will now apply the proper permissions for plex."
chmod go+rx /media/$user
if [ $? != 0 ]
then
echo "Failed to give permissions, exiting"
exit 1
fi
chmod go+rx /media/$user/$name
if [ $? != 0 ]
then
echo "Failed to give permissions, exiting"
exit 1
fi
setfacl -m g:plex:rx /media/$user
if [ $? != 0 ]
then
echo "Failed to give permissions, exiting"
exit 1
fi
read -p "What is the path to the folder that the plex media will reside at? (just hit enter if there is no subfolder called something like \"Media\" where all of the plex content resides) ex: /media/$user/$name/" folder
chmod -R +rwX /media/$user/$name/$folder
if [ $? != 0 ]
then
echo "Failed to give permissions, exiting"
exit 1
fi
echo "Permissions should be properly set!"
fi
fi
fi
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment