-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMount_class.cpp
More file actions
81 lines (58 loc) · 2 KB
/
Mount_class.cpp
File metadata and controls
81 lines (58 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include <sys/mount.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstring>
#include <cerrno>
#include <linux/loop.h>
using namespace std;
class MountLinux
{
public:
MountLinux(Logger& l) : logger(l)
{
}
void umount_ext(const char* mountPoint, const char* imageFile, const char* loopDevice)
{
if (umount(mountPoint) == -1) {
logger.log(Logger::Level::ERR, "Error unmounting: " + *mountPoint + *strerror(errno));
return;
}
}
void mount_ext(const char* mountPoint, const char* imageFile, const char* loopDevice)
{
if (mkdir(mountPoint, 0755) && errno != EEXIST) {
logger.log(Logger::Level::ERR, "Error creating mount point: " + *strerror(errno));
}
int loopFd = open("/dev/loop0", O_RDWR);
if (loopFd < 0) {
logger.log(Logger::Level::ERR, "Error opening loop device: " + *strerror(errno));
}
// Открытие образа
int imgFd = open(imageFile, O_RDONLY);
if (imgFd < 0) {
logger.log(Logger::Level::ERR, "Error opening image file: " + *strerror(errno));
close(loopFd);
return;
}
char command[256];
snprintf(command, sizeof(command), "losetup %s %s", loopDevice, imageFile);
// Связывание устройста с образом
int result = system(command);
if (result == -1) {
logger.log(Logger::Level::ERR, "Error executing losetup: " + *strerror(errno));
} else {
cout << "Successfully set up " << imageFile << " on " << loopDevice << endl;
}
// Монтирование
if (mount("/dev/loop0", mountPoint, "ext4", 0, nullptr) == -1) {
logger.log(Logger::Level::ERR, "Error mounting image: " + *strerror(errno));
}
close(imgFd);
ioctl(loopFd, LOOP_CLR_FD);
close(loopFd);
}
private:
Logger& logger;
};