{"id":2796,"date":"2021-10-03T01:01:05","date_gmt":"2021-10-03T09:01:05","guid":{"rendered":"https:\/\/wonghoi.humgar.com\/blog\/?p=2796"},"modified":"2025-08-01T23:35:22","modified_gmt":"2025-08-02T07:35:22","slug":"auto-mount-usb-drives","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2021\/10\/03\/auto-mount-usb-drives\/","title":{"rendered":"Auto mount USB drives"},"content":{"rendered":"\n<p>Raspbian OS (Raspberry Pi) do not mount USB drives automatically out of the box.<\/p>\n\n\n\n<p>I&#8217;m pretty annoyed by the lack of easy to use packages by 2021 and I still have to do it myself with the instructions here: <a href=\"https:\/\/github.com\/avanc\/mopidy-vintage\/wiki\/Automount-USB-sticks\">https:\/\/github.com\/avanc\/mopidy-vintage\/wiki\/Automount-USB-sticks<\/a><\/p>\n\n\n\n<p>These are cookbook instructions, but I&#8217;ll add some insights to what each component means so it&#8217;s easier to remember the steps.<\/p>\n\n\n\n<p>At top level, to auto-detect and mount USB drives, we need the following components<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>udev<\/strong>: analogous to what happens behind <a href=\"https:\/\/www.linux.com\/news\/udev-introduction-device-management-modern-linux-system\/\">device manager<\/a>, it keeps track of and updates devices as they are connected and disconnected immediately. Need to register USB sticks by adding a event handler, which triggers a systemd service (see below)<\/li><li><strong>systemd<\/strong>: analogous to Windows&#8217; services. Need to register the service by stating what commands it will call on start (mostly mounting) and cleanup (mostly unmounting)<\/li><li><em>automount<\/em> script: it&#8217;s a user defined script that abstracts most of the hard work detecting the partitions on the USB stick, assign the mount point names, and mount them<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Register udev event handler (rules)\n# \/etc\/udev\/rules.d\/usbstick.rules\n\nACTION==\"add\", KERNEL==\"sd&#91;a-z]&#91;0-9]\", TAG+=\"systemd\", ENV{SYSTEMD_WANTS}=\"usbstick-handler@%k\"\n\n# It triggers a systemd call to \"usbstick-handler@\" service registered under \/etc\/systemd\/system\/<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># Register systemd service\n# \/etc\/systemd\/system\/usbstick-handler@.service\n# (Note: instructions used \/lib instead of \/etc. It's better to add it as \/etc as this is manually registered as user-defined service rather than from a package)\n\n&#91;Unit]\nDescription=Mount USB sticks\nBindsTo=dev-%i.device\nAfter=dev-%i.device\n\n&#91;Service]\nType=oneshot\nRemainAfterExit=yes\nExecStart=\/usr\/local\/bin\/automount %I\nExecStop=\/usr\/bin\/pumount \/dev\/%I\n\n# %I is the USB stick's device name under \/dev, usually sda\n# Abstracted the logic of determining the mount point name and mounting to 'automount' (see below)<\/code><\/pre>\n\n\n\n<p>Create the file  <code><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">\/usr\/local\/bin\/automount<\/code> <\/code> <strong>and give it execution permission<\/strong>:  <code><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">chmod +x  \/usr\/local\/bin\/automount<\/code> <\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# $1 (first argument) is usually \"sda\" (supposedly USB stick device name) seen from %I in the systemd service commands\nPART=$1\n\n# Within the \"sda\" (USB stick device of interest), extract the partition labels (if applicable) from lsblk command. The first column (name) is dropped\nFS_LABEL=`lsblk -o name,label | grep ${PART} | awk '{print $2}'`\n\n# Decide the mount point name {partition label}_{partition name}\n# e.g. MS-DOS_sda1\ntokens=($FS_LABEL)\ntokens+=($PART)\nMOUNT_LABEL=$(IFS='_'; echo \"${tokens&#91;*]}\")\n# Using string array makes it easier to drop the prefix if there's no {partition label}\n# Bash use IFS to specify separators for listing all elements of the array\n\n# Suggestion: drop --sync for faster USB access (if you can umount properly)\n\/usr\/bin\/pmount --umask 000 --noatime -w --sync \/dev\/${PART} \/media\/${MOUNT_LABEL}\n<\/code><\/pre>\n\n\n\n<p>This automount script is adapted from <a rel=\"noreferrer noopener\" href=\"https:\/\/raspberrypi.stackexchange.com\/questions\/66169\/auto-mount-usb-stick-on-plug-in-without-uuid\" target=\"_blank\">https:\/\/raspberrypi.stackexchange.com\/questions\/66169\/auto-mount-usb-stick-on-plug-in-without-uuid<\/a> with my improvements.<\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_2796\" class=\"pvc_stats all  \" data-element-id=\"2796\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Raspbian OS (Raspberry Pi) do not mount USB drives automatically out of the box. I&#8217;m pretty annoyed by the lack of easy to use packages by 2021 and I still have to do it myself with the instructions here: https:\/\/github.com\/avanc\/mopidy-vintage\/wiki\/Automount-USB-sticks &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2021\/10\/03\/auto-mount-usb-drives\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_2796\" class=\"pvc_stats all  \" data-element-id=\"2796\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[39,54],"tags":[],"class_list":["post-2796","post","type-post","status-publish","format-standard","hentry","category-linux","category-linux-embedded"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/2796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=2796"}],"version-history":[{"count":3,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/2796\/revisions"}],"predecessor-version":[{"id":2831,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/2796\/revisions\/2831"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=2796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=2796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=2796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}