Safely removing a USB drive with a bash script

In a previous post (Safely removing a USB flash drive) I noted that, when removing an external USB drive, Linux Mint 18.2 (Sonya) provided a message indicating that “It is now safe to remove the drive”. However, I sometimes noticed that, even though this message was displayed, the red LED on my USB flash drive was still flashing, indicating that disk activity was still underway such that it wasn’t necessarily safe to pull the plug. Furthermore, the safe-to-remove message does not occur in Linux Mint 18.3 (Sylvia) – perhaps because it didn’t really work?

This led me to revisit the idea of a bash script that would wait for any cached data to be written to the USB drive before indicating that it was indeed safe to remove the external drive. I think I have now created such a bash script that has the desired effect.

The script undertakes multiple tasks. Firstly, it calls the sync command to ensure that any disk writes are complete before proceeding. Sync doesn’t return control to the main script file until any/all cached data have been written to the USB drive. Secondly, a combination of lsblk and grep is used to identify the device name for the USB drive of interest. This is necessary since if multiple USB devices are plugged into the computer, the device name (e.g. /dev/sg1) may not always be the same. The last three characters in the device name (e.g. sg1) are saved to a “usbname” variable for use in unmounting and powering off the USB drive. Finally, the udisksctl command is used to unmount the USB drive and then power it off.

The specific code used is as follows:

#! /bin/bash

# Eject USB drive once buffer transfer has completed

echo "Flushing USB drive buffer"
sync

echo "sync complete"

# Identify the device name for the SILICON16GB USB drive

usblongname=$(lsblk -l | grep SILICON16GB)
usbname="${usblongname:0:4}"

# Unmount the USB drive

udisksctl unmount -b /dev/$usbname

echo "unmount complete"

# Power off the USB drive

udisksctl power-off -b /dev/$usbname

echo "power off complete"

# Script complete

echo "Shell command complete"
read

Note that the above-noted script has the name of my USB flash drive hard coded as “SILICON16GB“. This designator should be changed to suit a different user’s system. Also, udisksctl is part of the gnome-disk-utility package so this package should be installed if it is not available by default.

References:

Safely removing a USB flash drive
https://linuxnorth.wordpress.com/2017/07/04/safely-removing-a-usb-flash-drive/

Eject / safely remove vs umount
https://unix.stackexchange.com/questions/178638/eject-safely-remove-vs-umount

wait for dd command to fully write to the disk
https://askubuntu.com/questions/318186/wait-for-dd-command-to-fully-write-to-the-disk

sync(8) – Linux man page
https://linux.die.net/man/8/sync

ubuntu manuals – udisksctl – The udisks command line tool
http://manpages.ubuntu.com/manpages/trusty/man1/udisksctl.1.html

gnome-disk-utility package in Ubuntu
https://launchpad.net/ubuntu/+source/gnome-disk-utility

Advertisements
Posted in Bash script, Mint | Tagged , | Leave a comment

Updating LineageOS

One of the reasons that I installed a custom ROM on my smartphone was so that I would be able to update the operating system and maintain some level of security. And, sure enough, the installed LineageOS 14.1 provided notifications of available updates, essentially on a weekly basis.

Navigating to Settings – About phone – LineageOS updates shows a list of the available software updates (see screenshot below). An icon – a down-arrow above a horizontal line – next to each listing indicates that the file can be downloaded. Having done this for the most recent update, I thought I had to locate the downloaded file and install it. However, the trick is really to click on the now, slightly-different arrow next to the file. Clicking on the “download” arrow – now a down-arrow inside a rectangular box – a second time causes the phone to boot into recovery mode and install the update.

List of LineageOS updates

I subsequently experienced a problem with booting the newly-updated system. Error messages were popping up indicating that “Google play services keeps stopping”. The real problem here was that, without Google play services running properly, Google Play Store wouldn’t download and install any apps.

None of the fixes for this issue that were posted on the Internet seemed to make any difference. These included changing the option for “WebView implementation” and installing the latest version of Google Play Store. The solution that worked for me was to download and install the latest version of Gapps (open-gapps-arm-7.1-stock-20171208.zip).

The phone rebooted more-or-less normally. A minor issue was that it booted into the Pixel Launcher. I navigated to Settings – Apps, chose the option for the Google Launcher, and set this to Always, in order to return the user interface to its previous look and feel.

I also found that the update process had removed root access such that Quick Reboot would no longer run. The fix for this was to boot into recovery mode and re-flash the LineageOS super-user file (addonsu-14.1-arm-signed.zip).

As a side note, the successful update to LineageOS also seemed to clear up a problem whereby TWRP would no longer make a new system backup. In particular, the backup process produced the error: “createTarFork() process ended with ERROR: 255 Backup Failed. Cleaning Backup Folder”.

Once again, there proved to be a dearth of useful information on the cause of this problem, and how to fix it. Simply re-installing TWRP had no effect, nor did deleting the file /addon.d. However, once I had successfully updated LineageOS as noted above, I was able to re-run TWRP recovery and make a complete system backup without any error messages appearing.

Posted in Android | Tagged , , | Leave a comment

Customizing Mint’s login screen

One of the things I don’t like about the default login screen in Linux Mint Cinnamon Version 18.3 (Sylvia) is the availability of a guest session. Fortunately, it’s easy to disable this function:
Continue reading

Posted in Mint, Tweaks | Tagged , | 1 Comment

Incorrect System Time in Linux Mint 18.3

After putting a fresh installation of Linux Mint 18.3 (Sylvia) in place on my dual-booted laptop computer, I noticed that the system date was fours hours slow. Previously, I found this to be related to the difference in which Windows and Linux handle time. In particular, Windows uses local time while Linux uses Universal Time Coordinated (UTC). However, the previous fix no longer worked.
Continue reading

Posted in Commands, Mint | Tagged , | Leave a comment

Synchronizing IMAP folders in K-@mail

As I noted in a previous posting about K@mail (Turn off notification sounds in K-@mail), “Dr. Google doesn’t seem to know much about this app. So, support options are pretty limited.” This turned out to be once again the case when I found that only the Inbox folder for my IMAP account was being synchronized by default.
Continue reading

Posted in Android, Applications, E-mail, Tweaks | Tagged | Leave a comment

Moto G3 – Navigating XDA Developers

Custom ROM’s for Android devices are available from the XDA Developers web site (https://www.xda-developers.com/). Initially, users who are new to the site may find navigation a little difficult. This can certainly be the case when trying to locate relevant material for the Motorola Moto G3 smartphone.
Continue reading

Posted in Android, Moto G3 | Tagged , , , | Leave a comment

Adding Location to Android 7.1’s Quick Settings

Pulling down the notification shade on my Moto G3 smartphone running Nougat provided access to the Quick Settings menu as expected – but – there was no Location icon! Sure, I could open Settings, scroll down to Location, and turn this feature on, but using the Quick Settings’ icon would be much more convenient.
Continue reading

Posted in Android, Moto G3 | Tagged , | Leave a comment