project/fstools.git
18 months agoblock: try to find the root device on both / and /rom
Luca Barbato [Tue, 3 Jan 2023 11:06:58 +0000 (12:06 +0100)]
block: try to find the root device on both / and /rom

Sometimes / is already overlay mounted, assume /rom contains the real
rootfs.

Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
18 months agoblock: support extroot on already mounted overlay
Luca Barbato [Tue, 27 Dec 2022 09:05:15 +0000 (10:05 +0100)]
block: support extroot on already mounted overlay

Try to find extroot configuration on already mounted overlay

Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
21 months agolibblkid-tiny: add exfat superblock support
Rafał Miłecki [Thu, 23 Feb 2023 18:26:14 +0000 (19:26 +0100)]
libblkid-tiny: add exfat superblock support

exFAT became very popular over last years and also well supported with
Linux kernel driver and macOS support. It's commonly used for USB
drivers. Port a simple support for it from util-linux / libblkid.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
22 months agolibfstools: use class interface to iterate over ubi devices
Daniel Golle [Fri, 23 Dec 2022 00:25:56 +0000 (00:25 +0000)]
libfstools: use class interface to iterate over ubi devices

Instead of relying on /sys/devices/virtual/ubi/* better use
/sys/class/ubi/* which makes things more straight forward.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
22 months agopartname: Correct fstools_partname_fallback_scan comparison
Brian Norris [Thu, 26 Jan 2023 06:18:15 +0000 (22:18 -0800)]
partname: Correct fstools_partname_fallback_scan comparison

Commit 1ea5855e980c ("partname: Introduce fstools_partname_fallback_scan
option") had two problems:

1. The strcmp() aborted when the param *matched* 1; we wanted the
   inverse
2. It was too aggressive about skipping the fallback behavior. For
   devices that had no root= parameter, they would always attempt the
   fallback scan.

Fix both of those.

Fixes: 1ea5855e980c ("partname: Introduce fstools_partname_fallback_scan option")
Signed-off-by: Brian Norris <computersforpeace@gmail.com>
[ add extra spacing between function ]
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
22 months agopartname: Introduce fstools_partname_fallback_scan option
Christian Marangi [Sun, 22 Jan 2023 15:43:35 +0000 (16:43 +0100)]
partname: Introduce fstools_partname_fallback_scan option

Some device may contains a GPT partition named rootfs_data that may
not be suitable.
To save from regression with old implementation that doesn't use
fstools_ignore_partname to explicitly say that that parname scan
should be ignored, make explicit that scanning each partition should
be done by providing fstools_partname_fallback_scan=1 and skip partname
scan in every other case.

Fixes: e9b59f063bb3 ("partname: Ignore root=PARTUUID...")
Tested-by: Dirk Buchwalder <buchwalder@posteo.de>
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
22 months agopartname: Ignore root=PARTUUID...
Brian Norris [Sat, 7 Jan 2023 02:04:22 +0000 (18:04 -0800)]
partname: Ignore root=PARTUUID...

We're assuming all root= arguments are /dev/ paths, but many targets
utilize root=PARTUUID=<xxx> strategies. At least allow them to fall back
to scanning all block devices.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
2 years agoblock: try multiple NTFS filesystem implementations
Rafał Miłecki [Thu, 3 Nov 2022 13:21:49 +0000 (14:21 +0100)]
block: try multiple NTFS filesystem implementations

NTFS filesystem is supported by multiple implementations. Try mounting
it using all known drivers. Trying just "ntfs" string was limiting
fstools to compatibility with the read-only upstream Linux driver.

This fixes:
daemon.err block: No "mount.ntfs" utility available
daemon.err block: mounting /dev/sda1 (ntfs) as /mnt/sda1 failed (25) - Not a tty

Above errors were appearing even with ntfs3 upstrea kernel driver or
ntfs-3g (user-space) installed.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
2 years agolibfstools: skip JFFS2 padding on block devices
Daniel Golle [Tue, 18 Oct 2022 16:17:29 +0000 (17:17 +0100)]
libfstools: skip JFFS2 padding on block devices

Images also used on devices with NOR flash may come with JFFS2 deadc0de
marking and padding up to the largest NOR erase size.
Skip that padding and take it into account when extracting sysupgrade
backup from mmc/block partition.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2 years agoblock: Do not include linux/fs.h any more
Hauke Mehrtens [Sun, 7 Aug 2022 14:45:30 +0000 (16:45 +0200)]
block: Do not include linux/fs.h any more

glibc 2.36 changed the definition of enum fsconfig_command in
sys/mount.h. This definition collides with the same definition from
linux/fs.h now. Remove the include of linux/fs.h. This compiles still
with musl too.

musl and glibc provide the defines linux/fs.h was included for in
sys/mount.h since some years.

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
2 years agolibfstools: Rename move_mount() function to ovl_move_mount() for glibc 2.36
Hauke Mehrtens [Sun, 7 Aug 2022 14:41:38 +0000 (16:41 +0200)]
libfstools: Rename move_mount() function to ovl_move_mount() for glibc 2.36

glibc in version 2.36 defines an own function named move_mount() in some
header. The definition from glibc collides with our definition, just
rename the function in fstools.

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
2 years agolibfstools: handle gzip return value in block_volume_format
Christian Marangi [Sun, 17 Jul 2022 09:44:24 +0000 (11:44 +0200)]
libfstools: handle gzip return value in block_volume_format

Fix the following compilation warning:
libfstools/common.c: In function 'block_volume_format':
libfstools/common.c:120:17: error: ignoring return value of 'system' declared with attribute 'warn_unused_result' [-Werror=unused-result]
  120 |                 system(str);
      |                 ^~~~~~~~~~~

Correctly handle return value from gzip and print an error on such case.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
2 years agoRevert "fstools: remove SELinux restorecon hack"
Daniel Golle [Thu, 2 Jun 2022 06:36:47 +0000 (07:36 +0100)]
Revert "fstools: remove SELinux restorecon hack"

Now that procd only relables the filesystem in case of the system
beging started with initramfs we will again need to take care of
labeling newly created /overlay.

This reverts commit 9e11b3723ce30b9b8c94ad7d15072a10cf13c0b4.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2 years agofstools: remove SELinux restorecon hack
Daniel Golle [Tue, 3 May 2022 00:55:31 +0000 (01:55 +0100)]
fstools: remove SELinux restorecon hack

Now that procd takes care of it properly we no longer need this hack.
Remove it to safe some space also on non-SELinux systems.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2 years agoblock: don't probe mtdblock on NAND (with legacy exceptions)
Daniel Golle [Sun, 1 May 2022 13:13:01 +0000 (14:13 +0100)]
block: don't probe mtdblock on NAND (with legacy exceptions)

Recent kernels started to spill warnings on the log if a userspace
process open()s an mtdblock device backed by NAND flash:

mtdblock: MTD device 'foo' is NAND, please consider using UBI block devices instead.

The warning itself is legitimate -- one really shouldn't be using
mtdblock on NAND.
Hence make fstools skip probing mtdblock devices if their underlaying
mtd device is of type 'nand'. As we don't want to break boards actually
using JFFS2 and squashfs directly on NAND, still probe the mtdblock
device in case the mtd device name is 'rootfs' or 'rootfs_data'.
This will then also trigger the kernel warning as it should.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2 years agoblockd: restore device_move semantics
Daniel Golle [Wed, 13 Apr 2022 00:00:31 +0000 (01:00 +0100)]
blockd: restore device_move semantics

Before commit 4963db4 block device were only removed and re-added in
case of device_move() returning a non-zero value. Commit 4963db4 then
(supposedly) accidentally inverted that logic and also (probably to
work-around the problems resulting from the now inverted logic) limited
this behavior to autofs mounts, leaving the autofs codepath in a semi-
broken state.
Restore the original semantics as of before commit 4963db4 to fully
restore functionality for autofs mounts.

Fixes: 4963db4 ("blockd: use uloop_process for calling /sbin/hotplug-call mount")
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2 years agolibfstools: mtd: improve error handling
Daniel Golle [Tue, 12 Apr 2022 21:51:04 +0000 (22:51 +0100)]
libfstools: mtd: improve error handling

Use -1 to mark invalid file descriptors as 0 can theoretically be a
valid open file descriptor.
Do not ignore lseek() return value and check if an error has occured.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2 years agolibfstools: avoid segfault in find_mount_point
Stijn Tintel [Tue, 21 Dec 2021 17:37:03 +0000 (19:37 +0200)]
libfstools: avoid segfault in find_mount_point

If block is NULL, find_mount_point will segfault when comparing it
against the device name found in /proc/self/mountinfo. Avoid this by
checking if block is NULL.

Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
2 years agolibfstools: use variable for overlay mount-point
Rafał Miłecki [Tue, 4 Jan 2022 22:17:30 +0000 (23:17 +0100)]
libfstools: use variable for overlay mount-point

This avoids duplicating path over and over.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
2 years agolibfstools: get rid of "extroot_prefix" global variable
Rafał Miłecki [Tue, 4 Jan 2022 22:17:29 +0000 (23:17 +0100)]
libfstools: get rid of "extroot_prefix" global variable

Replace it with mount_extroot() argument. It's cleaner than a global
var.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
2 years agoUpdate / fix extroot comments
Rafał Miłecki [Tue, 4 Jan 2022 14:27:14 +0000 (15:27 +0100)]
Update / fix extroot comments

Comment in start() was invalid as mount_extroot() doesn't handle any
mounting internally. It was a misunderstanding coming from block.c
function called just the same.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
2 years agolibfstools: check for overlay mounting errors
Rafał Miłecki [Tue, 4 Jan 2022 14:27:13 +0000 (15:27 +0100)]
libfstools: check for overlay mounting errors

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
3 years agofstools: fix a couple of minor code problems
Daniel Golle [Tue, 16 Nov 2021 20:49:23 +0000 (20:49 +0000)]
fstools: fix a couple of minor code problems

Improve error handling and fix a resource leak inside an error path.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agolibfstools: use uevent instead of relying on custom kernel patch
Daniel Golle [Mon, 1 Nov 2021 12:39:11 +0000 (12:39 +0000)]
libfstools: use uevent instead of relying on custom kernel patch

Detect partition name based on 'uevent' instead of relying in custom
kernel patch exposing 'name' in sysfs directly.
This will allow to drop the custom patch again.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agolibfstools: make sure file is closed on error
Daniel Golle [Mon, 30 Aug 2021 19:11:30 +0000 (20:11 +0100)]
libfstools: make sure file is closed on error

Coverity CID: 1330286 Resource leak
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblockd: include missing libubox/utils.h
Daniel Golle [Wed, 25 Aug 2021 21:44:14 +0000 (22:44 +0100)]
blockd: include missing libubox/utils.h

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agolibfstools: handle open() return value properly in F2FS check
Daniel Golle [Mon, 23 Aug 2021 16:55:31 +0000 (17:55 +0100)]
libfstools: handle open() return value properly in F2FS check

Coverity CID: 1490101 Argument cannot be negative

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblock: fix two resources leaks discovered by Coverity
Daniel Golle [Sat, 14 Aug 2021 17:30:48 +0000 (18:30 +0100)]
block: fix two resources leaks discovered by Coverity

Coverity CID: 1412456 Resource leak
Coverity CID: 1412458 Resource leak

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoubi: fix resource leak in legacy codepath
Daniel Golle [Sat, 14 Aug 2021 17:15:54 +0000 (18:15 +0100)]
ubi: fix resource leak in legacy codepath

I'm not even sure we are even using this 'ubi' tool anywhere.
Fix the resource leak reported by Coverity anyway.

Coverity CID: 1330289 Resource leak
Coverity CID: 1330290 Resource leak
Coverity CID: 1330291 Resource leak

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agolibubi: fix several issues discovered by Coverity
Daniel Golle [Sat, 14 Aug 2021 17:09:43 +0000 (18:09 +0100)]
libubi: fix several issues discovered by Coverity

Coverity CID: 1329896 Out-of-bounds access
Coverity CID: 1330127 Resource leak
Coverity CID: 1330173 Resource leak
Coverity CID: 1330472 Wrong size argument

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblockd: fix resource leak discovered by coverity scan
Daniel Golle [Thu, 12 Aug 2021 15:34:39 +0000 (16:34 +0100)]
blockd: fix resource leak discovered by coverity scan

Fixes Coverity CID 1463265

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblockd: also report target in notifications
Daniel Golle [Wed, 4 Aug 2021 22:31:44 +0000 (23:31 +0100)]
blockd: also report target in notifications

Useful to start/stop services triggered by mountpoints.
See procd.sh procd_add_mount_trigger to make use of that.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agolibblkid-tiny: install header file to include dir
Daniel Danzberger [Mon, 2 Aug 2021 09:57:09 +0000 (11:57 +0200)]
libblkid-tiny: install header file to include dir

Hence libblkid-tiny is a shared lib, it makes sense to install it's
header to the system incldue/ dir for other applications to use it.

Signed-off-by: Daniel Danzberger <daniel@dd-wrt.com>
3 years agolibblkid-tiny: fix invalid open syscall return check
Daniel Danzberger [Mon, 2 Aug 2021 09:50:00 +0000 (11:50 +0200)]
libblkid-tiny: fix invalid open syscall return check

open() returns -1 on error, not 0 (stdin).

Signed-off-by: Daniel Danzberger <daniel@dd-wrt.com>
3 years agoblockd: make most calls to 'block' asynchronous
Daniel Golle [Fri, 30 Jul 2021 00:01:25 +0000 (01:01 +0100)]
blockd: make most calls to 'block' asynchronous

Don't wait for calls to 'block' to complete unless it's for an
autofs event (which cannot be handled async).
Use uloop for 'mount.ready' notificaion when startup has completed
to avoid blocking in waitpid() while the 'block' process is calling
back via ubus.
This greatly reduces the amount of time blockd needs on boot.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblockd: send mount.ready when startup has completed
Daniel Golle [Wed, 28 Jul 2021 18:20:56 +0000 (19:20 +0100)]
blockd: send mount.ready when startup has completed

Emmit mount.ready notification when 'block autofs start' has completed.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblock: don't add non-ubifs ubi devices
Daniel Golle [Wed, 28 Jul 2021 16:30:31 +0000 (17:30 +0100)]
block: don't add non-ubifs ubi devices

As they require ubiblock to be mounted, just skip ubi devices in case
they don't contain a ubifs.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblock: support umount device basename
Daniel Golle [Fri, 23 Jul 2021 23:52:44 +0000 (00:52 +0100)]
block: support umount device basename

blockd calls the umount action with the basename of the device rather
than the full path.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblockd: fix length of timeout int passed to ioctl
Daniel Golle [Fri, 23 Jul 2021 23:14:57 +0000 (00:14 +0100)]
blockd: fix length of timeout int passed to ioctl

AUTOFS_IOC_SETTIMEOUT expects a pointer to an 'unsigned long' which
will result in out-of-bounds access when passing a pointer to an 'int'.
Change type of timeout to 'unsigned long'.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblock: allow remove hotplug event to arrive at blockd
Daniel Golle [Fri, 23 Jul 2021 22:20:43 +0000 (23:20 +0100)]
block: allow remove hotplug event to arrive at blockd

As the hotplug event fires after the device has been removed, it will
never be found by _cache_load(). Hence the 'remove' event needs to
propagate in all cases and we only need to populate the cache in case
of 'add' events.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblock: use /dev/dm-* instead of /dev/mapper/*
Daniel Golle [Fri, 23 Jul 2021 21:32:56 +0000 (22:32 +0100)]
block: use /dev/dm-* instead of /dev/mapper/*

This avoids a lot of problems as hotplug events also arrive for the
/dev/dm-* names.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoRevert "blockd: detect mountpoint of /dev/mapper/*"
Daniel Golle [Fri, 23 Jul 2021 21:33:46 +0000 (22:33 +0100)]
Revert "blockd: detect mountpoint of /dev/mapper/*"

This reverts commit 4d4dcfb33c5d9fa31c9916e106bee309ec7b4b01.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoRevert "block: resolve /dev/mapper/* name for /dev/dm-0 when hotplugging"
Daniel Golle [Fri, 23 Jul 2021 21:30:56 +0000 (22:30 +0100)]
Revert "block: resolve /dev/mapper/* name for /dev/dm-0 when hotplugging"

Turns out using the device nodes /dev/mapper/* turns out to be a bad
idea to begin with. Will use /dev/dm-* instead.

This reverts commit 2f42515977adef7c026eb380ed2423c533cf8f3d.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblockd: create mountpoint parent folder if needed
Daniel Golle [Sat, 17 Jul 2021 22:13:27 +0000 (23:13 +0100)]
blockd: create mountpoint parent folder if needed

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblockd: move to its own POSIX process group
Daniel Golle [Thu, 15 Jul 2021 14:49:34 +0000 (15:49 +0100)]
blockd: move to its own POSIX process group

Not to be confused with cgroups, there are also POSIX process groups.
They do matter when it comes to autofs, as all requests coming from the
process group of the automounter itself will be ignored.
Hence, if blockd runs in the same process group as init and all
services, requests from services will be ignored.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblockd: fix trigger name
Daniel Golle [Thu, 15 Jul 2021 01:03:38 +0000 (02:03 +0100)]
blockd: fix trigger name

Make it 'mount.add' instead of just 'add' which is more obvious when
used with procd_add_raw_trigger.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblockd: also send ubus notification on mount hotplug
Daniel Golle [Wed, 14 Jul 2021 03:42:25 +0000 (04:42 +0100)]
blockd: also send ubus notification on mount hotplug

Also sending a ubus notification on mount hotplug provides a useful
shortcut for procd service triggers.
As the /etc/hotplug.d/mount API for now doesn't have any users and also
makes it hard to aggregate hotplug calls, we should consider removing it
in favor of only using triggers on the block notifications in future.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblock: resolve /dev/mapper/* name for /dev/dm-0 when hotplugging
Daniel Golle [Wed, 14 Jul 2021 03:05:03 +0000 (04:05 +0100)]
block: resolve /dev/mapper/* name for /dev/dm-0 when hotplugging

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblockd: detect mountpoint of /dev/mapper/*
Daniel Golle [Wed, 14 Jul 2021 01:58:12 +0000 (02:58 +0100)]
blockd: detect mountpoint of /dev/mapper/*

If a device cannot be found in /dev, also try /dev/mapper.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblockd: add missing #define _GNU_SOURCE
Daniel Golle [Sun, 16 May 2021 08:49:24 +0000 (09:49 +0100)]
blockd: add missing #define _GNU_SOURCE

asprintf requires _GNU_SOURCE to be defined. Set it.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblock: allow autofs and umount commands also on MTD/UBI
Daniel Golle [Wed, 12 May 2021 22:54:47 +0000 (23:54 +0100)]
block: allow autofs and umount commands also on MTD/UBI

As devices with large NAND flash are becoming more common, users may
use that resource for additional (UBI) volumes. To support that in
fstools, allow autofs and umount also on MTD/UBI devices.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblock: match device path instead of assuming /dev/%s
Daniel Golle [Wed, 12 May 2021 17:05:37 +0000 (18:05 +0100)]
block: match device path instead of assuming /dev/%s

Block devices created by device mapper are in /dev/mapper/ folder,
hence the assumption of the 'block' tool expecting /dev/%s being the
path of a device doesn't hold true.
Match device path from cache instead.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agolibblkid-tiny: fix buffer overflow
Daniel Golle [Wed, 12 May 2021 16:07:28 +0000 (17:07 +0100)]
libblkid-tiny: fix buffer overflow

Copying device name into a fixed-length buffer is problematic as
the name can be longer than the buffer, resulting in subsequent fields
getting corrupted and potentially even worse things.
Drop strcpy of device name and use of the copied value as it is known
anyway.

Before this fix:
/dev/mapper/owrt--volumes--e093cc66-rw_test: UUID="c66-rw_test" LABEL="test" VERSION="1.14" TYPE="f2fs"

After this fix:
/dev/mapper/owrt--volumes--e093cc66-rw_test: UUID="5eda3e52-3427-493a-a6d6-ffdb5a5836fd" LABEL="test" VERSION="1.14" TYPE="f2fs"

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblockd: use allocated strings instead of fixed buffers
Daniel Golle [Wed, 12 May 2021 15:29:36 +0000 (16:29 +0100)]
blockd: use allocated strings instead of fixed buffers

device names can be pretty long when using LVM2, mount targets are
user defined and can potentially also be of PATH_MAX length.
Replace static buffers with dynamically allocated strings to avoid
buffer overflow with too long device names or mount targets.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoblock: use dynamically allocated target string
Daniel Golle [Wed, 12 May 2021 22:35:46 +0000 (23:35 +0100)]
block: use dynamically allocated target string

Dynamically allocate string buffer for target mountpoint if needed in
order to avoid the static buffer overflowing for long (device mapper)
paths.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agofstools: block: fix segfault on mount with no target
Daniel Danzberger [Tue, 4 May 2021 13:23:11 +0000 (15:23 +0200)]
fstools: block: fix segfault on mount with no target

When a UCI fstab mount config doesn't contain a target option,
a 'block mount' call segfaults when comparing a mount's target (NULL)
to a found mount point returned by find_mount_point()

Signed-off-by: Daniel Danzberger <daniel@dd-wrt.com>
Acked-by: Paul Spooren <mail@aparcar.org>
3 years agooverlay: fix syncronizing typo
Petr Štetiar [Wed, 7 Apr 2021 10:07:32 +0000 (12:07 +0200)]
overlay: fix syncronizing typo

So it's now correct `synchronizing`.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
3 years agopartname: allow skipping existing 'rootfs_data' partition
Daniel Golle [Fri, 19 Mar 2021 12:13:33 +0000 (12:13 +0000)]
partname: allow skipping existing 'rootfs_data' partition

Apparently some devices with eMMC (e.g. ZyXEL NBG6817) got an exsiting
GPT partition called 'rootfs_data'. This partition is then selected
as rootfs_data eventhough it may not be suitable for that (too small)
or serve another purpose (ie. used by vendor firmware).
To avoid this, check if the kernel cmdline contains a variable
fstools_ignore_partname=1 and if so, skip partname detection of volumes
alltogether.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agolibfstools: remove superflus include
Daniel Golle [Mon, 8 Mar 2021 00:05:51 +0000 (00:05 +0000)]
libfstools: remove superflus include

Turns out C header file <stropts.h> is unneeded after all.
Remove it.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agolibfstools: fix build with glibc
Daniel Golle [Fri, 5 Mar 2021 01:33:22 +0000 (01:33 +0000)]
libfstools: fix build with glibc

stropts.h which is unavailable under glibc (and not needed
when building against glibc). Include it only if not building
against glibc.

Reported by: @DazzyWalkman
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agolibfstools: partname: several fixes
Daniel Golle [Tue, 2 Mar 2021 17:40:43 +0000 (17:40 +0000)]
libfstools: partname: several fixes

partname driver didn't allow mount_root to identify the filesystem
and hence the overlay filesystem state remained PENDING which lead
to overlay being cleared again at every reboot.
Fix that by setting v->blk early and, while at it, eliminate unneeded
strdup's by storing the full device paths in a union allowing accessing
the full path or the device name only.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agofstools: add partname volume driver
Daniel Golle [Fri, 26 Feb 2021 19:45:55 +0000 (19:45 +0000)]
fstools: add partname volume driver

Add driver to handle block devices with (GPT) partition table which
can include a partition name.
If 'root=' is set on the kernel cmdline, only search for partitions
on the same device.
Among with other changes (ptgen, image*.mk, base-files, ...) this
allows for a much more straight forward storage model on block based
devices.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agojffs2reset: support fristboot on unmounted UBI overlay
Daniel Golle [Mon, 4 Jan 2021 18:05:07 +0000 (18:05 +0000)]
jffs2reset: support fristboot on unmounted UBI overlay

To erase the overlay partitions on systems using UBIFS overlay,
instead of writing the jffs2-marker, truncating the UBI volume to 0
has the desired effect.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
4 years agoblock: replace local mkdir_p implementation
Daniel Golle [Sat, 12 Dec 2020 23:01:15 +0000 (23:01 +0000)]
block: replace local mkdir_p implementation

Drop local mkdir_p implementation in favour of using the
implementation now added to libubox.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
4 years agomount: restorecon: guard against execl() errors
Daniel Golle [Fri, 16 Oct 2020 02:24:17 +0000 (03:24 +0100)]
mount: restorecon: guard against execl() errors

In the current implementation, in case of execl("/sbin/restorecon")
failing, the child process will also return and that will lead to
even more disasterous effects.
Though it seems unlikely that execl() would fail given that the file
exists, simply catch that case by exiting in case execl() returns.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
4 years agojffs2-reset: allow doing a factory reset and passing a sysupgrade.tgz
John Crispin [Fri, 27 Nov 2020 16:20:04 +0000 (17:20 +0100)]
jffs2-reset: allow doing a factory reset and passing a sysupgrade.tgz

Signed-off-by: John Crispin <john@phrozen.org>
4 years agomount: apply SELinux labels before overlayfs mount
Daniel Golle [Sun, 11 Oct 2020 00:36:51 +0000 (01:36 +0100)]
mount: apply SELinux labels before overlayfs mount

Use restorecon to apply SELinux labels if applicable.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
4 years agooverlay: use precompiler macros for reoccuring path names
Daniel Golle [Thu, 15 Oct 2020 23:31:15 +0000 (00:31 +0100)]
overlay: use precompiler macros for reoccuring path names

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
4 years agomount: fix log format string and indentation
Daniel Golle [Thu, 15 Oct 2020 23:33:25 +0000 (00:33 +0100)]
mount: fix log format string and indentation

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
4 years agomount: remove support for legacy overlayfs before v2.3
Daniel Golle [Sun, 11 Oct 2020 00:06:52 +0000 (01:06 +0100)]
mount: remove support for legacy overlayfs before v2.3

overlayfs has been in mainline since Linux v3.18 (OpenWrt CC 15.05).
Remove support for pre-mainline overlayfs.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
4 years agofstoools: add define for GLOB_ONLYDIR
Rosen Penev [Wed, 25 Mar 2020 23:27:47 +0000 (16:27 -0700)]
fstoools: add define for GLOB_ONLYDIR

This was originally a patch for musl. It is not present in musl as it is a
GNU extension.

Place it here where it belongs.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
4 years agoUse autoclear for overlay loopback device
David Woodhouse [Wed, 17 Jun 2020 18:29:56 +0000 (19:29 +0100)]
Use autoclear for overlay loopback device

During a sysupgrade on a block-based device, the partition table might
get updated.

The partitions have to be completely unused by the time partx is
invoked, or it fails thus:
partx: /dev/mmcblk1: error deleting partition 3
partx: /dev/mmcblk1: error adding partition 3

That's cosmetic in some cases, but in others where the old root
partition overlaps with the new partition where the config is stored
during the reboot, it causes a sysugprade failure (resulting in the
backup being lost and a completely clean system image).

Although we carefully unmount the root and overlay file systems, the
problem is that the loopback device used for the overlay isn't being
torn down, and it still has a refcount on the root block partition (in
the above case, /dev/mmcblk1p3).

Installing losetup and adding 'losetup -D' to the switch_to_ramfs()
function makes it work nicely. But the better option that doesn't add a
new dependency is to use the autoclear flag when setting up the loop
device, so it goes away automatically when the overlay file system is
unmounted.

To make that work sanely, we have to *not* close the fd right after
configuring it — or it'll go away immediately. We could store the fd in
the volume struct and either add destructor method or close it after
performing the mount… but honestly it just seems simpler and saner to
"leak" the fd in the knowledge that it'll get closed when the process
exits in a few milliseconds anyway. We can revisit that if anyone
really feels strongly about it. Dissent is best expressed in 'diff -up'
form.

Signed-off-by: David Woodhouse <dwmw2@infradead.org>
4 years agoblockd: fix segfault triggered by non-autofs mounts
Daniel Golle [Mon, 11 May 2020 23:26:28 +0000 (00:26 +0100)]
blockd: fix segfault triggered by non-autofs mounts

Program received signal SIGSEGV, Segmentation fault.
main_autofs (argv=<optimized out>, argc=<optimized out>)
    at fstools-2020-05-06-eec16e2f/block.c:1193
1193:    if (!m->autofs && (mp = find_mount_point(pr->dev))) {

Fixes: 9ab936d ("block(d): always call hotplug.d "mount" scripts from blockd")
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
4 years agoblockd: add optional "device" parameter to "info" ubus method
Rafał Miłecki [Wed, 6 May 2020 15:46:55 +0000 (17:46 +0200)]
blockd: add optional "device" parameter to "info" ubus method

It allows getting info about specified device.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agoblock(d): always call hotplug.d "mount" scripts from blockd
Rafał Miłecki [Wed, 6 May 2020 14:48:06 +0000 (16:48 +0200)]
block(d): always call hotplug.d "mount" scripts from blockd

This resolves problem on boot with "mount" scripts being called too
early to get blockd info. With this change "mount" scripts won't get
called until blockd starts. On startup it requests all devices info and
calls relevant scripts.

This fixes samba36-hotplug package hotplug.d script.

One downside of this change is handling "mount" evens on block restart
or crash. On restart "add" actions will get executed for mounted /
available (autofs) devices. On crash no events will be generated.

Ref: http://lists.infradead.org/pipermail/openwrt-devel/2019-December/020886.html
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agoblockd: use uloop_process for calling /sbin/hotplug-call mount
Rafał Miłecki [Tue, 5 May 2020 19:08:03 +0000 (21:08 +0200)]
blockd: use uloop_process for calling /sbin/hotplug-call mount

As blockd uses uloop calling any script and using waitpid() can easily
result in a lock. It's enough for script to use /bin/ubus to cause that.

It's not an option to drop waitpid() as it's important to e.g. call
mount scripts with ACTION=remove before unmounting devices. So solving
this problem requires using uloop_process.

Unfortunately this means:
1. Using callbacks making code slightly more complex
2. Dropping that nice devices_update_cb()

With this change however hotplug.d "mount" scripts can safely call
"ubus".

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agoTruncate FAT filesystem label until 1st occurance of a blank (0x20)
Mirko Vogt [Sat, 25 Apr 2020 23:47:50 +0000 (01:47 +0200)]
Truncate FAT filesystem label until 1st occurance of a blank (0x20)

According to spec the FAT filesystem label is not terminated by a
\0-byte but instead has a fixed length where the name is padded by
blanks.
Before libblkid-tiny just passed the always 11 bytes long blank padded
string through, however it makes matching for a filesystem label
inconsistent with other filesystems, which use a \0-terminated string.

Before: "foobar     "
After : "foobar"

Signed-off-by: Mirko Vogt <mirko-openwrt@nanl.de>
4 years agoblock: always use st_dev (device ID) of / when looking for root
Rafał Miłecki [Sat, 2 May 2020 12:05:11 +0000 (14:05 +0200)]
block: always use st_dev (device ID) of / when looking for root

Use this method even if UBIFS extroot support is enabled. This is needed
on x86 systems (using F2FS for overlay) as they may still come with F2FS
enabled.

Ref: https://bugs.openwrt.org/index.php?do=details&task_id=2231
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agoblock: simplify check_extroot() a bit
Rafał Miłecki [Tue, 5 May 2020 06:49:17 +0000 (08:49 +0200)]
block: simplify check_extroot() a bit

Avoid too big code indent.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agoblock: add some basic extroot documentation
Rafał Miłecki [Sat, 2 May 2020 11:29:08 +0000 (13:29 +0200)]
block: add some basic extroot documentation

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agoRevert "block: support hierarchical mount/umount"
Rafał Miłecki [Thu, 2 Apr 2020 12:12:13 +0000 (14:12 +0200)]
Revert "block: support hierarchical mount/umount"

This reverts commit fb0700f0fdfc4bec220203d31df87be2b4ccc467.

Switching to AVL tree allows sorting devices by selected parameter
(mount path) but does not implement a real hierarchical layout. That
would require a tree with parent nodes being parent devices / mount
paths.

Cc: Yousong Zhou <yszhou4tech@gmail.com>
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agoRevert "block: mount_action: handle mount/umount deps"
Rafał Miłecki [Thu, 2 Apr 2020 12:12:12 +0000 (14:12 +0200)]
Revert "block: mount_action: handle mount/umount deps"

This reverts commit 32c3126b2f0464106d74317336b6aef1d7d5f82f.

Internally stored list of devices is implemented using struct vlist_tree
and keeps devices sorted by their mount target paths. This DOESN'T mean
that all preceding entires of a given device are its parent devices.
Making such an assumption and mounting all preceding devices results in
unwanted mounts in most cases.

For example having devices like:
/dev/sda1 (mount target: /mnt/foo1)
/dev/sda2 (mount target: /mnt/foo2)
/dev/sdb1 (mount target: /mnt/bar1)
/dev/sdb2 (mount target: /mnt/bar2)
will result in devices vlist_tree having entries sorted like:
/dev/sdb1 (bar1), /dev/sdb2 (bar2), /dev/sda1 (foo1), /dev/sda2 (foo2)

Using autofs and accessing /mnt/foo2 would result in mounting all 4
partitions and spinning unneeded /dev/sdb.

Cc: Yousong Zhou <yszhou4tech@gmail.com>
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agolibfstools/mtd: attempt to read from OOB data if empty space is found
Felix Fietkau [Thu, 9 Apr 2020 11:19:08 +0000 (13:19 +0200)]
libfstools/mtd: attempt to read from OOB data if empty space is found

When using jffs2 on NAND flash, it stores its magic in the OOB data on
newly erased blocks. This change fixes identifying the filesystem type.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
4 years agoblockd: remove symlink linkpath file if it's a dir or link
Rafał Miłecki [Sat, 28 Dec 2019 21:07:22 +0000 (22:07 +0100)]
blockd: remove symlink linkpath file if it's a dir or link

Files like that can remain from using non-autofs mounting and can cause
mounting errors after switching to autofs:
blockd: failed to symlink /mnt/sda1->/tmp/run/blockd/sda1

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agoblock: remove mount target file if it's a link
Rafał Miłecki [Sat, 28 Dec 2019 21:07:21 +0000 (22:07 +0100)]
block: remove mount target file if it's a link

Links like that can remain from using autofs and can cause mounting
errors after switching to non-autofs:

block: mounting /dev/sda1 (vfat) as /mnt/sda1 failed (2) - No such file or directory

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agoblockd: report "target" path as "mount" for autofs available mounts
Rafał Miłecki [Fri, 27 Dec 2019 12:09:11 +0000 (13:09 +0100)]
blockd: report "target" path as "mount" for autofs available mounts

Devices handled with autofs should be seen as available even if
currently unmounted. Mounting is handled on demand and transparently for
users.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agoblockd: print symlink error code and string message
Rafał Miłecki [Sat, 28 Dec 2019 21:29:03 +0000 (22:29 +0100)]
blockd: print symlink error code and string message

It may help understanding failure reason, e.g.:
blockd: failed to symlink /mnt/sda1->/tmp/run/blockd/sda1 (17) - File exists

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agoRevert "fstools: Add support to read-only MTD partitions (eg. recovery images)"
Petr Štetiar [Tue, 21 Jan 2020 16:11:07 +0000 (17:11 +0100)]
Revert "fstools: Add support to read-only MTD partitions (eg. recovery images)"

This reverts commit f5c7c1813f52e6d7b59ecfb2f9f95e69b05b1980 which needs
more work and testing as it broke at least jffs2 overlays at least on
ath79 platform, marking them as read-only, thus unusable:

 jffs2_build_filesystem(): erasing all blocks after the end marker...
 jffs2: Erase at 0x009e0000 failed immediately: -EROFS. Is the sector locked?

Ref: http://lists.infradead.org/pipermail/openwrt-devel/2020-January/021344.html
Reported-by: Steve Brown <sbrown@ewol.com>
Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agofstools: Add support to read-only MTD partitions (eg. recovery images)
Bruno Pena [Sat, 4 Jan 2020 11:52:08 +0000 (12:52 +0100)]
fstools: Add support to read-only MTD partitions (eg. recovery images)

This patch enables fstools to open read-only MTD partitions, which in
turn also enables OpenWrt to boot from read-only partitions.

The use of read-only partitions is of special importance for WiFi-only
devices, where a protected read-only recovery image can be used in case
something goes wrong with the main firmware (eg. user gets locked out
due to bad settings, flash of an unbootable dev firmware, etc).

Signed-off-by: Bruno Pena <brunompena@gmail.com>
4 years agolibblkid-tiny: fix f2fs labels by increasing label buffer
Petr Štetiar [Tue, 14 Jan 2020 18:49:41 +0000 (19:49 +0100)]
libblkid-tiny: fix f2fs labels by increasing label buffer

f2fs max label length is 1024B (512 * uint16_t) which is way above
current 256B used in `struct blkid_struct_probe` which renders labels on
f2fs system unusable:

 $ mkfs.f2fs -l drive /dev/sda1
 $ block info /dev/sda1
 label buffer too small 1024 > 255

So increase the label buffer again in order to make enough space for
f2fs labels.

Fixes: FS#2735
Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoblock: re-discover mtd devices on extroot mount retry
Jo-Philipp Wich [Sun, 5 Jan 2020 16:21:25 +0000 (17:21 +0100)]
block: re-discover mtd devices on extroot mount retry

When retrying extroot mounting after waiting for the underlying device to
appear, the internal device cache was rebuilt without mtd devices, leading
to the following error:

    block: extroot: unable to lookup root device /dev/...

The extroot mount procedure needs access to the actual mtd root device to
temporarily mount it for uuid file verification.

Fix this error by rebuilding the cache with mtd devices to allow the
`check_extroot()` procedure to find the internal root device.

Fixes: FS#2701
Fixes: fb0700f ("block: support hierarchical mount/umount")
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
4 years agolibblkid-tiny: fix symbol collision with full libblkid
Jo-Philipp Wich [Sun, 22 Dec 2019 18:46:05 +0000 (19:46 +0100)]
libblkid-tiny: fix symbol collision with full libblkid

The recent introduction of blkid_new_probe() and blkid_free_probe() in the
dynamically linked libblkid-tiny caused the dlopen'd libblkid.so to call into
the wrong version of blkid_new_probe() within blkid_new_probe_from_filename(),
leading to memory corruption and eventual segmentation faults.

Fixes: b82c5c1 ("libblkid-tiny: add functions for allocating & freeing probe struct")
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
4 years agolibblkid-tiny: vfat: Change parsing label in special cases
Pali Rohár [Tue, 17 Dec 2019 07:28:36 +0000 (08:28 +0100)]
libblkid-tiny: vfat: Change parsing label in special cases

commit f0ca7e80d7a171701d0d04a3eae22d97f15d0683 upstream.

* Use only label from the root directory and do not fallback to the label
  stored in boot sector. This is how MS-DOS 6.22, MS-DOS 7.10, Windows 98,
  Windows XP and also Windows 10 behave. Moreover Windows XP and Windows 10
  do not touch label in boot sector anymore, so removing FAT label on those
  Windowses leads to having old label still stored in boot sector (which
  MS-DOS and Windows fully ignore).

* Label entry "NO NAME" in root directory is treated as label "NO NAME"
  instead of empty label. In root directory it has no special meaning.
  String "NO NAME" has a special meaning (empty label) only for label
  stored in boot sector.

* Label from the boot sector is now stored into LABEL_FATBOOT field. So if
  there are applications which depends or needs to read this label, they
  have ability.

After this change LABEL always correspondent to the label from the root
directory and LABEL_FATBOOT to the label stored in the boot sector. If some
of those labels is missing or is not present (e.g. "NO LABEL" in boot
sector) then particular field is not set.

Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
[rmilecki: drop unneeded now downstream hacks for handling spaces]
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agolibblkid-tiny: vfat: Fix reading labels which starts with byte 0x05
Pali Rohár [Tue, 17 Dec 2019 07:28:35 +0000 (08:28 +0100)]
libblkid-tiny: vfat: Fix reading labels which starts with byte 0x05

commit e526f503918cc29d8b1ccf36a5c3a34645d2be6e upstream.

When FAT directory entry has leading byte 0x05 it is interpreted as byte
0xE5. This is how FAT stores file name which starts with byte 0xE5 as
leading byte in 0xE5 in FAT directory entry means that file slot is empty.

Fixes: #533
4 years agolibblkid-tiny: add blkid_probe_set_id_label() stub
Rafał Miłecki [Tue, 17 Dec 2019 07:28:34 +0000 (08:28 +0100)]
libblkid-tiny: add blkid_probe_set_id_label() stub

This stub will allow porting more upstream code without commenting out
calls and them unused variables. This will simplify maintenance.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agolibblkid-tiny: use separated buffer for each block device read
Rafał Miłecki [Sat, 14 Dec 2019 21:55:01 +0000 (22:55 +0100)]
libblkid-tiny: use separated buffer for each block device read

This allows reading multiple chunks of block device data and operating
on them simultaneously. Previous implementation was using a single
buffer (except for reading more data than allocated size) and subsequent
reads were corrupting memory of previously returned buffers.

This fixes e.g. problem with reading NTFS UUID and validating VFAT
signature.

Implementation is based on original libblkid code which handles it
similarly. Buffers are put on probe internal list and freed when
releasing a probe struct.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agolibblkid-tiny: add functions for allocating & freeing probe struct
Rafał Miłecki [Sat, 14 Dec 2019 21:55:00 +0000 (22:55 +0100)]
libblkid-tiny: add functions for allocating & freeing probe struct

This adds blkid_new_probe() and blkid_free_probe() which have to be used
in place of simple struct memory allocation. They will allow extending
probe struct by any extra initialization code and resources release.

Newly introduced probe.c file is based on original libblkid's code.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
5 years agoblockd: don't flush devices list on "hotplug" call
Rafał Miłecki [Fri, 13 Dec 2019 10:05:12 +0000 (11:05 +0100)]
blockd: don't flush devices list on "hotplug" call

The point of "hotplug" call is to add or remove a single entry to/from
devices list. Using vlist_update() and vlist_flush() was clearing whole
list (and leaving the last entry in case of adding a device).

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
5 years agoblockd: fix vlist memory corruption
Rafał Miłecki [Fri, 13 Dec 2019 10:05:11 +0000 (11:05 +0100)]
blockd: fix vlist memory corruption

vlist_add() expects key to point a persistent memory as it doesn't make
its copy. Passing blob_attr of current message was resulting in
undefined/random behavior including list corruption and possible
crashes.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
5 years agoblock: detect: use current mp as value of target option
Yousong Zhou [Sun, 3 Nov 2019 03:06:32 +0000 (03:06 +0000)]
block: detect: use current mp as value of target option

Move down the function so that no declaration of find_mount_point() is
not needed

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>