if test $blkstatus -eq 0
then
blklocation="/dev/mmcblk1p1"
blklocationforsed="dev/mmcblk1p1"
else
blklocation="/dev/mmcblk1"
blklocationforsed="dev/mmcblk1"
fi
filesystem=lsblk -no FSTYPE $blklocation
if [ "$filesystem" = "ntfs" ]; then
filesystem="ntfs-3g"
fi
sudo umount /opt/system/Tools
if [ "filesystem"="ext4"];thenfilesystem filesystem $blklocation /roms2 -o umask=0000,iocharset=utf8,uid=1002,gid=1002
fi
if [ ! -d "/roms2/videos/" ]; then
sudo mkdir /roms2/videos
fi
sudo mount /roms2/tools /opt/system/Tools
sed -i '//roms//s///roms2//' /etc/emulationstation/es_systems.cfg
if [ "filesystem"="ext4"];thensudosed−i′a/'"blklocationforsed"′\/roms2filesystem"' defaults,nofail,x-systemd.device-timeout=7 0 1' /etc/fstab
else
sudo sed -i 'a\/′"blklocationforsed"' /roms2 '"filesystem"' umask=0000,iocharset=utf8,noatime,nofail,x-systemd.device-timeout=7,uid=1002,gid=1002 0 0' /etc/fstab
fi
sudo sed -i '/roms\/tools/s//roms2\/tools/' /etc/fstab
sudo sed -i '/roms\/pico-8/s//roms2\/pico-8/g' /usr/local/bin/pico8.sh
sudo sed -i '/roms\//s//roms2\//g' /usr/local/bin/scummvm.sh
sudo sed -i '/roms\//s//roms2\//g' /usr/local/bin/ti99.sh
sudo sed -i '/roms\//s//roms2\//g' /usr/local/bin/dreamcast.sh
sudo sed -i '/roms\//s//roms2\//g' /usr/local/bin/doom.sh
sudo sed -i '/roms\//s//roms2\//g' /usr/local/bin/saturn.sh
sudo sed -i '/roms\//s//roms2\//g' /usr/local/bin/atomiswave.sh
sudo sed -i '/roms\//s//roms2\//g' /usr/local/bin/naomi.sh
sudo sed -i '/roms\//s//roms2\//g' /usr/local/bin/singe.sh
sudo sed -i '/roms\//s//roms2\//g' /usr/local/bin/easyrpg.sh
sudo sed -i '/roms\//s//roms2\//g' /usr/local/bin/ecwolf.sh
sudo sed -i '/roms\//s//roms2\//g' /usr/local/bin/drastic.sh
sudo sed -i '/roms\//s//roms2\//g' /usr/local/bin/ppsspp.sh
sudo sed -i '/roms\//s//roms2\//g' /opt/system/PS1\ -\ Generate\ m3u\ files.sh
sudo sed -i '/roms\//s//roms2\//g' /opt/system/PS1\ -\ Delete\ m3u\ files.sh
sed -i '/roms\//s//roms2\//' /home/ark/.CommanderGenius/cgenius.cfg
sed -i '/roms\//s//roms2\//g' /home/ark/.config/retroarch/retroarch.cfg
sed -i '/roms\//s//roms2\//g' /home/ark/.config/retroarch32/retroarch.cfg
sed -i '/roms\//s//roms2\//g' /home/ark/.atari800.cfg
sed -i '/roms\//s//roms2\//g' /home/ark/.config/retroarch/config/Atari800/retroarch_5200.cfg
sed -i '/roms\//s//roms2\//g' /home/ark/.config/retroarch/config/Atari800/retroarch_A800.cfg
sed -i '/roms\//s//roms2\//g' /home/ark/.config/retroarch/config/Atari800/retroarch_XEGS.cfg
sed -i '/roms\//s//roms2\//g' /home/ark/.config/mupen64plus/mupen64plus.cfg
sed -i '/roms\//s//roms2\//g' /home/ark/.config/duckstation/settings.ini
sed -i '/roms\/bios/s//roms2\/bios/g' /opt/amiberry/conf/amiberry.conf
sed -i '/.\/351Files 2/s//.\/351Files-sd2 2/g' /opt/system/351Files.sh
sudo sed -i '/roms\//s//roms2\//g' /usr/local/bin/scummvm.sh
sudo cp /usr/local/bin/Switch\ to\ main\ SD\ for\ Roms.sh /opt/system/Advanced/.
sudo rm /opt/system/Advanced/Switch\ to\ SD2\ for\ Roms.sh
sudo tar --strip-components=1 -xvkf /roms.tar -C /roms2/
#sudo mv -v -f -n /roms2/roms/* /roms2/
unlink /opt/hypseus/roms
ln -sfv /roms2/daphne/roms/ /opt/hypseus/roms
unlink /opt/hypseus-singe/singe
if [ ! -d "/roms2/alg/" ]; then
sudo mkdir /roms2/alg
fi
sudo cp -f -v -r /roms/alg/Scan_for_new_games.alg /roms2/alg/Scan_for_new_games.alg
sudo sed -i '/roms\//s//roms2\//g' /roms2/easyrpg/Scan_for_new_games.alg
ln -sfv /roms2/alg/ /opt/hypseus-singe/singe
unlink /home/ark/.emulationstation/music
ln -sfv /roms2/bgmusic/ /home/ark/.emulationstation/music
sudo sed -i '/roms\/scummvm/s//roms2\/scummvm/g' /roms2/scummvm/Scan_for_new_games.scummvm
sudo touch /roms2/scummvm/Scan_for_new_games.scummvm
sudo sed -i '/roms\/scummvm/s//roms2\/scummvm/g' /roms2/bios/scummvm.ini
sudo sed -i '/roms\/bios/s//roms2\/bios/g' /roms2/bios/scummvm.ini
sed -i '/roms\//s//roms2\//g' /home/ark/.config/scummvm/scummvm.ini
sed -i '/roms\//s//roms2\//g' /home/ark/.config/scummvm/scummvm.ini
sudo sed -i '/roms\//s//roms2\//g' /roms2/alg/Scan_for_new_games.alg
sudo cp -f -v -r /roms/easyrpg/Scan_for_new_games.easyrpg /roms2/easyrpg/Scan_for_new_games.easyrpg
sudo sed -i '/roms\//s//roms2\//g' /roms2/easyrpg/Scan_for_new_games.easyrpg
sudo cp -f -v -r /roms/wolf/Scan_for_new_games.wolf /roms2/wolf/Scan_for_new_games.wolf
sudo sed -i '/roms\//s//roms2\//g' /roms2/wolf/Scan_for_new_games.wolf
sudo sed -i '/roms\//s//roms2\//g' /roms2/psx/duckstation_gamesettings/*.ini
sudo cp -f -r -v /roms/ports/* /roms2/ports/
unlink /opt/drastic/backup
ln -sf /roms2/nds/backup/ /opt/drastic/backup
unlink /opt/drastic/cheats
ln -sf /roms2/nds/cheats/ /opt/drastic/cheats
unlink /opt/drastic/savestates
ln -sf /roms2/nds/savestates/ /opt/drastic/savestates
unlink /opt/drastic/slot2
ln -sf /roms2/nds/slot2/ /opt/drastic/slot2
unlink /home/ark/.config/ppsspp
ln -sf /roms2/psp/ppsspp/ /home/ark/.config/ppsspp
#sudo rm -rf /roms2/roms/
sudo cp -f /etc/samba/smb.conf.sd2 /etc/samba/smb.conf
if [ "filesystem" = "ext4" ]; then
sudo chown -R ark:ark /roms2/
fi
if [ ! -d "/roms2/wasm4/" ]; then
sudo mkdir /roms2/wasm4
fi
if [ ! -d "/roms2/thomson/" ]; then
sudo mkdir /roms2/thomson
fi
if [ ! -d "/roms2/enterprise/" ]; then
sudo mkdir /roms2/enterprise
fi
if [ ! -d "/roms2/tvc/" ]; then
sudo mkdir /roms2/tvc
fi
if [ ! -d "/roms2/palm/" ]; then
sudo mkdir /roms2/palm
fi
if [ ! -d "/roms2/coco3/" ]; then
sudo mkdir /roms2/coco3
fi
if [ ! -d "/roms2/videopac/" ]; then
sudo mkdir /roms2/videopac
fi
if [ ! -d "/roms2/apple2/" ]; then
sudo mkdir /roms2/apple2
fi
if [ ! -d "/roms2/piece/" ]; then
sudo mkdir /roms2/piece
fi
if [ ! -d "/roms2/scv/" ]; then
sudo mkdir /roms2/scv
fi
if [ ! -d "/roms2/vmac/" ]; then
sudo mkdir /roms2/vmac
fi
if [ ! -d "/roms2/puzzlescript/" ]; then
sudo mkdir /roms2/puzzlescript
fi
if [ ! -d "/roms2/vircon32/" ]; then
sudo mkdir /roms2/vircon32
fi
if [ ! -d "/roms2/j2me/" ]; then
sudo mkdir /roms2/j2me
fi
sudo pkill filebrowser
filebrowser -d /home/ark/.config/filebrowser.db users update ark --scope "/roms2"
printf "\n\n\e[32m$filesystem sdcard in slot2 is mounted to /roms2...\n"
printf "\033[0m"
sleep 3
printf "\033c" >> /dev/tty1
sudo swapoff /swapfile
sudo rm -f -v /swapfile
sudo systemctl restart emulationstation
else
printf "\n\n\e[91mCould not find a Fat, Fat32, Exfat, EXT4, or NTFS based sdcard to mount from slot2...\n"
printf "\033[0m"
sleep 5
printf "\033c" >> /dev/tty1
fi
Certainly! I'd be happy to help you understand and analyze the Bash script you've provided. This script appears to be designed for a Linux-based gaming or emulation system, possibly like those found on handheld gaming devices or retro gaming setups. The script automates the process of mounting a secondary SD card, setting up directories, adjusting configurations, and ensuring that various emulators and services point to the correct paths.
Below is a step-by-step explanation of what the script does:
Script Breakdown:
1. Check and Create the /roms2 Directory:
bash
if [ ! -d "/roms2/" ]; then
sudo mkdir /roms2
fi
Purpose: Ensures that the /roms2 directory exists.
Action: If /roms2 does not exist, it creates it using sudo (superuser) privileges.
2. Identify the SD Card Device and Filesystem:
bash
blkchk=`lsblk -no FSTYPE /dev/mmcblk1p1`
blkstatus=$?
if test $blkstatus -eq 0
then
blklocation="/dev/mmcblk1p1"
blklocationforsed="dev\/mmcblk1p1"
else
blklocation="/dev/mmcblk1"
blklocationforsed="dev\/mmcblk1"
fi
Purpose: Determines the correct block device representing the secondary SD card.
Action:
Attempts to get the filesystem type of /dev/mmcblk1p1.
If successful (blkstatus equals 0), it sets blklocation to /dev/mmcblk1p1.
If not, it assumes the device is /dev/mmcblk1.
Prepares blklocationforsed for use in sed commands by escaping the forward slash.
3. Determine the Filesystem Type:
bash
filesystem=`lsblk -no FSTYPE $blklocation`
if [ "$filesystem" = "ntfs" ]; then
filesystem="ntfs-3g"
fi
Purpose: Identifies the filesystem of the SD card to ensure proper mounting.
Action:
Retrieves the filesystem type of the block device.
If the filesystem is NTFS, it changes filesystem to ntfs-3g to use the appropriate driver.
4. Unmount Existing Mount Points:
bash
sudo umount /opt/system/Tools
Purpose: Unmounts the Tools directory to prepare for remounting.
5. Mount the SD Card to /roms2:
bash
if [ "$filesystem" = "ext4" ]; then
sudo mount -t $filesystem $blklocation /roms2
else
sudo mount -t $filesystem $blklocation /roms2 -o umask=0000,iocharset=utf8,uid=1002,gid=1002
fi
Purpose: Mounts the SD card to the /roms2 directory using the appropriate options based on the filesystem.
Action:
For ext4, it mounts with default options.
For other filesystems, it sets specific mount options to handle permissions and character encoding.
6. Check if Mount Was Successful:
bash
status=$?
if test $status -eq 0
then
# Proceed with setup
else
# Print an error message
fi
Purpose: Determines if the mount operation was successful.
Action: If the mount was successful, it proceeds; otherwise, it displays an error message.
Purpose: Creates a temporary swapfile to assist with memory-intensive operations during the script execution.
Action:
Creates a swapfile of 256MB (262,144 blocks of 1024 bytes).
Sets up and activates the swapfile.
8. Create Necessary Directories on the SD Card:
bash
if [ ! -d "/roms2/videos/" ]; then
sudo mkdir /roms2/videos
fi
Purpose: Ensures that essential directories exist on the SD card.
Action: Creates the /roms2/videos directory if it doesn't already exist.
9. Mount the tools Directory:
bash
sudo mount /roms2/tools /opt/system/Tools
Purpose: Mounts the tools directory from the SD card to the system's tools directory.
10. Update EmulationStation's Configuration:
bash
sed -i '/<path>\/roms\//s//<path>\/roms2\//' /etc/emulationstation/es_systems.cfg
Purpose: Modifies the EmulationStation systems configuration to point to the new ROMs directory.
11. Update /etc/fstab for Persistent Mounts:
bash
if [ "$filesystem" = "ext4" ]; then
sudo sed -i '$a\/'"$blklocationforsed"' \/roms2 '"$filesystem"' defaults,nofail,x-systemd.device-timeout=7 0 1' /etc/fstab
else
sudo sed -i '$a\/'"$blklocationforsed"' \/roms2 '"$filesystem"' umask=0000,iocharset=utf8,noatime,nofail,x-systemd.device-timeout=7,uid=1002,gid=1002 0 0' /etc/fstab
fi
Purpose: Adds an entry to /etc/fstab to ensure the SD card is mounted to /roms2 at boot.
Action: Uses appropriate mount options based on the filesystem type.
12. Update Paths in Various Scripts and Configurations:
Purpose: Modifies scripts and configuration files to change references from /roms/ to /roms2/.
Examples:
bash
sudo sed -i '/roms\/tools/s//roms2\/tools/' /etc/fstab
sudo sed -i '/roms\/pico-8/s//roms2\/pico-8/g' /usr/local/bin/pico8.sh
sed -i '/roms\//s//roms2\//g' /home/ark/.config/retroarch/retroarch.cfg
Action: Uses sed to perform in-place substitutions across multiple files.
Purpose: Disables and removes the temporary swapfile and restarts EmulationStation to apply changes.
20. Handle Mount Failure:
bash
else
printf "\n\n\e[91mCould not find a Fat, Fat32, Exfat, EXT4, or NTFS based sdcard to mount from slot2...\n"
printf "\033[0m"
sleep 5
printf "\033c" >> /dev/tty1
fi
Purpose: If the SD card could not be mounted, it informs the user.
Additional Notes and Suggestions:
User and Group IDs: The script uses uid=1002,gid=1002 when mounting non-ext4 filesystems. Ensure that these IDs correspond to the correct user and group on your system (in this case, likely the ark user).
Permissions: For ext4 filesystems, the script recursively changes ownership of /roms2 to ark:ark. This ensures that the ark user has the necessary permissions.
bash
if [ "$filesystem" = "ext4" ]; then
sudo chown -R ark:ark /roms2/
fi
Swapfile Usage: The temporary swapfile is created to provide additional memory during the execution of the script, possibly due to memory-intensive operations like extracting large archives.
Backup Important Files: Before running scripts that modify system configurations or user data, it's a good practice to back up important files.
Script Execution: Ensure that the script is executed with the necessary permissions and in an environment where commands like sudo are available.
Testing: Consider testing the script in a safe environment before deploying it to a production system to prevent potential data loss.
Error Handling: While the script checks the success of the mount operation, it might be beneficial to add additional error checks after critical commands to handle unexpected failures gracefully.
Summary:
The script automates the process of:
Mounting a secondary SD card to /roms2.
Adjusting various emulator and system configurations to use the new directory.
Updating symbolic links, scripts, and configuration files.
Setting up directories for emulators.
Handling permissions and user scopes.
Cleaning up after execution and restarting services to apply changes.
This allows emulators and tools on the system to access ROMs and resources from the secondary SD card seamlessly.
If you have any questions about specific parts of the script or need further assistance with modifications or troubleshooting, feel free to ask!