Move dm backend initialisation to library calls.

git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@125 36d66b0a-2a48-0410-832c-cd162a569da5
This commit is contained in:
Milan Broz
2009-10-01 10:14:32 +00:00
parent 8bec41ab34
commit de95a38381
4 changed files with 72 additions and 31 deletions

View File

@@ -3,6 +3,7 @@
* Do not use internal lib functions in cryptsetup.
* Add crypt_log to library.
* Fix crypt_remove_device (remove, luksClose) implementation.
* Move dm backend initialisation to library calls.
2009-09-28 Milan Broz <mbroz@redhat.com>
* Add luksHeaderBackup and luksHeaderRestore commands.

View File

@@ -44,12 +44,15 @@ int dm_init(struct crypt_device *context, int check_kernel)
if (!_dm_use_count++) {
log_dbg("Initialising device-mapper backend%s.",
check_kernel ? "" : " (NO kernel check requested)");
if (check_kernel && !_dm_simple(DM_DEVICE_LIST_VERSIONS, NULL))
if (check_kernel && !_dm_simple(DM_DEVICE_LIST_VERSIONS, NULL)) {
log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
return -1;
}
dm_log_init(set_dm_error);
dm_log_init_verbose(10);
}
// FIXME: global context is not safe
if (context)
_context = context;

View File

@@ -643,6 +643,9 @@ int crypt_resize_device(struct crypt_options *options)
uint64_t size, skip, offset;
int key_size, read_only, r;
if (dm_init(NULL, 1) < 0)
return -ENOSYS;
r = dm_query_device(options->name, &device, &size, &skip, &offset,
&cipher, &key_size, &key, &read_only, NULL, &uuid);
if (r < 0)
@@ -676,6 +679,7 @@ out:
free(device);
free(uuid);
crypt_free(cd);
dm_exit();
return r;
}
@@ -684,14 +688,20 @@ int crypt_query_device(struct crypt_options *options)
{
int read_only, r;
if (dm_init(NULL, 1) < 0)
return -ENOSYS;
r = dm_status_device(options->name);
if (r == -ENODEV)
if (r == -ENODEV) {
dm_exit();
return 0;
}
r = dm_query_device(options->name, (char **)&options->device, &options->size,
&options->skip, &options->offset, (char **)&options->cipher,
&options->key_size, NULL, &read_only, NULL, NULL);
dm_exit();
if (r < 0)
return r;
@@ -973,7 +983,7 @@ int crypt_init(struct crypt_device **cd, const char *device)
return -ENOMEM;
}
if (!dm_init(h, 1) < 0) {
if (dm_init(h, 1) < 0) {
free(h);
return -ENOSYS;
}
@@ -994,6 +1004,9 @@ int crypt_init_by_name(struct crypt_device **cd, const char *name)
log_dbg("Allocating crypt device context by device %s.", name);
ci = crypt_status(NULL, name);
if (ci == INVALID)
return -ENODEV;
if (ci < ACTIVE) {
log_err(NULL, _("Device %s is not active.\n"), name);
return -ENODEV;
@@ -1211,20 +1224,26 @@ int crypt_suspend(struct crypt_device *cd,
return -EINVAL;
}
if (!cd && dm_init(NULL, 1) < 0)
return -ENOSYS;
r = dm_query_device(name, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, &suspended, NULL);
if (r < 0)
return r;
goto out;
if (suspended) {
log_err(cd, _("Volume %s is already suspended.\n"), name);
return -EINVAL;
r = -EINVAL;
goto out;
}
r = dm_suspend_and_wipe_key(name);
if (r)
log_err(cd, "Error during suspending device %s.\n", name);
out:
if (!cd)
dm_exit();
return r;
}
@@ -1724,20 +1743,33 @@ int crypt_activate_by_volume_key(struct crypt_device *cd,
int crypt_deactivate(struct crypt_device *cd, const char *name)
{
int r;
if (!name)
return -EINVAL;
log_dbg("Deactivating volume %s.", name);
if (!cd && dm_init(NULL, 1) < 0)
return -ENOSYS;
switch (crypt_status(cd, name)) {
case ACTIVE: return dm_remove_device(name, 0, 0);
case ACTIVE: r = dm_remove_device(name, 0, 0);
break;
case BUSY: log_err(cd, _("Device %s is busy."), name);
return -EBUSY;
r = -EBUSY;
break;
case INACTIVE: log_err(cd, _("Device %s is not active."), name);
return -ENODEV;
r = -ENODEV;
break;
default: log_err(cd, _("Invalid device %s."), name);
return -EINVAL;
r = -EINVAL;
}
if (!cd)
dm_exit();
return r;
}
// misc helper functions
@@ -1848,8 +1880,14 @@ crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
{
int r;
if (!cd && dm_init(NULL, 1) < 0)
return INVALID;
r = dm_status_device(name);
if (!cd)
dm_exit();
if (r < 0 && r != -ENODEV)
return INVALID;

View File

@@ -64,32 +64,31 @@ static struct action_type {
int (*handler)(int);
int arg;
int required_action_argc;
int required_dm_backend;
int required_memlock;
int show_status;
const char *arg_desc;
const char *desc;
} action_types[] = {
{ "create", action_create, 0, 2, 1, 1, 1, N_("<name> <device>"),N_("create device") },
{ "remove", action_remove, 0, 1, 1, 1, 1, N_("<name>"), N_("remove device") },
{ "resize", action_resize, 0, 1, 1, 1, 1, N_("<name>"), N_("resize active device") },
{ "status", action_status, 0, 1, 1, 0, 1, N_("<name>"), N_("show device status") },
{ "luksFormat", action_luksFormat, 0, 1, 1, 1, 1, N_("<device> [<new key file>]"), N_("formats a LUKS device") },
{ "luksOpen", action_luksOpen, 0, 2, 1, 1, 1, N_("<device> <name> "), N_("open LUKS device as mapping <name>") },
{ "luksAddKey", action_luksAddKey, 0, 1, 1, 1, 1, N_("<device> [<new key file>]"), N_("add key to LUKS device") },
{ "luksRemoveKey",action_luksRemoveKey, 0, 1, 1, 1, 1, N_("<device> [<key file>]"), N_("removes supplied key or key file from LUKS device") },
{ "luksKillSlot", action_luksKillSlot, 0, 2, 1, 1, 1, N_("<device> <key slot>"), N_("wipes key with number <key slot> from LUKS device") },
{ "luksUUID", action_luksUUID, 0, 1, 0, 0, 1, N_("<device>"), N_("print UUID of LUKS device") },
{ "isLuks", action_isLuks, 0, 1, 0, 0, 0, N_("<device>"), N_("tests <device> for LUKS partition header") },
{ "luksClose", action_remove, 0, 1, 1, 1, 1, N_("<name>"), N_("remove LUKS mapping") },
{ "luksDump", action_luksDump, 0, 1, 0, 0, 1, N_("<device>"), N_("dump LUKS partition information") },
{ "luksSuspend",action_luksSuspend, 0, 1, 1, 1, 1, N_("<device>"), N_("Suspend LUKS device and wipe key (all IOs are frozen).") },
{ "luksResume", action_luksResume, 0, 1, 1, 1, 1, N_("<device>"), N_("Resume suspended LUKS device.") },
{ "luksHeaderBackup",action_luksBackup, 0, 1, 1, 1, 1, N_("<device>"), N_("Backup LUKS device header and keyslots") },
{ "luksHeaderRestore",action_luksRestore,0,1, 1, 1, 1, N_("<device>"), N_("Restore LUKS device header and keyslots") },
{ "luksDelKey", action_luksDelKey, 0, 2, 1, 1, 1, N_("<device> <key slot>"), N_("identical to luksKillSlot - DEPRECATED - see man page") },
{ "reload", action_create, 1, 2, 1, 1, 1, N_("<name> <device>"), N_("modify active device - DEPRECATED - see man page") },
{ NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL }
{ "create", action_create, 0, 2, 1, 1, N_("<name> <device>"),N_("create device") },
{ "remove", action_remove, 0, 1, 1, 1, N_("<name>"), N_("remove device") },
{ "resize", action_resize, 0, 1, 1, 1, N_("<name>"), N_("resize active device") },
{ "status", action_status, 0, 1, 0, 1, N_("<name>"), N_("show device status") },
{ "luksFormat", action_luksFormat, 0, 1, 1, 1, N_("<device> [<new key file>]"), N_("formats a LUKS device") },
{ "luksOpen", action_luksOpen, 0, 2, 1, 1, N_("<device> <name> "), N_("open LUKS device as mapping <name>") },
{ "luksAddKey", action_luksAddKey, 0, 1, 1, 1, N_("<device> [<new key file>]"), N_("add key to LUKS device") },
{ "luksRemoveKey",action_luksRemoveKey, 0, 1, 1, 1, N_("<device> [<key file>]"), N_("removes supplied key or key file from LUKS device") },
{ "luksKillSlot", action_luksKillSlot, 0, 2, 1, 1, N_("<device> <key slot>"), N_("wipes key with number <key slot> from LUKS device") },
{ "luksUUID", action_luksUUID, 0, 1, 0, 1, N_("<device>"), N_("print UUID of LUKS device") },
{ "isLuks", action_isLuks, 0, 1, 0, 0, N_("<device>"), N_("tests <device> for LUKS partition header") },
{ "luksClose", action_remove, 0, 1, 1, 1, N_("<name>"), N_("remove LUKS mapping") },
{ "luksDump", action_luksDump, 0, 1, 0, 1, N_("<device>"), N_("dump LUKS partition information") },
{ "luksSuspend",action_luksSuspend, 0, 1, 1, 1, N_("<device>"), N_("Suspend LUKS device and wipe key (all IOs are frozen).") },
{ "luksResume", action_luksResume, 0, 1, 1, 1, N_("<device>"), N_("Resume suspended LUKS device.") },
{ "luksHeaderBackup",action_luksBackup, 0, 1, 1, 1, N_("<device>"), N_("Backup LUKS device header and keyslots") },
{ "luksHeaderRestore",action_luksRestore,0,1, 1, 1, N_("<device>"), N_("Restore LUKS device header and keyslots") },
{ "luksDelKey", action_luksDelKey, 0, 2, 1, 1, N_("<device> <key slot>"), N_("identical to luksKillSlot - DEPRECATED - see man page") },
{ "reload", action_create, 1, 2, 1, 1, N_("<name> <device>"), N_("modify active device - DEPRECATED - see man page") },
{ NULL, NULL, 0, 0, 0, 0, NULL, NULL }
};
static void clogger(struct crypt_device *cd, int class, const char *file,