lib: [loopdev.c] add module for work loop devices

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2011-05-30 16:37:31 +02:00
parent 87e77645f5
commit 10ee593293
6 changed files with 1224 additions and 3 deletions

View File

@ -15,6 +15,7 @@ dist_noinst_HEADERS = \
linux_reboot.h \
linux_version.h \
list.h \
loopdev.h \
mangle.h \
mbsalign.h \
md5.h \

154
include/loopdev.h Normal file
View File

@ -0,0 +1,154 @@
#ifndef UTIL_LINUX_LOOPDEV_H
#define UTIL_LINUX_LOOPDEV_H
/*
* loop_info.lo_encrypt_type
*/
#define LO_CRYPT_NONE 0
#define LO_CRYPT_XOR 1
#define LO_CRYPT_DES 2
#define LO_CRYPT_CRYPTOAPI 18
#define LOOP_SET_FD 0x4C00
#define LOOP_CLR_FD 0x4C01
/*
* Obsolete (kernel < 2.6)
*
* #define LOOP_SET_STATUS 0x4C02
* #define LOOP_GET_STATUS 0x4C03
*/
#define LOOP_SET_STATUS64 0x4C04
#define LOOP_GET_STATUS64 0x4C05
/* #define LOOP_CHANGE_FD 0x4C06 */
#define LOOP_SET_CAPACITY 0x4C07
/*
* loop_info.lo_flags
*/
enum {
LO_FLAGS_READ_ONLY = 1,
LO_FLAGS_USE_AOPS = 2,
LO_FLAGS_AUTOCLEAR = 4, /* New in 2.6.25 */
};
#define LO_NAME_SIZE 64
#define LO_KEY_SIZE 32
/*
* Linux LOOP_{SET,GET}_STATUS64 ioclt struct
*/
struct loop_info64 {
uint64_t lo_device;
uint64_t lo_inode;
uint64_t lo_rdevice;
uint64_t lo_offset;
uint64_t lo_sizelimit; /* bytes, 0 == max available */
uint32_t lo_number;
uint32_t lo_encrypt_type;
uint32_t lo_encrypt_key_size;
uint32_t lo_flags;
uint8_t lo_file_name[LO_NAME_SIZE];
uint8_t lo_crypt_name[LO_NAME_SIZE];
uint8_t lo_encrypt_key[LO_KEY_SIZE];
uint64_t lo_init[2];
};
#define LOOPDEV_MAJOR 7 /* loop major number */
#define LOOPDEV_DEFAULT_NNODES 8 /* default number of loop devices */
struct loopdev_iter {
FILE *proc; /* /proc/partitions */
int ncur; /* current position */
int *minors; /* ary of minor numbers (when scan whole /dev) */
int nminors; /* number of items in *minors */
int ct_perm; /* count permission problems */
int ct_succ; /* count number of detected devices */
int done:1; /* scanning done */
int default_check:1;/* check first LOOPDEV_NLOOPS */
int flags; /* LOOPITER_FL_* flags */
};
enum {
LOOPITER_FL_FREE = (1 << 0),
LOOPITER_FL_USED = (1 << 1)
};
/*
* handler for work with loop devices
*/
struct loopdev_cxt {
char device[128]; /* device path (e.g. /dev/loop<N>) */
char *filename; /* backing file for loopcxt_set_... */
int fd; /* open(/dev/looo<N>) */
int flags; /* LOOPDEV_FL_* flags */
int has_info:1; /* .info contains data */
int extra_check:1; /* unusual stuff for iterator */
struct sysfs_cxt sysfs; /* pointer to /sys/dev/block/<maj:min>/ */
struct loop_info64 info; /* for GET/SET ioctl */
struct loopdev_iter iter; /* scans /sys or /dev for used/free devices */
};
/*
* loopdev_cxt.flags
*/
enum {
LOOPDEV_FL_RDONLY = (1 << 0), /* default */
LOOPDEV_FL_RDWR = (1 << 1), /* necessary for loop setup only */
LOOPDEV_FL_OFFSET = (1 << 4),
LOOPDEV_FL_NOSYSFS = (1 << 5),
LOOPDEV_FL_NOIOCTL = (1 << 6),
LOOPDEV_FL_DEVSUBDIR = (1 << 7)
};
/*
* High-level
*/
extern int is_loopdev(const char *device);
extern int loopdev_is_autoclear(const char *device);
extern char *loopdev_get_backing_file(const char *device);
extern int loopdev_is_used(const char *device, const char *filename,
uint64_t offset, int flags);
extern char *loopdev_find_by_backing_file(const char *filename,
uint64_t offset, int flags);
extern int loopcxt_find_unused(struct loopdev_cxt *lc);
extern int loopdev_delete(const char *device);
/*
* Low-level
*/
extern int loopcxt_init(struct loopdev_cxt *lc, int flags);
extern void loopcxt_deinit(struct loopdev_cxt *lc);
extern int loopcxt_set_device(struct loopdev_cxt *lc, const char *device);
extern char *loopcxt_strdup_device(struct loopdev_cxt *lc);
extern const char *loopcxt_get_device(struct loopdev_cxt *lc);
extern struct sysfs_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc);
extern int loopcxt_get_fd(struct loopdev_cxt *lc);
extern int loopcxt_init_iterator(struct loopdev_cxt *lc, int flags);
extern int loopcxt_deinit_iterator(struct loopdev_cxt *lc);
extern int loopcxt_next(struct loopdev_cxt *lc);
extern int loopcxt_setup_device(struct loopdev_cxt *lc);
extern int loopcxt_delete_device(struct loopdev_cxt *lc);
int loopcxt_set_offset(struct loopdev_cxt *lc, uint64_t offset);
int loopcxt_set_sizelimit(struct loopdev_cxt *lc, uint64_t sizelimit);
int loopcxt_set_flags(struct loopdev_cxt *lc, uint32_t flags);
int loopcxt_set_backing_file(struct loopdev_cxt *lc, const char *filename);
int loopcxt_set_encryption(struct loopdev_cxt *lc,
const char *encryption,
const char *password);
extern char *loopcxt_get_backing_file(struct loopdev_cxt *lc);
extern int loopcxt_get_offset(struct loopdev_cxt *lc, uint64_t *offset);
extern int loopcxt_get_sizelimit(struct loopdev_cxt *lc, uint64_t *size);
extern int loopcxt_is_autoclear(struct loopdev_cxt *lc);
extern int loopcxt_find_by_backing_file(struct loopdev_cxt *lc,
const char *filename,
uint64_t offset, int flags);
#endif /* UTIL_LINUX_LOOPDEV_H */

View File

@ -111,6 +111,9 @@
#define _PATH_MOUNTED_TMP _PATH_MOUNTED ".tmp"
#ifndef _PATH_DEV
/*
* The tailing '/' in _PATH_DEV is there for compatibility with libc.
*/
# define _PATH_DEV "/dev/"
#endif

View File

@ -3,12 +3,13 @@ include $(top_srcdir)/config/include-Makefile.am
AM_CPPFLAGS += -DTEST_PROGRAM
noinst_PROGRAMS = test_blkdev test_ismounted test_wholedisk test_mangle \
test_tt test_canonicalize test_strutils test_procutils
test_tt test_canonicalize test_strutils test_procutils \
test_at
if LINUX
if HAVE_CPU_SET_T
noinst_PROGRAMS += test_cpuset
endif
noinst_PROGRAMS += test_sysfs
noinst_PROGRAMS += test_sysfs test_loopdev
endif
test_blkdev_SOURCES = blkdev.c
@ -26,10 +27,19 @@ if LINUX
test_cpuset_SOURCES = cpuset.c
test_sysfs_SOURCES = sysfs.c at.c
test_sysfs_CFLAGS = -DTEST_PROGRAM_SYSFS
test_loopdev_SOURCES = loopdev.c \
$(test_sysfs_SOURCES) \
$(top_srcdir)/lib/linux_version.c \
$(top_srcdir)/lib/canonicalize.c
test_loopdev_CFLAGS = -DTEST_PROGRAM_LOOPDEV
endif
test_tt_SOURCES = tt.c $(top_srcdir)/lib/mbsalign.c
test_canonicalize_SOURCES = canonicalize.c
test_canonicalize_CFLAGS = -DTEST_PROGRAM_CANONICALIZE
if LINUX
test_blkdev_SOURCES += linux_version.c

View File

@ -189,7 +189,7 @@ canonicalize_path(const char *path)
}
#ifdef TEST_PROGRAM
#ifdef TEST_PROGRAM_CANONICALIZE
int main(int argc, char **argv)
{
if (argc < 2) {

1053
lib/loopdev.c Normal file

File diff suppressed because it is too large Load Diff