-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop.c
145 lines (111 loc) · 2.73 KB
/
loop.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#define _GNU_SOURCE // mandatory in order to compile successfully
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/mount.h>
#include <errno.h>
#include <utils.h>
#include "loop.h"
#include "logger.h"
#include "context.h"
static int getFreeLoop(char* buffer,int size)
{
FILE *fp;
int length;
logger_enter(__func__,NAFILE);
memset(buffer,0,size);
logger_log(__func__,NAFILE,DEBUG,"Getting free loop device");
fp = popen("losetup -f", "r");
if (fp == NULL)
{
logger_errno(__func__,NAFILE,"Failed to get free loop device");
return -1;
}
if (fgets(buffer, size-1, fp)==NULL)
{
logger_errno(__func__,NAFILE,"Failed to get free loop device");
fclose(fp);
return -1;
}
pclose(fp);
length=strlen(buffer);
if (length>0) buffer[length-1]='\0';
logger_log(__func__,NAFILE,DEBUG,"Found free loop device: %s",buffer);
return 0;
}
static int loop_bind(Loop* loop)
{
char command[1024];
logger_enter(__func__,NAFILE);
strcpy(command,"losetup ");
strcat(command,loop->name);
strcat(command," ");
strcat(command,loop->fileName);
logger_log(__func__,NAFILE,DEBUG,"%s",command);
return system(command);
}
static int loop_unbind(Loop* loop)
{
char command[1024];
logger_enter(__func__,NAFILE);
strcpy(command,"losetup -d ");
strcat(command,loop->name);
return system(command);
}
Loop* loop_create(const char* fileName,const char* mountPoint)
{
Loop* loop;
logger_enter(__func__,NAFILE);
loop=malloc(sizeof(Loop));
loop->fileName=fileName;
loop->mountPoint=mountPoint;
logger_log(__func__,NAFILE,DEBUG,"Creating loop device");
if (getFreeLoop(loop->name,1024)!=0)
{
logger_log(__func__,NAFILE,ERROR,"Failed to create loop device");
free(loop);
return NULL;
}
if (loop_bind(loop)!=0)
{
logger_log(__func__,NAFILE,ERROR,"Failed to bind loop device");
free(loop);
return NULL;
}
return loop;
}
int loop_mount(Loop* loop)
{
logger_enter(__func__,NAFILE);
/*logger_log(__func__,NAFILE,DEBUG,"Unshare filesystem namespace");
if (unshare(CLONE_NEWNS | CLONE_FS) != 0)
{
logger_errno(__func__,NAFILE,"Failed to unshare filesystem namespace");
return -1;
}//*/
logger_log(__func__,NAFILE,DEBUG,"Mounting loop device");
/*if (mount("none", "/", NULL, MS_REC|MS_PRIVATE, NULL) != 0)
{
logger_ernno(__func__,NAFILE,ERROR,"Failed to mount loop device");
return -1;
}*/
if (mount(loop->name, loop->mountPoint, "ext4",0,NULL)!= 0)
{
logger_errno(__func__,NAFILE,"Failed to mount loop device");
return -1;
}
return 0;
}
int loop_umount(Loop* loop)
{
logger_enter(__func__,NAFILE);
return umount(loop->mountPoint);
}
void loop_free(Loop* loop)
{
logger_enter(__func__,NAFILE);
loop_unbind(loop);
free(loop);
}