forked from pantavisor/pantavisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmount.c
203 lines (179 loc) · 6.04 KB
/
mount.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright (c) 2020-2022 Pantacor Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/limits.h>
#include "mount.h"
#include "init.h"
#include "blkid.h"
#include "utils/tsh.h"
#include "pantavisor.h"
#include "utils/fs.h"
#include "loop.h"
#include "blkid.h"
#include "utils/str.h"
#include "paths.h"
#define MODULE_NAME "mount-init"
#define pv_log(level, msg, ...) vlog(MODULE_NAME, level, msg, ##__VA_ARGS__)
#include "log.h"
static int ph_mount_init(struct pv_init *this)
{
struct stat st;
char storage_path[PATH_MAX], pv_path[PATH_MAX];
// Make pantavisor control area
pv_paths_pv_file(pv_path, PATH_MAX, "");
if (stat(pv_path, &st) != 0)
pv_fs_mkdir_p(pv_path, 0500);
// Logs
pv_paths_storage_log(storage_path, PATH_MAX);
if (stat(storage_path, &st) != 0)
pv_fs_mkdir_p(storage_path, 0500);
// Dropbear
pv_paths_storage_dropbear(storage_path, PATH_MAX);
if (stat(storage_path, &st) != 0)
pv_fs_mkdir_p(storage_path, 0500);
pv_paths_etc_file(pv_path, PATH_MAX, DROPBEAR_DNAME);
if (stat(pv_path, &st) != 0)
pv_fs_mkdir_p(pv_path, 0755);
mount_bind(storage_path, pv_path);
return 0;
}
void pv_mount_umount(void)
{
char path[PATH_MAX];
pv_paths_etc_file(path, PATH_MAX, DROPBEAR_DNAME);
if (umount(path))
pv_log(ERROR, "Error unmounting etc_file %s", strerror(errno));
if (pv_config_get_str(PV_STORAGE_LOGTEMPSIZE)) {
pv_paths_storage(path, PATH_MAX);
size_t logmount_size = strlen(path) + strlen("/logs ");
char *logmount = malloc(sizeof(char) * logmount_size);
SNPRINTF_WTRUNC(logmount, logmount_size, "%s%s", path, "/logs");
if (umount(logmount))
pv_log(ERROR, "Error unmounting logmount: %s / %s",
logmount, strerror(errno));
free(logmount);
}
}
static int pv_mount_init(struct pv_init *this)
{
struct stat st;
struct blkid_info dev_info = { 0 };
char path[PATH_MAX];
int ret = -1;
if (pv_config_get_system_init_mode() == IM_APPENGINE)
return 0;
// Create storage mountpoint and mount device
pv_paths_storage(path, PATH_MAX);
pv_fs_mkdir_p(path, 0755);
blkid_init(&dev_info);
/*
* Check that storage device has been enumerated and wait if not there yet
* (RPi2 for example is too slow to pvan the MMC devices in time)
*/
for (int wait = pv_config_get_int(PV_STORAGE_WAIT); wait > 0; wait--) {
/*
* storage.path will contain UUID=XXXX or LABEL=XXXX
* */
const char *storage_path = pv_config_get_str(PV_STORAGE_DEVICE);
if (get_blkid(&dev_info, storage_path))
pv_log(ERROR, "cannot get block device from '%s'",
storage_path);
if (dev_info.device && stat(dev_info.device, &st) == 0)
break;
pv_log(INFO,
"trail storage not yet available. Waiting %d seconds...",
wait);
sleep(1);
continue;
}
if (!dev_info.device) {
pv_log(FATAL, "could not mount '%s': %s", dev_info.device,
strerror(errno));
exit_error(errno, NULL);
}
pv_log(INFO, "trail storage found: '%s'", dev_info.device);
// attempt auto resize only if we have ext4 and in embedded init mode
if ((pv_config_get_system_init_mode() == IM_EMBEDDED) &&
!strcmp(pv_config_get_str(PV_STORAGE_FSTYPE), "ext4")) {
size_t run_size = strlen("/lib/pv/pv_e2fsgrow") +
strlen(dev_info.device) + 3;
char *run = malloc(sizeof(char) * run_size);
SNPRINTF_WTRUNC(run, run_size, "/lib/pv/pv_e2fsgrow %s",
dev_info.device);
tsh_run(run, 1, NULL);
free(run);
}
const char *mnttype = pv_config_get_str(PV_STORAGE_MNTTYPE);
const char *logtempsize = pv_config_get_str(PV_STORAGE_LOGTEMPSIZE);
if (!mnttype) {
ret = mount(dev_info.device, path,
pv_config_get_str(PV_STORAGE_FSTYPE), 0, NULL);
if (ret < 0)
goto out;
} else {
int status;
size_t mntcmd_size = strlen("/btools/pvmnt.%s %s") +
strlen(mnttype) + strlen(path) + 1;
char *mntcmd = calloc(mntcmd_size, sizeof(char));
if (!mntcmd) {
pv_log(FATAL, "couldn't allocate mount command");
goto out;
}
SNPRINTF_WTRUNC(mntcmd, mntcmd_size, "/btools/pvmnt.%s %s",
mnttype, path);
pv_log(DEBUG, "mounting through helper: %s\n", mntcmd);
ret = tsh_run(mntcmd, 1, &status);
free(mntcmd);
}
free_blkid_info(&dev_info); /*Keep if device_info is required later.*/
if (logtempsize) {
size_t logmount_size = strlen(path) + strlen("/logs ");
char *logmount = malloc(sizeof(char) * logmount_size);
size_t opts_size = strlen(logtempsize) + strlen("size=%s") + 1;
char *opts = malloc(sizeof(char) * opts_size);
SNPRINTF_WTRUNC(opts, opts_size, "size=%s", logtempsize);
SNPRINTF_WTRUNC(logmount, logmount_size, "%s%s", path, "/logs");
pv_fs_mkdir_p(logmount, 0755);
pv_log(DEBUG, "mounting tmpfs logmount: %s with opts: %s",
logmount, opts);
ret = mount("none", logmount, "tmpfs", 0, opts);
free(logmount);
if (opts)
free(opts);
}
out:
if (ret < 0)
exit_error(errno, "Could not mount trails storage");
return 0;
}
struct pv_init pv_init_mount = {
.init_fn = pv_mount_init,
.flags = 0,
};
struct pv_init ph_init_mount = {
.init_fn = ph_mount_init,
.flags = 0,
};