Skip to content

Commit

Permalink
Automount all block devices that do not include the boot partition
Browse files Browse the repository at this point in the history
Rather than filter by usb subsystems, just ensure that the parent
block device does not include the boot partition provided to the kernel.
All other block devices are fair game to mount.

Change-type: patch
Signed-off-by: Kyle Harding <kyle@balena.io>
  • Loading branch information
klutchell committed Jul 7, 2022
1 parent 70a4d4d commit 6d8f09d
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions 30-mount.sh
@@ -1,21 +1,22 @@
#!/usr/bin/with-contenv bash
# shellcheck shell=bash

mapfile -t usb_devices < <(lsblk -J -O | jq -r '.blockdevices[] |
select(.subsystems=="block:scsi:usb:platform" or .subsystems=="block:scsi:usb:pci:platform") |
.path, .children[].path')
# list all block devices that do not include the kernel boot partition
root_uuid="$(cat /proc/cmdline | sed -rn 's/.*root=UUID=([^ ]+).*/\1/p')"
mapfile -t block_devs < <(lsblk -J -O | jq -r --arg UUID "${root_uuid}" '.blockdevices[] |
select(.children) | select( all(.children[]; .uuid != $UUID )) | .children[].path')

# automount USB device partitions at /media/{UUID}
if [ ${#usb_devices[@]} -gt 0 ]
# automount block device partitions at /media/{UUID}
if [ ${#block_devs[@]} -gt 0 ]
then
echo "Found USB storage block devices: ${usb_devices[*]}"
for uuid in $(blkid -sUUID -ovalue "${usb_devices[@]}")
echo "Found block storage devices: ${block_devs[*]}"
for uuid in $(blkid -sUUID -ovalue "${block_devs[@]}")
do
{
mkdir -pv /media/"${uuid}"
mount -v UUID="${uuid}" /media/"${uuid}"
} || continue
done
else
echo "No USB block storage devices found."
echo "No block storage devices found."
fi

0 comments on commit 6d8f09d

Please sign in to comment.