A Script to Automatically Mount USB Storage on an ESXi System

Tom Fenton offers up his personal, time-saving, 94-line bash script, complete with code on GitHub.

I have been using a Raspberry Pi to run ESXi on Arm (a topic I've been writing about a lot lately; my articles are located here, here and here). Because this device uses USB for storage, I have had to mount USB devices to act as VMFS filesystems. Doing this has required me to type in a half-dozen commands, a process which I documented in another article located here.

While this is not usually an issue, I have been testing different types of USB storage, which means I have been mounting a lot of different USB devices. It gets tiresome to retype these commands, and I do occasionally have typos. To automate the process, I created a script to mount them. You can download the script from my GitHub repository (located here).

[Click on image for larger view.]

To use the script, log in to your ESXi server (I use SSH) as root, then copy the script to it (I use SCP) and change its permission to make it executable (chmod 755 ./AutoMountUSB.bash).

When you execute the script (./AutoMountUSB.bash), devices that are already mounted will be displayed and you will be prompted to insert the USB device and press the Enter key.

[Click on image for larger view.]

At this point, plug in your USB drive and press Enter. If a new device is not found, you can try again by entering Y; or, if you want to exit the program, enter Q. Once a new device is found, you will then be prompted for the name of the new datastore. The script will then mount, format and name the datastore.

[Click on image for larger view.]

After the USB device has been mounted, it will show up in vSphere Client as a datastore.

[Click on image for larger view.]

At this point, I was able to verify that I could run a VM from it.

My ESXi on Arm system was running vSphere 7.0, but I also mounted a USB drive on a vSphere 6.7 x86 ESXi host using the script without any issues.

[Click on image for larger view.]

I then cloned a VM to the USB drive and put the drive in another 6.7 x86 host. After doing this, I was able to instantiate the VM without any issues.

[Click on image for larger view.]

I have found this script to be extremely useful, but I have not had a chance to extensively test it; as such, it should be used with extreme caution and not on a production system. Enjoy!

About the Author

Tom Fenton has a wealth of hands-on IT experience gained over the past 25 years in a variety of technologies, with the past 15 years focusing on virtualization and storage. He previously worked at VMware as a Senior Course Developer, Solutions Engineer, and in the Competitive Marketing group. He has also worked as a Senior Validation Engineer with The Taneja Group, where he headed the Validation Service Lab and was instrumental in starting up its vSphere Virtual Volumes practice. He's on Twitter @vDoppler.

Featured