Commit Graph

78 Commits

Author SHA1 Message Date
Karel Zak 846985f743 losetup: remove 32-bit ioctls
... kernel 2.4 ?! Who cares?

Signed-off-by: Karel Zak <kzak@redhat.com>
2011-12-20 12:20:51 +01:00
Karel Zak 6541e717d9 losetup: remove #ifdef LOOP_SET_FD nonsense
It's build-system task to enable/disable utils according to accessible
system features.

Signed-off-by: Karel Zak <kzak@redhat.com>
2011-12-20 12:13:06 +01:00
Karel Zak 653290586f losetup: remove obsolete #ifdef MAIN
Signed-off-by: Karel Zak <kzak@redhat.com>
2011-12-20 11:41:24 +01:00
Petr Uzel 34f9b684c0 losetup: new -D option to delete all used loop devices
Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
Signed-off-by: Karel Zak <kzak@redhat.com>
2011-10-03 15:50:42 +02:00
Petr Uzel c6455a945c losetup: print verbose messages if requested
verbose can't be > 1

Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
2011-10-03 15:48:33 +02:00
Petr Uzel 960cf5737e misc: use %m in format string instead of %s and strerror(errno)
Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
2011-09-27 15:46:07 +02:00
Davidlohr Bueso 7ef9fd7cdb mount: use common libs
Get rid of the local xmalloc.[c/h] files by using the global xalloc and
strutils libraries.

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
2011-09-27 12:49:52 +02:00
Andreas Schwab 3c55d914df losetup: correct qsort compare function
Return the correct value from comparing the operands.

Signed-off-by: Andreas Schwab <schwab@linux-m68k.org>
2011-08-23 11:58:44 +02:00
Karel Zak b71e4c30b6 losetup: allow to use --verbose with --all
Reported-by: Bernhard Voelker <mail@bernhard-voelker.de>
Signed-off-by: Karel Zak <kzak@redhat.com>
2011-08-17 16:40:05 +02:00
Karel Zak 6997468eaa losetup: cleanup usage()
Signed-off-by: Karel Zak <kzak@redhat.com>
2011-08-16 14:00:06 +02:00
Karel Zak d759ac366a mount: fix compiler warnings [-Wsign-compare -Wunused-parameter]
Signed-off-by: Karel Zak <kzak@redhat.com>
2011-08-01 13:33:04 +02:00
Karel Zak 6c7d5ae9a2 move struct option to .rodata
It does not make sense to have writable large arrays of "struct
option" on the stack.

Signed-off-by: Karel Zak <kzak@redhat.com>
2011-03-03 15:00:30 +01:00
Petr Uzel f4612577c9 umount: allow unmounting loopdev specified by associated file
Make it possible to unmount a filesystem on a loop device if it is
specified by associated backing file. It does not attempt to unmount
anything if there are more than one loop device associated with the
given file.

Umount looks for associated loopdevice(s) only if umount is called
with the regular file as an argument.

Before:
	mount -o loop -t ext2 img mnt
	umount -v img
	> Could not find /home/puzel/upstream/util-linux/img in mtab
	> umount: img: not mounted

After:
	mount -o loop -t ext2 img mnt
	umount -v img
	> img is associated with /dev/loop0, trying to unmount it
	> /dev/loop0 has been unmounted

[kzak@redhat.com: - fix memory leak in lomount.c]

Addresses: https://bugzilla.novell.com/show_bug.cgi?id=666161
Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
Signed-off-by: Karel Zak <kzak@redhat.com>
2011-02-21 14:54:43 +01:00
Petr Uzel b642d0e0c9 losetup: spelling fixes
Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
2011-02-21 14:41:34 +01:00
Karel Zak 218f9d3d66 remove duplicate includes
Signed-off-by: Karel Zak <kzak@redhat.com>
2011-01-04 22:36:09 +01:00
Karel Zak db3b5b76a7 mount: read /sys for loopdev backing file
On systems without /etc/mtab (or everywhere if kernel >= 2.6.37)
we use loop autoclear flag and then the backing file name is not
stored in /etc/mtab. mount(8) uses sysfs to get the filename (or
LOOP_GET_STATU* ioctls on old kernels).

Signed-off-by: Karel Zak <kzak@redhat.com>
2010-12-01 23:02:17 +01:00
Karel Zak b61e939013 losetup: use /sys/dev/block/.../loop/backing_file
The basic loopdev attributes are available in sysfs since kernel
2.6.37. This patch uses the backing_file attribute from sysfs for very
long filenames (the LOOP_GET_STATUS ioctl uses only 64 bytes for the
filename).

old version:
 # losetup -a
 /dev/loop0: [0804]:12865322 (/home/images/filesystems/this_is_really_really_long_directory_*)

new version:
 # losetup -a
 /dev/loop0: [0804]:12865322 (/home/images/filesystems/this_is_really_really_long_directory_name/ext2.img)

Signed-off-by: Karel Zak <kzak@redhat.com>
2010-12-01 14:04:35 +01:00
Davidlohr Bueso 8abcf29002 lib: [strutils] general purpose string handling functions
This patch replaces a few functions used throughout the source:
* Renames getnum (from schedutils) to strtol_or_err
* Moves strtosize (from lib/strtosize.c)
* Moves xstrncpy (from include/xstrncpy.h)
* Adds strnlen, strnchr and strndup if not available (remove it from libmount utils)

A few Makefile.am files were modified to compile accordingly along with trivial renaming
in schedutils source code.

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
2010-11-23 21:06:49 +01:00
Tobias Klauser 9695a7c653 losetup: do not distinguish between malloc and realloc
realloc(NULL, size) behaves the same as malloc(size) so there is no need
to distinguish between the two.

[kzak@redhat.com: - better handle realloc() errors]

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Karel Zak <kzak@redhat.com>
2010-08-20 13:05:21 +02:00
Karel Zak 108591d2b7 losetup: use stdout and return 0 for --help
Reported-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2010-08-04 11:54:19 +02:00
Karel Zak ca1e1363e7 losetup: support suffixes and hex numbers for --offset and --sizelimit
Reported-By: Liu Aleaxander <aleaxander@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2010-03-30 15:09:41 +02:00
Karel Zak c4c0eedc4d losetup: remove unused macro
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-10-16 01:34:20 +02:00
Karel Zak 14b613f3a9 include: fix _PATH_DEV
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-07-02 09:50:26 +02:00
Karel Zak c74c0dafff include: clean up _PATH_DEV_* macros
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-06-30 01:13:42 +02:00
Karel Zak 26d5130869 losetup: fix return codes of functions arounf is_associated()
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-06-30 00:48:15 +02:00
Karel Zak d34ac93a61 losetup: add --set-capacity
The LOOP_SET_CAPACITY allows to resize loop device size.

Example:

 # blockdev --getsize64 /dev/loop0
 10485760

 # dd if=/dev/zero of=/home/images/aaa.img count=10 bs=1M oflag=append conv=notrunc

 # blockdev --getsize64 /dev/loop0
 10485760

 # ./losetup --set-capacity /dev/loop0

 # blockdev --getsize64 /dev/loop0
 20971520

CC: J. R. Okajima <hooanon05@yahoo.co.jp>
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-05-29 21:46:00 +02:00
Mike Frysinger 72f6902e43 losetup: handle symlinks in /dev/loop/
The loop_scandir() expects all the files in /dev/loop/ to be actual
devices and not symlinks to devices.  However, udev by default sets up
symlinks in /dev/loop/ thereby breaking the fallback scan logic when
looking for devices more than the default number of 8.

Simple way to reproduce is:
 - <detach all devices>
 - cd /dev/loop
 - rm -f [2-7]
 - losetup 0 autogen.sh
 - mv 1 8
 - for n in {1..7} ; do ln -s 0 $n ; done
 - losetup -f
	<error but should have found /dev/loop/8>

[kay.sievers@vrfy.org:  
       just a note, udev does not do that by default. It is
       devfs-nonsense, which the "default" never did.]

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Reported-by: Mario Bachmann <grafgrimm77@gmx.de>
2009-05-11 10:54:41 +02:00
Karel Zak fc08b4bb9d losetup: mount endless loop hang
Based on
       fix-hang-on-stale-nfs.diff
from SUSE src.rpm package.

It seems better to more precisely check for ENXIO rather than assume
that all LOOP_GET_STATUS errors mean that the device is not used.

Addresses-Novell-Bugzilla: #449646
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-04-23 15:09:04 +02:00
Karel Zak 63754e714a mount: remove useless if-before-free tests
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-02-24 16:41:17 +01:00
Karel Zak 27d320d6f9 losetup: detach more devices by "-d <loop> [<loop> ..]"
This patch allows to detach more devices by "losetup -d",
for example:

  # losetup -a
  /dev/loop0: [0804]:9955739 (/home/images/ary0.img)
  /dev/loop1: [0804]:9955740 (/home/images/ary1.img)
  /dev/loop2: [0804]:9955745 (/home/images/ary2.img)

  # losetup -d /dev/loop0 /dev/loop1 /dev/loop2

Signed-off-by: Karel Zak <kzak@redhat.com>
2009-02-19 18:51:43 +01:00
Karel Zak 74a9c6f7b7 mount: move realpath.c code to lib/
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-02-11 23:55:51 +01:00
Karel Zak c268327edd losetup: add warning about read-only mode
Signed-off-by: Karel Zak <kzak@redhat.com>
2008-11-18 15:50:38 +01:00
Matthias Koenig bc984ef97c losetup: try to set up loop readonly if EACCES
Currently mounting/losetup an image fails if it is accessable readonly.

There are no problems if it is a file on a local filesystem.
It seems only to happen if it is a NFS mounted image, which is
read-write in permission but with root_squash option.

set_loop checks only for EROFS to retry open with readonly mode,
but in this case we get EACCES.

Signed-off-by: Matthias Koenig <mkoenig@suse.de>
2008-11-18 15:40:34 +01:00
Pedro Ribeiro 5bbba4a5bd losetup: several strings without gettext strings
Signed-off-by: Pedro Ribeiro <p.m42.ribeiro@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2008-10-03 09:25:10 +02:00
Karel Zak af17e0d170 losetup: missing EBUSY error hint message
old version:
 # losetup /dev/loop0 /foo.img
 # losetup /dev/loop0 /bar.img; echo $?
 2

new version:
 # losetup /dev/loop0 /foo.img
 # losetup /dev/loop0 /bar.img; echo $?
 losetup: /dev/loop0: device is busy
 2

Signed-off-by: Karel Zak <kzak@redhat.com>
2008-09-30 13:16:50 +02:00
Karel Zak 65158b1081 losetup: remove dependence on minor numbers
The latest kernel supports partitioned loop devices (kernel commit
476a4813cfddf7cf159956cc0e2d3c830c1507e3). Unfortunately, this change
makes minor numbers useless, because mirror number does not match with
loop device name (loop<N>).

We have to follow loop device names only.

Signed-off-by: Karel Zak <kzak@redhat.com>
2008-08-21 12:06:25 +02:00
Karel Zak edb68d0ca3 losetup: looplist_* refactoring, remove scandir()
This patch replaces scandir-based implementation with readdir(). The
readdir(3) is less expensive and more portable (to non-glibc environment).

The patch also replaces sysfs-based solution with simpler /proc/partitions
parsing. The /proc/partitions includes all used loop devices on all systems
(include 2.4). This solution seems faster than scandir(/sys/block/) too.

Summary, the losetup (with this patch) uses three methods to found a
loop device:

  a) parse /proc/partitions to found already used loop devices (for
     loserup -a)

  b) stat(2) for all loop[0-7] devices (default number of loop devices).
     This is classic method from util-linux <= 2.13. This method is good
     enough for standard Linux machines with default number of loop
     devices.

  c) scan all /dev or /dev/loop/ for loop devices. This is useful for
     crazy people who need more than 8 loop devices.

Signed-off-by: Karel Zak <kzak@redhat.com>
2008-08-04 13:54:27 +02:00
Karel Zak b4cbb7b897 losetup: clean up code around LO_FLAGS_AUTOCLEAR
Signed-off-by: Karel Zak <kzak@redhat.com>
2008-07-02 15:16:11 +02:00
Karel Zak e84feaecfd umount: improve "-d" option for autoclear loops
The new loop auto-destruct feature detaches automatically loop devices
when no longer used. This means they are detached with the umount()
call. But when we call umount with -d, del_loop() is called and fails
because the ioctl() returns ENXIO. We have to check for autoclear
loop devices rather than blindly call del_loop().

Reported-by: Matthias Koenig <mkoenig@suse.de>
Signed-off-by: Karel Zak <kzak@redhat.com>
2008-07-02 15:01:28 +02:00
Benno Schulenberg f01fd66c1a losetup: split help message into two smaller parts
As it has already changed since the previous release, take the opportunity
to cut it into two more manageable chunks for translators.

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
Signed-off-by: Karel Zak <kzak@redhat.com>
2008-04-16 00:22:46 +02:00
Karel Zak 4ac02d4dd0 losetup: mark the option -s as deprecated
Signed-off-by: Karel Zak <kzak@redhat.com>
2008-04-15 15:18:48 +02:00
Shachar Shemesh 7bcefc7f03 losetup: add --sizelimit option
[kzak@redhat.com: split the original patch to small patches]
Signed-off-by: Shachar Shemesh <shachar@debian.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
2008-04-15 12:57:35 +02:00
Bernardo Innocenti 024e1a4f90 mount: allow auto-destruction of loop devices
This allows a flag to be set on loop devices so that when they are closed
for the last time, they'll self-destruct.

The kernel part has been submitted to lkml by David Woodhouse.

Signed-off-by: Bernardo Innocenti <bernie@codewiz.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
2008-02-19 01:05:55 +01:00
Karel Zak 259fcc5734 losetup: add --associated option
losetup -j | --associated <file> [-o <num>]

This new option lists all loop devices associated with given file
(and optionally file offset).

Examples:

 # losetup -a
 /dev/loop0: [0807]:21921808 (/foo/vfat-mbr.img)
 /dev/loop1: [0807]:21921808 (/foo/vfat-mbr.img), offset 100
 /dev/loop2: [0807]:21921802 (/bar/ext3.img)

 # losetup -j /foo/vfat-mbr.img
 /dev/loop0: [0807]:21921808 (/foo/vfat-mbr.img)
 /dev/loop1: [0807]:21921808 (/foo/vfat-mbr.img), offset 100

 # losetup -j /foo/vfat-mbr.img -o 100
 /dev/loop1: [0807]:21921808 (/foo/vfat-mbr.img), offset 100

Signed-off-by: Karel Zak <kzak@redhat.com>
2008-02-08 01:31:45 +01:00
Karel Zak 2b326e7cf2 losetup: support unlimited number of loops
Old implementation:

 - supports 256 loop devices only
 - doesn't support gaps in list of loop devices
   (e.g. loop0, loop1, loop3 -- loop3 is invisible)

Kernel 2.6.21 removes artificial maximum 256 loop device. Now the maximum
of loop devices could be really huge (depends on limit of MINOR
numbers). It means we need a better way how work with loop devices
than blindly call stat(2) for all 0-1048575 devices.

This patch uses three methods:

 a) scan /sys/block/loopN (used for losetup -a only). This method is
    probably the fastest way how found used loop device on machine with
    huge number of devices in /dev.

 b) classic way, stat(2) for all loop[0-7] devices (default number of
    loop devices). This cheap method is sufficient for 99% of all machines.

 c) scan all /dev/loopN or /dev/loop/N

Signed-off-by: Karel Zak <kzak@redhat.com>
2008-02-08 01:31:36 +01:00
Samuel Thibault 7711bc1733 losetup: use standard uintxy_t types (struct loop_info64)
The kernel loop_info64 definition uses __u{8,32,64} types,
but userspace uses unsigned {long long,int,char} types.

This patch turns into exactly defined standard uint{8,32,64}_t types.

Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
2007-12-03 15:27:52 +01:00
Matthias Koenig f7858f66f7 mount: fix fd leak
Signed-off-by: Matthias Koenig <mkoenig@suse.de>
2007-11-26 14:04:15 +01:00
Karel Zak 2368077223 mount: prevent loop mounting the same file twice
The mount syscall prevents mounting the same device twice
to the same mountpoint. When loop mounting a file, for each
file a new loop device gets allocated, which prevents the detection
of loop mounting the same file to the same mountpoint twice.
The patch adds a check to prevent double mounts, if the same loopfile
is going to be mounted with the same offset to the same mountpoint.

Co-Author: Matthias Koenig <mkoenig@suse.de>
Signed-off-by: Matthias Koenig <mkoenig@suse.de>
Signed-off-by: Karel Zak <kzak@redhat.com>
2007-10-26 01:02:44 +02:00
Karel Zak bfdb8be5c4 losetup: canonicalize loopfile name
When setting up a loop device, canonicalize the loop file
name. This simplifies a later identification of loop file names
when querying the loop devices.

Co-Author: Matthias Koenig <mkoenig@suse.de>
Signed-off-by: Matthias Koenig <mkoenig@suse.de>
Signed-off-by: Karel Zak <kzak@redhat.com>
2007-10-26 01:02:12 +02:00
Karel Zak 0e7cd33ef2 losetup: fix errno usage
Signed-off-by: Karel Zak <kzak@redhat.com>
2007-10-25 21:50:59 +02:00