How do I mount a volume by label in Alpine Linux?

I'm running a Docker container based on Alpine (specifically the balenalib/armv7hf-alpine image). Within the container I want to mount a USB drive by its label, but the Alpine mount command doesn't appear to support the --label option.

I'm an Alpine newbie, but under Raspbian/Debian, I'd just use, e.g.:

mount --label USBDRIVE /mnt/usbdrive

Under Alpine I can successfully mount the drive, but I have to use its device name instead of its label, e.g.:

mount /dev/sda1 /mnt/usbdrive

Is there an approach I can use to achieve mount by label under Alpine? E.g.: a different version of mount? Or a way of finding the volume name by iterating through the labels on all available volumes?

Edit: OK, here's a solution that works (with apologies for my Bash scripting):

VOLUME=$(for VOL in $(fdisk -l | grep ^/dev/sd | awk '{print $1}'); \ do blkid $VOL; done | grep USBDRIVE | awk -F: '{print $1}')
mount $VOLUME /mnt/usbdrive

This lists all disks using fdisk, isolates all the /dev/sd* entries, uses blkid to find the volume labels, identifies the label required and grabs its volume name. (Exceptions ignored for ease of presentation).

3

2 Answers

I'm sure there are neater ways to do this, but -- as noted in the edit above -- the approach below works for my needs. Note that it would break if multiple volumes with the required label were present.

VOLUME=$(for VOL in $(fdisk -l | grep ^/dev/sd | awk '{print $1}'); \ do blkid $VOL; done | grep USBDRIVE | awk -F: '{print $1}')
mount $VOLUME /mnt/usbdrive

Or just use blkid's -L option:

mount $(blkid -L USBDRIVE) /mnt/usbdrive

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like