Mounting Block Devices

This pages discuses the advanced details and underlying operation. For general usage, see fstab.

Overview

The mounting of block devices is handled by the block-mount source package, which contains the block-mount and block-hotplug packages. block-mount contains the code that does the actual mounting, and the mounting via /etc/init.d/fstab (i.e. on boot rather than when device is hotplugged), and block-hotplug takes care of mounting devices when the device is recognized by the system (.e.g. when modules are loaded and the partition detected).

block-mount (binary package)

The block-mount binary package (i.e. the one you actually install, rather than the source package containing block-mount and block-hotplug), contains three library scripts (in addition to /etc/init.d/fstab and the sample config file /etc/config/fstab). These three scripts are: block.sh, mount.sh, and fsck.sh.

As of r26314 block-extroot and block-hotplug have been merged with block-mount. That means that once you install block-mount the scripts for extroot mounting and hotplug mounting are installed. With r36988 the original package block-mount was removed. Technically, the new package ubox replaced its functionality. For Fstab configuration, the new block-mount package now contains the executable block which facilitates this. You can run block <info|mount|umount|detect>. See Fstab configuration.

With the new block mount mechanism you can run block info to get the same output that blkid delivered. You can do "block mount" to mount all devices (same as what /etc/init.d/fstab restart used to do. If you run "block detect" you will get a sample uci file for the currently attached block devices. That way you can do "block detect > /etc/config/fstab" to store it

:!: block info cannot detect btrfs (r39399), xfs , jfs and some other FS. Use manual scripting to mount them.

root@OpenWrt:~# blkid
/dev/sda1: TYPE="ext2" 
/dev/sda2: UUID="890c87d4-e276-4fb0-a34a-296db408d792" TYPE="ext4" 
/dev/sdb1: LABEL="OPENWRT-BTRFS" UUID="2412e056-a1d8-4710-bf0e-d54b8ff0662f" UUID_SUB="edd04b0f-ccf6-4978-9d76-1fa17921fe58" TYPE="btrfs" 
root@OpenWrt:~# block info
/dev/sda1: VERSION="1.0" TYPE="ext2"
/dev/sda2: UUID="890c87d4-e276-4fb0-a34a-296db408d792" VERSION="1.0" TYPE="ext4"

Notes on how to use the new block-mount in Barrier Breaker

  • You can do block info to get the same output that blkid delivered
  • You can do block mount to mount all devices. The init.d script /etc/init.d/fstab used to provide this functionality by itself. Of course it still works, but in /etc/init.d/fstab are now just calls to block mount and block umount.
  • You can do block umount to unmount all devices.
  • If you run block detect you will get a sample uci file for the currently attached block devices.
  • You can do block detect > /etc/config/fstab to store it as the fstab configuration.

FIXME Are the following sections also all defunct, now that we have the ubox based block-mount?

block.sh

This the core script for mounting of block devices. It consists of the following functions:

Function Description
reset_block_cb Sets the functions mount_cb and swap_cb to the default functions
mount_cb This subfunction is used return the values from a call to config_get_mount. It is usually overridden by a more specific version. The default version just takes the parameters passed to it by config_get_mount and assigns them to variables (which are global, unless made local by the calling function, then they are local to the calling function).
swap_cb Like mount_cb but for config_get_swap
reset_dev_section_cb Sets the functions mount_dev_section_cb and swap_dev_section_cb to the default functions.
mount_dev_section_cb Like mount_cb, but for a call to get_mount_section_by_device
swap_dev_section_db Like mount_dev_section_cb, but for a call to get_swap_section_by_device
config_get_mount Gets the values of options from specified mount section and returns using mount_cb. See fstab.
config_get_swap Gets the values of options from specified swap section and returns using swap_cb. See fstab.
config_get_automount Gets values of options from global section named automount. See fstab.
config_get_autoswap Get values of options from global section named autoswap. See fstab.
config_create_swap_fstab_entry Creates an entry in /tmp/fstab (which by default is used as /etc/fstab by OpenWRT) for an enabled swap device.
config_create_mount_fstab_entry Creates an entry in /tmp/fstab (which by default is used as /etc/fstab by OpenWRT) for an enabled fs device.
libmount_find_token Uses blkid to find the device (if any) with a given UUID or LABEL (token = UUID or LABEL, value = the uuid to find or label to find)
libmount_find_device_by_id Takes a uuid, label and device (e.g. of which may be empty), and finds the device with the given uuid, or, if (and only if), no uuid is specifed, the given label, and if (and only if) no uuid or label is specified, the given device. Return the device found for the given 'id'
config_get_mount_section_by_device Uses mount_dev_section_cb to return the values of the mount section which corresponds to the device the function is asked to find, and creates an /tmp/fstab entry for it. Overrides mount_cb, then resets it to default.
config_get_swap_section_by_device Uses swap_dev_section_cb to return the values of the swap section which corresponds to the device the function is asked to find, and creates an /tmp/fstab entry for it. Overrides swap_cb, then resets it to default.

Usage notes

Most of these functions are only used internally. Generally of the functions only the callbacks and config_get_x_by_device and config_get_x are relevant outside this file. The usage is usually to override the callback to only return the desired values, and then to do e.g.

device=/dev/sda1
config_load fstab
config_foreach config_get_mount_by_device mount $device
Which would return the mount section associated with /dev/sda1 (e.g. when called by hotplug because /dev/sda1 was mounted). Will correctly find the section associated with this /dev/sda1, even if the mount section is defined by uuid and not device.

mount.sh

This script library is used by /etc/init.d/fstab and by the block-extroot package. It consists of the following functions:

Function Description
config_mount_by_section For the specified mount section, finds the device associated with it. If enabled_fsck is specified, checks the filesystem for errors and repairs any found, then create an /tmp/fstab entry for it and mounts it'. If the device section is a rootfs (i.e. is_rootfs is specfied), the device is mounted on /overlay, otherwise, if mounted (and in the fstab entry), the directory is the target option of this mount section. Overrides mount_cb and resets it to default.
config_swap_by_section Like config_mount_by_section but for a swap section (and there is not is_rootfs type option). Overrides swap_cb and resets it to default.

Usage notes

These functions are generally called like:

config_load fstab
config_foreach config_mount_by_section mount
for a normal mount, or
config_load fstab
config_foreach config_mount_by_section mount 1
to mount a rootfs on /overlay (if any rootfs section specified).

fsck.sh

this script library is used by the block-mount and block-hotplug packages. It has one function libmount_fsck and expects that fsck programs will place files under /lib/functions/fsck which contain functions fsck_{fstype} for each fstype the fsck program supports and will append the supported filesystems to the variable libmount_known_fsck. e.g. the package e2fsprogs has /lib/functions/fsck/e2fsck.sh, which defines fsck_ext2, fsck_ext3, and fsck_ext4, and and if the fsck in the image is e2fsck, then libmount_known_fsck will equal the string "ext2 ext3 ext4".

The function libmount_fsck is called with the device, fstype, and boolean fsck_enabled as parameters. If fsck is enabled and fstype is one of the known types, and the filesystem is not already mounted, or a swap, then the function will attempt to call fsck_{fstype} $device.

block-hotplug (binary package)

Block hotplug consists of three scripts, 10-swap, 20-fsck, and 40-mount. When a block devices is added these scripts are executed in the order listed. So, first the device is checked for being a swap section, or to attempt to mount as swap, if it is not a defined section for swap or mount (this is known as anon_swap or anonymous swap). Then 20-fsck checks if the device is listed as enabled_fsck and if so, attempts to check/repair the filesystem, and, finally, we check if the device should be mounted, either named, or anonymously (i.e. not listed in any section).

Back to top

doc/techref/block_mount.txt · Last modified: 2014/09/18 21:39 by tmomas