#include <fcntl.h>
#include <errno.h>
-
#include <klay/error.h>
#include <klay/exception.h>
#include <klay/filesystem.h>
#include "../../ext4-tool.h"
#include "dmcrypt-engine.h"
+#include "misc.h"
#define DM_MAX_BUFFER_SIZE 4096
#define DM_KEY_MIN_LEN_BYTE 32
}
}
+std::string getCryptoBlkDevName(const std::string &mountName)
+{
+ char dmBuf[DM_MAX_BUFFER_SIZE];
+ int fd;
+
+ // Open dm control IOCTL
+ if ((fd = open("/dev/mapper/control", O_RDWR)) < 0) {
+ throw runtime::Exception("Cannot open device-mapper");
+ }
+
+ auto dmIo = (struct dm_ioctl *)dmBuf;
+
+ // Get the device status, in particular, the mount_name of it's device file
+ initDMIoctl(dmBuf, DM_MAX_BUFFER_SIZE, mountName, 0);
+ int ret = ioctl(fd, DM_DEV_STATUS, dmBuf);
+ close(fd);
+
+ // No such device
+ if (ret)
+ return std::string();
+
+ // Not opened
+ if (dmIo->open_count == 0)
+ return std::string();
+
+ // Get the device name
+ unsigned int dmMinor = (dmIo->dev & 0xff) | ((dmIo->dev >> 12) & 0xfff00);
+ return std::string("/dev/dm-") + std::to_string(dmMinor);
+}
+
BinaryData sanitizeKey(const BinaryData &key)
{
if (key.size() < DM_KEY_MIN_LEN_BYTE)
} // namepsace
DMCryptEngine::DMCryptEngine(const std::string &src, const std::string &dest, const ProgressBar &prgsBar) :
- source(src), destination(dest), progress(prgsBar), mounted(false)
+ source(src), destination(dest), progress(prgsBar)
{
}
if (::mount(cryptoBlkDev.c_str(), destination.c_str(), "ext4", 0, 0) < 0)
throw runtime::Exception(runtime::GetSystemErrorMessage());
-
- mounted = true;
}
void DMCryptEngine::umount()
throw runtime::Exception(runtime::GetSystemErrorMessage());
destroyCryptoBlkDev(DM_DEFAULT_LABEL_NAME);
-
- mounted = false;
}
bool DMCryptEngine::isMounted()
{
- return mounted;
+ // TODO isMounted() vs. isOpened()
+ std::string cryptoDev = getCryptoBlkDevName(DM_DEFAULT_LABEL_NAME);
+ if (cryptoDev.empty())
+ return false;
+
+ Mtab mtab;
+ struct ::mntent* entry;
+ while ((entry = mtab.next()) != NULL) {
+ if (cryptoDev == entry->mnt_fsname && destination == entry->mnt_dir)
+ return true;
+ }
+
+ return false;
}
void DMCryptEngine::encrypt(const BinaryData &key, unsigned int options)