Create Virtual USB Sticks With A Raspberry Pi Zero

Infotainment system playing back from USB. (Folkert van Heusden)

Playing back music files from USB sticks is a common feature these days, and is built-into the infotainment system in [Folkert van Heusden]’s Opel Astra. Unfortunately such USB playback features often come with a range of limitations on things like audio codecs, and in the case of [Folkert]’s car, a 1000 file limit. This had him looking at an alternative to lugging a lot of USB sticks around to avoid the horror of hearing the same songs within a week while commuting. The solution? Make a Raspberry Pi Zero into a virtual USB mass storage device using the Mass Storage Gadget (MSG) driver in the Linux kernel.

Picking USB storage as the ideal option here comes mostly from the age of the infotainment system, which lacks Bluetooth, and the audio input jack is rather crackly. Of course, having the Raspberry Pi Zero pretend to be a storage device via the MSG driver wouldn’t solve the file limit, but to get around this two Python scripts were written: one which creates images from a folder of music files, and another which randomly picks one of the available images from the Zero’s SD card and configures the MSG driver to use it.

As for the list of future improvements, there is mounting the RPi Zero’s SD card as read-only to deal with the power-off when the car is shut down, and the creating of images requires to be run as root due to the use of loopback devices. As a Proof-of-Concept it does seem to be on the right track.

It’s not just the older infotainment systems that get to have all the fun. If you’re lucky enough to have Linux running in the dashboard, you might be little more than a Bash script away from bending the system to your will.

28 thoughts on “Create Virtual USB Sticks With A Raspberry Pi Zero

    1. That depends on the socket type, in Astra J and K there is a step down converter in the USB port module(the one below hand rest) and newer versions of those can be bought, they have better current ratings. Possibly it could be a mod to a stock module.

  1. This is a great use of the Pi Zero- especially the original Pi Zero W, which is showing its age a bit. I set one up for MSC so I can easily send files over WiFi to my resin printer (which otherwise has no network connectivity). Works like a champ!

  2. How long does it take for the rpi-zero to boot up? I bet this is going to be more annoying than having to listen to the same song every now and then. It would be no use however when USB remains powered when the car is locked.

      1. Does it support Bluetooth? I only see AM, FM, CD, USB and analog AUX.

        Wonder if one could make the car play a very long file from the simulated USB stick, which, in the background, is constantly appended with data from alive audio stream from Spotify.

      2. Per the author above, for this particular model of car, ” the infotainment system […] lacks Bluetooth”. That’s probably why Bluetooth was not used. Just a guess…

      3. I do. I don’t want to pay for yet another subscription service that’s constantly trying to squeeze more money out of me, and collect data from me to sell to bog knows who. Plus, I already have all the music that I’ve collected over the years already digitized — so why would I want to pay for something I already have? Also, many places that I travel through have terrible or non-existent cellular service — it’s kinda hard to stream anything with no connection.

      4. The basic bluetooth codec is better than tapes, but worse than MP3, CD, and sometimes even FM in quality. It’s also fairly likely that in a number of years it’ll be the only codec your phone and your car have in common. Of course, perhaps you’d like to use an aux jack bluetooth adapter, but then you can’t change songs from the car’s buttons.

        Also MP3’s don’t have a subscription cost or advertisements, and are all right there if I want to skip or rewind or something. It’s just music, saved to a usb.

    1. If you use the standard Pi OS image, it can take a solid minute or two to boot and start running userland code, though that could be improved by disabling unused daemons. If you use a custom, stripped down image and disable unused kernel features using something like Buildroot, I’ve been able to get to userland from cold boot on a pi zero in about 5 seconds.

    2. It can take a couple minutes using a headless Pi OS image, though disabling unused daemons helps. You can get the boot time down significantly if you use a custom bare-bones image, though. I’ve been able to get a pi zero to boot from cold to userland in about 5 seconds with an image made with Buildroot along with disabling building of a bunch of kernel modules/features that I didn’t use. I’d guess that an Alpine Linux image would probably boot pretty quickly as well.

  3. Perhaps clean the jack and insert a freshly wiped plug. Car interiors aren’t very clean and free of oxidation. Make sure the source feeding the input is kept at the right level and use the in-dash volume only. Be glad there is no bluetooth as it would be out of date and not upgradeable. The same for lack of flac if that is the case.

    No jack, that’s a problem.

  4. I tried something similar, but with pictures.

    My Chevy Volt supports displaying images from the connected USB drive, including a slideshow mode, so naturally, I thought I could use that to implement some sort of additional (although slow) display, such as navigation, by constantly switching two image files on the drive. That, unfortunately, did not work.

    First, IIRC, the mass-storage protocol forbids the host (Raspberry Pi) from modifying the files once the fake drive is accessed. (Similar to how early Android phones could not access the SD card if they were connected to a PC) I did not get lower into the protocol to see if that could be bypassed or emulated, because slideshow mode is disabled while driving anyway. Certainly, it should be possible to set the infotainment’s console “car is moving” flag somehow, but I am unsure of safety implications of that.

    FYI Raspberry Pi Model A also works with MSG.

    1. Huh, neat. Do any of them support other codecs like ldac? Or, how do you search for them without getting results for all the other kinds of bluetooth audio dongle?

  5. First up: Fitting a Bluetooth receiver takes less than hour.
    Second: He could “only” store 1000 songs? “Only” 60 hours of music before a repeat of a song he had wanted to listen to enough to put it on a drive? I can’t even pretend to think that this even “might” be an issue.

    1. First: “the infotainment system (…) lacks Bluetooth, and the audio input jack is rather crackly” – there’s a system, it’s just not in the best shape, it doesn’t need to be replaced with a crappy aftermarket din from walmart that doesn’t tie in properly.

      Second: Are you saying that out of every song you have, you either don’t want to hear it at all or you are in the mood to hear it at all times? Because I think it’s pretty normal that people who own more than 1000 songs will probably not be able to pick 1000 to keep and the rest to never listen to.

      It only makes it worse that in all likelihood the randomization function is going to randomly pick the next song rather than randomize the order of songs, meaning that some songs won’t be played at all and some will be played multiple times in the next 1000 songs (or skips). And if you think 1000 is so many as to never notice the repeats, ask people who have background music at work how long it takes to get sick of hearing the same stuff.

      1. So cleaning the contacts or adding new ones is more hassle than developing a PiZ storage system? I really don’t think so. I’ve added Bluetooth to 1980’s and 1990’s playback devices with absolutely no hassle.
        I have thousands of songs and I also have a “Best of” folder with around 600 songs in it that’s my background music when I’m working. 600 songs that I have personally chosen, not random work chosen. If you don’t like a song being repeated in your own favourites list then you really need to question WTF is wrong with your own brain!
        Why would anyone keep a song in storage that they don’t like???

        1. That’s not what I thought you meant, which is why I talked about fitting a new single/double din receiver, as is the common term in cars, rather than just a bluetooth adapter to receive the audio. But even so, from the picture this car is 2004 or newer I believe, so it may be more annoying to work with than the stuff from 90s and before which I’ve had more luck with. At the end of the day the raspberry pi solution requires only a very very short script, still allows you to use the car’s controls, and doesn’t require doing anything to the car. Plus it’d work elsewhere just as well.

          As for the insult against my brain, I trust upon review you will find it unbecoming and no more must be said.

          I already pointed out that people may want some songs when they are in one mood or different songs when they are in another, but maybe I should give examples. Do you think that the sort of music someone might play on a romantic evening is the same sort of music that you would play when your in-laws bring their kids over to visit? Or at a business function or a charity event run by bunch of nuns? Would you play the same sort of music while exercising as you would while you’re trying to nap? Would you play your favorite Christmas songs over and over in June?

          So no, I wouldn’t rather hear the top 40 songs 5 times instead of the top 200 once, and I wouldn’t rather hear the top 200 six times instead of the top 1200 once, assuming I personally chose all 1200. Mind you, I still like some songs better than others, but every song can get annoying if it repeats enough, and I’d rather limit the repetition in favor of sometimes skipping a song.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.