-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdfbeadm.c
More file actions
256 lines (239 loc) · 7.15 KB
/
Copy pathdfbeadm.c
File metadata and controls
256 lines (239 loc) · 7.15 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright (c) 2017-2018, Exile Heavy Industries
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted (subject to the limitations in the disclaimer
* below) provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific
* prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
* LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
/*
* This is meant to be a utility similar to beadm(1) on FreeBSD/Illumos
* however, I'd rather have it written in C than have a massive shell script.
* So this should be an interesting exercise in getting familiar with hammer2's
* snapshot feature and managing important system files.
*
* Provided this project actually bears any results, DragonFly BSD development
* can proceed even faster as both vkernels and boot environments would
* make testing new builds almost completely painless
*/
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
/* holds prototypes */
#ifndef DFBEADM_MAIN_H
#include "dfbeadm.h"
#endif
/* collect the filesystems into a struct buffer */
#ifndef DFBEADM_FSCOLLECT_H
#include "fscollect.h"
#endif
/* functions to destroy boot environments */
#ifndef DFBEADM_FSDESTROY_H
#include "fsdestroy.h"
#endif
/* functions to get the available environments */
#ifndef DFBEADM_DF_LIST_H
#include "fslist.h"
#endif
/* functions to test filesystems */
#ifndef DFBEADM_H2TEST_H
#include "fstest.h"
#endif
/* generate and install the new fstab */
#ifndef DFBEADM_FSUP_H
#include "fsupdate.h"
#endif
/* make the snapshots */
#ifndef DFBEADM_SNAPFS_H
#include "snapfs.h"
#endif
/* envtest return code mnemonics */
#define LISTBENV 0x04
#define CREATEBE 0x08
#define ACTIVATE 0x10
/* environment check results */
/* currently limited to just UID checking */
#define ENV_OK 0
#define E_BADUSER 1
extern char *__progname;
extern char **environ;
extern bool dbg;
extern bool noop;
/*
* TODO: Rework and possibly expand for more robust option parsing
* ----------------------
* exflags layout
* ----------------------
* 0 0 0 0 0 0 0 0
* | | | | | | | |
* | | | | | | | \- verbosity flag
* | | | | | | \- verbosity flag
* | | | | | \- list
* | | | | \- create
* | | | \- activate
* | | \- delete
* | \- reserved
* \- reserved
*/
static void usage(void);
/* This is where the actual logic processing should take place */
int cook(uint8_t *flags, char *bestring);
int envtest(void);
#ifdef DEBUG
bool dbg = true; /* Force enable for debug builds */
#else
bool dbg = false; /* Default to not adding runtime traces */
#endif
bool noop = false;
/*
* TODO: Remove all but the most rudimentary logic from this function, instead
* pass the important data down to cook(), which will then determine how best to proceed
*/
int
main(int argc, char **argv) {
/* a bitmap flag value to pass to other functions */
uint8_t exflags;
int ch, ret;
char belabel[MNAMELEN];
exflags = 0;
ret = ch = 0;
/* bail early */
if ( argc == 1 ) { usage(); }
while((ch = getopt(argc,argv,"a:c:d:hlnrD")) != -1) {
switch(ch) {
case 'a':
/* this codepath is not yet ready for use */
/* exflags |= ACTIVATE;
strlcpy(belabel,optarg,(MNAMELEN-1));
break;
*/
NOTIMP(ch);
return(ret);
case 'c':
exflags |= CREATEBE;
strlcpy(belabel,optarg,(MNAMELEN-1));
break;
/*
* TODO: Add a config file to read, so users can specify which filesystems they actually want managed
* use either '-f' or '-C'
*/
case 'd':
NOTIMP(ch);
return(ret);
case 'D':
dbg = true;
break;
case 'h':
usage();
case 'l':
/* This will clear other flags */
exflags |= LISTBENV;
exflags &= LISTBENV;
break;
case 'n':
/*
* This is the NO-OP flag, it will mostly be useful during the debugging
* process, but also will allow users to get additional information regarding what
* could happen, especially with increased verbosity
*/
noop = true;
break;
case 'r':
NOTIMP(ch);
return(ret);
default:
usage();
}
}
/* Pass all the serious logic into cook() */
argc -= optind;
argv += optind;
ret = cook(&exflags, belabel);
return(ret);
}
int
cook(uint8_t *flags, char *bestring) {
int retc;
retc = 0;
if ((retc = envtest()) != 0) {
return(retc);
}
/* Placeholder logic to quelch compiler warnings */
assert(flags != NULL);
switch(*flags) {
case(ACTIVATE):
assert(bestring != NULL);
retc = activate(bestring);
break;
case(CREATEBE):
assert(bestring != NULL);
retc = create(bestring);
break;
case(LISTBENV):
list();
break;
default:
usage();
break;
}
return(retc);
}
int
envtest(void) {
int retc;
retc = ENV_OK;
retc = (geteuid() == (uid_t)0) ? ENV_OK : E_BADUSER;
if (retc != ENV_OK) {
fprintf(stderr,"ERR: %s [%s:%u] %s: You must run this tool as root (uid=0) or with sudo/doas! (Current EUID: %u)\n",
__progname,__FILE__,__LINE__,__func__,geteuid());
}
return(retc);
}
/*
* tell the user how this program works
*/
static void __attribute__((noreturn))
usage(void) {
fprintf(stderr,"%s: Utility to create HAMMER2 boot environments.\n",__progname);
fprintf(stderr,"WARNING: This version (%s) of %s is not yet completed, only basic functionality exists!\n",DFBEADM_VER_STRING,__progname);
fprintf(stderr,"Usage:\n"
" -a Activate the given boot environment\n"
" -c Create a new boot environment with the given label\n"
" -d Destroy the given boot environment\n"
" -D Print debugging information during execution\n"
" -h This help text\n"
" -l List existing boot environments\n"
" -n No-op/dry run, only show what would be done\n"
" -r Remove the given boot environment\n");
_exit(0);
}