Commit Graph

104 Commits

Author SHA1 Message Date
Karel Zak 3e03cb6806 lib/loopdev: perform retry on EAGAIN
Suggested-by: Ingo Franzki <ifranzki@linux.ibm.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-06-15 10:34:46 +02:00
Manuel Bentele cd3f22e6e0
losetup: fix wrong printf() format specifier for ino_t data type
Since the range of the ino_t data type is platform-specific (depending on
the wordsize), a usage of the fixed format specifier %PRIu64 is not correct
for ino_t on some 32-bit architectures, eg. ARM (Raspberry Pi 1). This issue
may lead to undefinied output and is not reported by gcc (in version 10.2.0
and 8.3.0-6+rpi1) even though -Wformat is enabled by -Wall. Therefore it is
most likely that it seems to be a false negative error in gcc's format
specifier check, so that this issue was never detected before.

This change fixes the issue by the use of a cast, since there is no
platform-independent format specifier for ino_t available. The wrong format
specifier %PRIu64 is replaced by %ju, where its corresponding variable of
type ino_t is casted to uintmax_t. The type uintmax_t represents the largest
platform-specific unsigned integer, so that all integer values are preserved
for a platform-independent printing.

Fixes: https://github.com/karelzak/util-linux/issues/1211
Signed-off-by: Manuel Bentele <development@manuel-bentele.de>
2020-12-05 23:51:27 +01:00
Karel Zak 75cd5e2f28 losetup: increase limit of setup attempts
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-10-13 12:55:44 +02:00
Karel Zak 3ff6fb802d losetup: avoid infinite busy loop
issue report:
 if i run the heavy duty test from #16859 a couple of times I can get
 the loopback layer in the kernel into a state where there's a loopback
 block device allocated, that you can open, but where both LOOP_CLR_FD
 and _SET_FD fail with EBUSY. and /dev/loop-control still returns it as
 the next free one...  weird state util-linux losetup when called to
 allocate a new device then freezes

This commit:
* restrict number of attempts to 16
* use 200000ms sleep between attempts
* add note about non-atomic loop device setup to the man page

Reported-by: Lennart Poettering <lennart@poettering.net>
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-10-13 10:58:04 +02:00
Sinan Kaya d5fd456c88 loopdev: use LOOP_CONFIG ioctl
Prefer to use the new LOOP_CONFIG ioctl.
https://lwn.net/Articles/820408/

Signed-off-by: Sinan Kaya <sinan.kaya@microsoft.com>
2020-09-30 20:04:21 -04:00
Stanislav Brabec ec06412c90 losetup: Typo fix
Fix a typo in usage introduced in a1a41597bf.

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
2019-06-21 14:38:32 +02:00
Karel Zak 25b7045e5d misc: replaces atexit(close_stdout) with new close_stdout_atexit()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-06-17 15:46:10 +02:00
Karel Zak 2c308875a7 misc: consolidate version printing and close_stdout()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-16 15:14:13 +02:00
Jeffrey Ferreira 014e7eb321 losetup: update an error message
Update an error message to add --partscan to the list of options
only allowed during loop device setup.

Signed-off-by: Jeffrey Ferreira <jeffpferreira@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-01-22 11:56:16 +01:00
Karel Zak 422f0e9f20 losetup: properly use --sector-size when create a new device
The --partscan functionality depends on sector size. Make sure
sector size is set before we force kernel to scan the device for
partitions. For example:

 losetup -f loopfile --sector-size 4KiB --partscan --show

where 'loopfile' contains GPT with 4096 sectors.

Reported-by: Jeffrey Ferreira <jeffpferreira@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-01-22 11:50:20 +01:00
Karel Zak 9fcc893624 lib/loopdev: differentiate between setter()s and ioctl calls
Now the internal API uses loopcxt_set_...() to set context variables
as well as to call ioctls. This patch introduces loopcxt_ioctl_...()
to makes things more obvious to readers.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-01-22 11:12:42 +01:00
Karel Zak 6daf185c3c losetup: use offset in warn_size() calculation
# dd if=/dev/urandom of=dummy bs=513 count=1 2>/dev/null

 # losetup -f --show --offset 1 dummy
 /dev/loop0
 losetup: dummy: Warning: file does not fit into a 512-byte sector; the end of the file will be ignore

The warning is bogus as with offset the size of the file fits into
512-byte sector. Simple test that all is right:

 # dd if=dummy bs=1 skip=1 2>/dev/null | sha256sum - /dev/loop0
 da2ed0ade6ea518a802cd8a7a3c01c408ad8699ef6856b0b01f92b867d4ba9b5  -
 da2ed0ade6ea518a802cd8a7a3c01c408ad8699ef6856b0b01f92b867d4ba9b5  /dev/loop0

Addresses: https://github.com/karelzak/util-linux/issues/722
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-01-18 12:13:12 +01:00
Karel Zak 9c6af25c31 losetup: fix mem leak, improve code [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-25 11:56:44 +02:00
Karel Zak bcf445fd68 lib/path lib/sysfs: add debug
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak c3f5a0f1d4 losetup: keep -f and <devname> mutually exclusive
losetup tries to blindly use specified device as well as search for
the first free device, the result is:

 # losetup /dev/loop1 -f /tmp/tfile_loop1
 losetup: /dev/loop1: failed to set up loop device: Invalid argument

fixed version:

 # losetup /dev/loop10 -f img
 losetup: unexpected arguments

Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1566432
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-07 12:05:08 +02:00
Sami Kerola 289673b1c0
losetup: add --output-all option
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-05-03 19:56:20 +01:00
Karel Zak 46de0fe6e3 losetup: use scols_column_set_json_type()
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-04-18 15:25:20 +02:00
Karel Zak e9aa2a14b0 losetup: fix bitwise vs. logical typo [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-10-12 14:45:11 +02:00
Karel Zak 76493ceba6 losetup: rename to --sector-size and LOG-SEC
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-10-02 11:59:57 +02:00
Stanislav Brabec a1a41597bf losetup: Add support for logical block size
Kernel since 4.14 supports setting of logical block size[1]. It allows to
create loop devices that report logical block size different from 512.

Add support for this feature to losetup.

References:
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/block/loop.c?id=89e4fdecb51cf5535867026274bc97de9480ade5

[kzak@redhat.com: - fix loopcxt_get_blocksize()
                  - remove lo_blocksize from loop_info64]

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Hannes Reinecke <hare@suse.com>
Cc: Omar Sandoval <osandov@fb.com>
Cc: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-09-27 14:30:02 +02:00
Ruediger Meier f45f3ec34a misc: consolidate macro style USAGE_HELP_OPTIONS
changed in include/c.h and applied via sed:

  sed -i 's/fprintf.*\(USAGE_MAN_TAIL.*\)/printf(\1/' $(git ls-files -- "*.c")
  sed -i 's/print_usage_help_options\(.*\);/printf(USAGE_HELP_OPTIONS\1);/' $(git ls-files -- "*.c")

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-29 16:54:33 +02:00
Ruediger Meier b1a294c448 misc: introduce print_usage_help_options()
Consolidate --help and --version descriptions. We are
now able to align them to the other options.

We changed include/c.h. The rest of this patch was
generated by sed, plus manually setting the right
alignment numbers. We do not change anything but
white spaces in the --help output.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-27 12:26:19 +02:00
Ruediger Meier 6e1eda6f22 misc: never use usage(stderr)
Here we fix all cases where we have usage(FILE*)
functions.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-26 14:38:24 +02:00
Karel Zak c3a4cfc579 misc: consolidate usage() "Available columns"
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-06-26 14:09:53 +02:00
Sami Kerola a9e3ec1989 losetup: add missing initializer [clang]
sys-utils/losetup.c:290:12: warning: variable 'rc' is used uninitialized
	whenever 'if' condition is false [-Wsometimes-uninitialized]
sys-utils/losetup.c:293:7: note: uninitialized use occurs here
sys-utils/losetup.c:290:8: note: remove the 'if' if its condition is always true
sys-utils/losetup.c:228:9: note: initialize the variable 'rc' to silence
	this warning

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-06-14 12:21:01 +02:00
Sami Kerola 1b5042636f misc: fix reassigned values before old ones has been used [cppcheck]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-06-14 12:19:20 +02:00
Karel Zak 780ce22cda misc: consolidate smartcols error messages
... just to keep translators happy

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-05-18 11:39:34 +02:00
Frederick Grose ec3624aa75 losetup.8: Reorder options documentation
Reorder options to match onboard help and group functionally.
Include some missing optional arguments.

[kzak@redhat.com: - don't include available columns in man page,
                  - add comments to the usage()
		  - fix order in usage()
		  - fix typo in usage()]

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-04-04 12:52:06 +02:00
Sami Kerola 8791804065 misc: do not use plain 0 as NULL [smatch scan]
text-utils/tailf.c:69:21: warning: Using plain integer as NULL pointer

Since many 'struct option' has used zero as NULL make them more readable in
same go by reindenting, and using named argument requirements.

Reference: https://lwn.net/Articles/93577/
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-02-20 12:58:49 +01:00
Karel Zak 677ec86cef Use --help suggestion on invalid option
The current default is to print all usage() output. This is overkill
in many case.

Addresses: https://github.com/karelzak/util-linux/issues/338
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-12-19 13:13:34 +01:00
Stanislav Brabec f3b0d1505e losetup --nooverlap: fix typo
Option is documented and used as --nooverlap, bud coded as --nooverlaps.
--nooverlap is considered as shortened option, making the typo
invisible.

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
2016-10-06 14:56:39 +02:00
Karel Zak d8ba61fcb4 losetup: allow to use --nooverlap when device specified
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-08-17 13:52:22 +02:00
Stanislav Brabec f27d989c67 losetup: Prevent AUTOCLEAR detach race
Kernel needs some time to delete a device after losetup --detach. If
the losetup --find --nooverlay is called just after losetup --delete,
it can sometimes attempt to recycle the device that is just being
released.  To prevent this race, clear the AUTOCLEAR flag of the
device.

[kzak@redhat.com: - rebase to the new version of the code]

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-08-17 12:58:12 +02:00
Karel Zak 9a94b634a3 losetup: add --nooverlap options
This patch introduces overlap detections and loop devices
re-use for losetup(8). We already support this feature for mount(8)
where it's enabled by default (because we mount filesystems and it's
always mistake to share the same filesystem between more loop
devices).

Stanislav has suggested to enable this feature also for losetup by
default. I'm not sure about it, IMHO it's better to keep losetup(8)
simple and stupid by default, and inform users about possible problems
and solutions in the man page.

The feature forces losetup to scan all loop devices always when new
one is requested. This maybe disadvantage (especially when we use
control-loop  to avoid /sys or /dev scans) on system with huge number
of loop devices.

Co-Author: Stanislav Brabec <sbrabec@suse.cz>
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-08-17 12:28:33 +02:00
Stanislav Brabec 74a4705a99 Add sizelimit to internal API
Fully safe checks of loop device need to check sizelimit. To prevent need of two
nearly equal functions, introduce sizelimit parameter to several internal
functions:
loopdev_is_used()
loopdev_find_by_backing_file()
loopcxt_is_used()
loopcxt_find_by_backing_file()

If sizelimit is zero, fall back to the old behavior (ignoring of sizelimit).

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
2016-08-03 11:53:28 +02:00
Yuriy M. Kaminskiy c79657ca85 losetup: fix outdated comment
From abb2a420924c792be33aa3ed825b1348ea7d51a9 Mon Sep 17 00:00:00 2001
From: "Yuriy M. Kaminskiy" <yumkam@gmail.com>
Date: Wed, 13 Apr 2016 17:30:10 +0300
Subject: [PATCH] losetup: fix outdated comment

--direct-io argument was made optional by
f98d9641a3, but this comment was not
updated accordingly

Signed-off-by: Yuriy M. Kaminskiy <yumkam@gmail.com>
2016-04-18 11:04:16 +02:00
Karel Zak 3fe3f560b7 Revert "include sysmacros.h where used"
This reverts commit 46a40c0184.
2016-03-08 14:26:33 +01:00
Mike Frysinger 46a40c0184 include sysmacros.h where used
BSD/Linux systems stick major/minor/makedev in sysmacros.h.  Newer Linux
libraries have been moving away from including sysmacros.h implicitly via
sys/types.h, so include it directly.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
2016-03-08 14:21:55 +01:00
Karel Zak f98d9641a3 losetup: clean up --direct-io
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-11-19 11:57:05 +01:00
Ming Lei faeef4d2e9 losetup: support list direct io
So that user can see if DIO is set for current loop device.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
2015-11-19 11:27:36 +01:00
Ming Lei 64c3bb3cb7 losetup: support ioctl cmd of LOOP_SET_DIRECT_IO
From v4.4, linux kernel starts to support direct I/O and
AIO to backing file for loop driver, so allow losetup to
enable the feature by using LOOP_SET_DIRECT_IO ioctl cmd.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
2015-11-19 11:27:36 +01:00
Karel Zak 4827093d4b losetup: add --json
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-06-05 10:30:52 +02:00
Karel Zak 362f5d2035 losetup: print more usable error message on failed -f
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1215370
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-06-03 12:05:09 +02:00
Karel Zak 40b175084f lib/strutils: fix string_add_to_idarray() int vs. size_t
The function uses "int" as argument, but for array size (and index) is better
to use unsigned type (size_t). If we mix "size_t" in util (e.g. fdisk)
and "int" in lib/strutils.c then result is unexpected behavior on
ppc64.

	# sfdisk --list -o DEVICE,START,SIZE /dev/sdb
	Disk /dev/sdb: 50 MiB, 52428800 bytes, 102400 sectors
	Units: sectors of 1 * 512 = 512 bytes
	Sector size (logical/physical): 512 bytes / 4096 bytes
	I/O size (minimum/optimal): 4096 bytes / 32768 bytes
	Disklabel type: gpt
	Disk identifier: 3B8559DB-33AF-43E9-BEFC-C331D829B539
	lt-sfdisk: libfdisk/src/label.c:178: fdisk_label_get_field: Assertion `id > 0' failed.

The patch cleanup all code to use size_t everywhere.

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-04-22 11:08:05 +02:00
Benno Schulenberg 451dbcfae1 textual: add a docstring to most of the utilities
This adds a concise description of a tool to its usage text.

A first form of this patch was proposed by Steven Honeyman
(see http://www.spinics.net/lists/util-linux-ng/msg09994.html).

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2015-01-06 11:27:38 +01:00
Karel Zak d78deddc6d losetup: clean up columns[] usage
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-07-18 13:52:02 +02:00
Karel Zak 710ed55dcd libsmartcols: add debug messages
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-06-06 13:47:06 +02:00
Ruediger Meier 60cb2c3720 build: fix some compiler warnings
Most of them catched on 32bit gcc and icc.

disk-utils/fsck.cramfs.c:     printf format type
lib/boottime.c:               unused variables
misc-utils/cal.c:             set but never used
sys-utils/losetup.c:          set but never used
sys-utils/lscpu-dmi.c:        defined but not used
sys-utils/switch_root.c:      comparison between signed and unsigned
tests/helpers/test_sysinfo.c: printf format type
2014-05-30 01:18:09 +02:00
Stanislav Brabec d6ef7d6c69 losetup: don't ignore EBUSY in losetup when the loop device is given
losetup ignores the EBUSY error.  In the loop fo A_CREATE action, it skips
the error handling when errno = EBUSY.  It's OK for the case where no loop
device is specified, but the error has to be handled when the device is given
explicitly.

Author:  Takashi Iwai <tiwai@suse.de>
Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
2014-05-28 09:18:40 +02:00
Karel Zak 3e55659feb losetup: return EXIT_FAILURE on unsuccessful -f
Reported-by: Ruediger Meier <sweet_f_a@gmx.de>
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-05-13 11:34:30 +02:00