Deployment Solution

 View Only

Mounting Removable Disks in Linux Automation 

May 24, 2011 01:57 PM

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

  1. Download usbmount.zip, and extract the file a-usbmount.sh to your desktop
  2. Open up BootDisk Creator, and expand the "Linux Additional Files" section
  3. 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.


     
  4. Add this articles shell script file here by right-clicking the startup folder and selecting the "Add File" option


     
  5. 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,
 
  1. Begins with the string "[sd"
  2. Which is followed by a letter (a-z)
  3. Which in turn is followed by a closing square bracket "]"
  4. Which is in turn followed by a number of characters and then the string "disk"
  5. 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./

Statistics
0 Favorited
1 Views
1 Files
0 Shares
0 Downloads
Attachment(s)
zip file
usbmount.zip   534 B   1 version
Uploaded - Feb 25, 2020

Tags and Keywords

Comments

Oct 03, 2011 02:01 PM

Updated

The comments combined in one new script, find attached.

But credits are still for Ian; without his original idea I would have no clue where to start.

  • Will unload and load disks to make sure the local disks are listed first for rdeploy
  • Will work for USB pendrives
  • Will work for USB storage formatted as FAT or NTFS
  • Unload/load pata_atiixp for HP thinclients (ATA-flash)
  • After loading usb_storage increased sleep to 10 sec for some types of USB pen drives.
  • Will not do a unnessary unload/load if there is no usb stick inserted
  • Works now if booted from PXE and if booted from local USB-stick.
  • Put in a example to check if the USB-stick is still uptodate with the version on the server.

Oct 03, 2011 05:49 AM

The comments combined in one new script, find attached.

  • Will unload and load disks to make sure the local disks are listed first for rdeploy
  • Will work for USB pendrives
  • Will work for USB storage formatted as FAT or NTFS
  • Unload/load pata_atiixp for HP thinclients (ATA-flash)
  • After loading usb_storage increased sleep to 10 sec for some types of USB pen drives.
  • Works now if booted from PXE and if booted from local USB-stick.

Jul 09, 2011 06:36 AM

Hi Xaveer,

Having worked now with this for a couple of weeks, I think your right -I should revert to the original method of inserting/removing modules.

This has the advantage that,

  1. Native rdeploy tasks will always see "prod:" correctly (as that seems to default the the first disk). Removing and re-inserting the kernel modules ensures fixed disks are always first in the drive order.
     
  2. USB drives are seen correctly (as you've noted) in addition to USB Flash drives
     
  3. We can stage the re-insertion of modules in stages to reveal whether the drive is fixed or removable by scanning the kernel buffer

Thanks again for your thoughts!

Jun 12, 2011 02:14 PM

Hi Xaveer,

Finally getting to this after my quick blast back to ImageInvoker as it expired last week.

Starting from the beginning -yes this is to sort out the USB drive imaging problem referred to in that old forum post. The solution I had there was quite dirty in my mind -ripping out the modules and re-installing is quite severe. In some circumstances, the USB re-mount isn't clean so this solution isn't ideal.

The idea behind this method was to be a little smarter where removable drives were concerned by ferreting out what information I could.

Now, as you've found out this works fabulously USB drives which are configured with with the removable bit set -most flash drives fall into this category nowadays. As you also note though, Linux cannot tell the difference between USB drives configured as fixed disks and the native SATA/SCSI disks in the system. They are all scsi fixed disks to Linux.

To get around this, a text file containing flash drive entries would have to be used for matching purposes. The code in this case would then mark as removable any disk descriptions which matched the text in this file. The codebase used in SplashDiag could easily be modified for that purpose.

Kind Regards,
Ian./

Jun 07, 2011 05:39 AM

Hi Xaveer,

Good man -Will look into this later this afternoon. Thanks!

A quick thing I'mm mention before this afternoon is 'removable bits' -some USB harddrives have this set, and some don't.

Kind Regards,

Jun 07, 2011 05:14 AM

1. The script is only valid for USB-sticks, not for USB drives:

USB-stick: sd 1:0:0:0: [sda] Attached SCSI removable disk
USB-disk:  sd 0:0:0:0: [sda] Attached SCSI disk

Changing the line:

dmesg | grep "removable" ...

into

dmesg | grep "Attached" ...

will mount the external USB-drive but has a not-wanted effect on VMWare because the normal disc in VMWare is also shown as 'Attached SCSI disk'. I have not found a way to differentiate between internal SCSI disks and external USB-disks.

 

2. You begin your script with:

  # Diskindex being -1 indicates no removable media found
  diskindex=-1

  This should be:
  diskletter=-1

3. 'Mount -t auto' gives an errormessage. I modified it to:

   mount -t vfat /dev/sd${diskletter}1 /mnt/usb
   if [ ${?} -ne 0 ]
   then
        mount -t ntfs /dev/sd${diskletter}1 /mnt/usb
   fi
   echo "Mounted Removable Device to /mnt/usb"
 

Jun 07, 2011 01:28 AM

Hi Ian, 

Thanks for a nice post.

Question: The comments that you issued  (see the link below) about the USB drive sometimes being recognised as the first drive and thus causing problems for rdeploy, to these also apply here?

Xaveer

http://www.symantec.com/connect/forums/deploying-image-local-esternal-usb-hard-drive )       

Related Entries and Links

No Related Resource entered.