1

I am using a persistent live USB running Ubuntu 18.04.3 LTS Bionic Beaver, as partitioning my hard drive is very difficult. When playing Steam games, I need a swapfile. With help from @malte-skoruppa, I am now using a swapfile on an NTFS partition on the internal hard drive.

I would now like to automate the process using my first Bash script, based on a HOWTO. Can you see any flaws in this script, please?:

#!/bin/bash
echo "Mounting partition..."
sudo mount -U 80EA3F58EA3F49A4 /media/ubuntu/LENOVO
echo "Opening swapfile..."
sudo swapon /media/ubuntu/LENOVO/swapfile.img
steam steam://open/minigameslist
echo "Closing swapfile..."
sudo swapoff /media/ubuntu/LENOVO/swapfile.img
echo "Unmounting partition..."
sudo umount -l /media/ubuntu/LENOVO
echo "Mission accomplished!"

I am especially concerned about the umount -l option. The HOWTO says that it will unmount the partition once it's not busy, but an old Stack Overflow answer says that it force-unmounts the partition, which is NOT what I want to do.

  • 1
    You don't need that -l switch for umount. As the man page of umount says, "(...) The recommended use-case for umount -l is to prevent hangs on shutdown due to an unreachable network share (...)". You can simply use umount /media/ubuntu/LENOVO, without that option. Don't worry. It's not going to destroy anything on the partition, but finish all pending I/O operations first. It's like the "Safely remove hardware" icon on Windows. ;) Another flaw that I see in your script is that all commands are executed in sequence, without any checks whether the previous command was successful. – Malte Skoruppa Nov 15 '19 at 16:11
1

Since I know you're new to Ubuntu, here's a few general comments.

1. Put that partition in your /etc/fstab

First off, in your script you use the line sudo mount -U 80EA3F58EA3F49A4 /media/ubuntu/LENOVO to mount your NTFS partition. I would suggest to add that partition to your /etc/fstab and map it to the UID of your partition there. Since you're on a single-user system, you may want to have an entry similar to this in your /etc/fstab:

UUID=80EA3F58EA3F49A4 /media/ubuntu/LENOVO ntfs rw,exec,uid=1000,gid=1000,umask=0002,nls=utf8,noauto  0  0

Most importantly, this tells the system to bind the mount point /media/ubuntu/LENOVO to the UID 80EA3F58EA3F49A4. That way, in the future (and thus in your script) you can simply use sudo mount /media/ubuntu/LENOVO instead of sudo mount -U 80EA3F58EA3F49A4 /media/ubuntu/LENOVO. If the UID changes in the future (because you reformat the partition), it's better to have only one central place, /etc/fstab, to edit rather than 20 scripts you may have lying around.

This also mounts the filesystem with various common options for NTFS partitions: read-write mode (rw), permit the execution of binaries (exec), make all files and directories on the file system belong to the user 1000 and group 1000 (uid=1000,gid=1000) (that's your normal user), set the permissions bits for all files and directories to 775 (umask=0002), set the character set for filenames to UTF-8 (nls=utf8), and do not mount the partition automatically (noauto). Refer to fstab(5) and mount(8) for more information. I personally find it convenient on my single-user system to be able to access all files on my NTFS partitions without having to use sudo, but of course your mileage may vary.

You may also want to drop the noauto option. Then, the partition will be mounted whenever the command mount -a is run, and in particular, at boot time (and thus, it would typically be mounted the whole time). Then you wouldn't even have to use the mount and umount commands in your script at all, but I don't know if you want that.

2. Don't use umount with -l

From umount(8):

The recommended use-case for umount -l is to prevent hangs on shutdown due to an unreachable network share where a normal umount will hang due to a downed server or a network partition.

So this option is at least rather unusual to use in your case.

Whether you run umount with or without -l, a partition will not be force-unmounted (there's the -f option for that). Rather, when you don't use -l and the partition is still busy, the unmount will simply fail and return an error message; when using -l and the partition is still busy, it will be detached from the filesystem hiearchy now (meaning you cannot access it anymore), but pending I/O operations will be silently completed in the background.

Since there shouldn't be any pending I/O operations after you've run swapoff, by not using -l you would at least notice if something is odd, because if the partition is unexpectedly still busy then the unmount command would output an error message.

3. Add error handling

You execute all commands in sequence, but don't check whether the previous command worked. So, e.g., if the mounting fails, then the swapon command will be nevertheless attempted but fail, and then your game will start even though the swap was not activated.

Try this:

#!/bin/bash

echo "Mounting partition..."
sudo mount /media/ubuntu/LENOVO

if [ $? -ne 0 ]; then
    echo "Could not mount partition, mission aborted." >&2
    exit 1
fi

echo "Opening swapfile..."
sudo swapon /media/ubuntu/LENOVO/swapfile.img

if [ $? -ne 0 ]; then
    echo "Could not activate swap, mission aborted." >&2
    exit 1
fi

steam steam://open/minigameslist

echo "Closing swapfile..."
sudo swapoff /media/ubuntu/LENOVO/swapfile.img

echo "Unmounting partition..."
sudo umount /media/ubuntu/LENOVO

echo "Mission accomplished!"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.