I use Linux automation a lot for imaging, and one feature which is very handy in deployments is to image directly off the USB stick. However, the auto-mounting capabilities built into the Linux automation environment are not consistent enough to be reliable. In this article I present a startup script which you can use to get around these problems to ensure your flash drives can be used more reliably within Linux.
Implementing the USB Mount Script
Below are the steps which will direct you to add the usbmount.zip attached to this article into your Linux automation environment
- Download usbmount.zip, and extract the file a-usbmount.sh to your desktop
- Open up BootDisk Creator, and expand the "Linux Additional Files" section
- Within the "Linux Additional Files" section, ensure you have the startup folder created. If not present right-click "Linux Additional Files" and select the option New -> Folder option from the context menu.
- Add this articles shell script file here by right-clicking the startup folder and selecting the "Add File" option
- Rebuild Linux automation
Right-Click your target Linux automation configuration, and selecting the option to create your bootdisks.
From there just create your ISOs, automation files or usb drives as you normally would.
Testing The USB Drive Mount
When Linux automation now loads, you should see the text "Mounted Removable Device to /mnt/usb" as shown below,
From there you should be able to navigate /mnt/usb and execute DS jobs to deliver/upload images from and to the drive.
How the USB mount script works
In order to mount a disk device, we parse the Linux kernel message buffer dmesg looking for entries which appear to have detected a removable device. An example of such a log entry is this,
sd 3:0:0:0: [sdb] Attached SCSI removable disk
In order to scavenge for such output in Linux we can use the regular expression matching program called grep. Using linux pipes I can parse the dmesg buffer as follows,
dmesg | grep "removable" | grep "\[sd[a-z]\].*disk" >/tmp/tmp.txt
The above line says get all the lines from the message log which hold the string "removable", then analyse this output looking for a text field which,
- Begins with the string "[sd"
- Which is followed by a letter (a-z)
- Which in turn is followed by a closing square bracket "]"
- Which is in turn followed by a number of characters and then the string "disk"
- And dump any matching lines into a file called /tmp/tmp.txt
Next we read back the result and dump it into a string,
IFS=$'\n'
lines=($(cat /tmp/tmp.txt))
and then read each line performing a pattern match which looks for the letter after the sd (which stands for scsi device) using a do loop,
do
line=${lines[$i]}
if [[ "$line" =~ "\[sd(.*)\]\sAttached" ]]
then
diskletter=${BASH_REMATCH[1]}
break
fi
done
Should a diskletter be found, then created the /mnt/usb folder and mount the scsi device letter to it. Here we assume that only the first partition requires mounting.
if [ ! "${diskletter}" = "-1" ]; then
mkdir /mnt/usb
mount -t auto /dev/sd${diskletter}1 /mnt/usb
echo "Mounted Removable Device to /mnt/usb"
fi
Caveats
The main thrust for me creating this script is to simplify imaging from USB sticks, as this ensures we always have a fixed path (/mnt/usb) which we can reference in imaging jobs for computer deployment. As such, is it not hotplug. It is not intended to cope USB sticks being mounted and unmounted. It's use is in it's current state purely limited to scenarios where the USB stick is present as automation loads.
Kind Regards,
Ian./