~nabijaczleweli/klapki

ref: bfaaf3b0ee87d924825987cdd6449a38d6d9627e klapki/src/config.cpp -rw-r--r-- 5.2 KiB
bfaaf3b0наб Sort our boot entries. Handle KLAPKI_EFI_ROOT better maybe? 1 year, 30 days ago
                                                                                
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
// The MIT License (MIT)

// Copyright (c) 2020 наб <nabijaczleweli@nabijaczleweli.xyz>

// 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 "config.hpp"
#include "util.hpp"
#include <cstdlib>
#include <fmt/format.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <unistd.h>
extern "C" {
#include <efivar/efivar.h>
}


#define TRY(...)                                       \
	({                                                   \
		auto ret = __VA_ARGS__;                            \
		if(auto err = std::get_if<std::string>(&ret); err) \
			return std::move(*err);                          \
		std::move(std::get<0>(ret));                       \
	})


static std::string get_host() {
	if(auto host = std::getenv("KLAPKI_HOST"))
		return host;
	else {
		auto mid = klapki::readline("/etc/machine-id");
		if(!mid.empty())
			return mid;

		char hostname[HOST_NAME_MAX + 1];
		gethostname(hostname, sizeof(hostname) / sizeof(*hostname));
		return hostname;
	}
}

static std::variant<std::string_view, std::string> get_wisom_root() {
	if(auto host = std::getenv("KLAPKI_WISDOM")) {
		std::string ret{host};
		if(!ret.empty() && ret.back() != '/')
			ret += '/';
		return ret;
	} else
		return std::string_view{"/etc/klapki/"};
}

static std::variant<std::string_view, std::string> find_esp() {
	const char * esp = "/boot/efi";

	struct stat boot;
	if(stat("/boot", &boot) < 0)
		return fmt::format("find_esp(): stat(\"/boot\"): {}", strerror(errno));

	struct stat boot_efi;
	if(stat("/boot/efi", &boot_efi) < 0) {
		if(errno != ENOENT)
			return fmt::format("find_esp(): stat(\"/boot/efi\"): {}", strerror(errno));
		else {
			esp      = "/boot";
			boot_efi = boot;
			if(stat("/", &boot) < 0)
				return fmt::format("find_esp(): stat(\"/\"): {}", strerror(errno));
		}
	}

	if(boot.st_dev == boot_efi.st_dev)
		return fmt::format("find_esp(): {}: not a mount point?", esp);

	return std::string_view{esp};
}


std::string klapki::config::news_efi_dir() const {
	std::string broot;
	std::string_view vroot = "klapki";
	if(auto root = std::getenv("KLAPKI_EFI_ROOT")) {
		while(*root == '\\' || *root == '/')
			++root;
		broot = root;
		std::transform(std::begin(broot), std::end(broot), std::begin(broot), [](auto c) { return c == '/' ? '\\' : c; });
		while(!broot.empty() && broot.back() == '\\')
			broot.pop_back();
		vroot = broot;
	}
	return fmt::format("\\{}{}{}\\", vroot, vroot.empty() ? "" : "\\", this->host);
}


std::variant<klapki::config, std::string> klapki::config::read(const char ** argv) {
	const auto argv0 = argv[0];
	++argv;

	bool verbose = false;
	bool commit  = true;
	for(; *argv && argv[0][0] == '-'; ++argv) {
		for(auto opts = argv[0] + 1; *opts; ++opts)
			switch(opts[0]) {
				case 'v':
					verbose = true;
					break;

				case 'E':
					efi_set_verbose(efi_get_verbose() + 1, nullptr);
					break;

				case 'n':
					commit = false;
					break;

				case 'h':
					return fmt::format("klapki {}\n"
					                   "Usage: {} [-vEnh]… [op [arg…]]…\n"
					                   "\n"
					                   "Flags:\n"
					                   "  -v\tVerbose operation\n"
					                   "  -E\tIncrease libefivar verbosity level\n"
					                   "  -n\tDon't commit\n"
					                   "  -h\tShow this help\n"
					                   "\n"
					                   "Recognised ops:\n"
					                   "\tdump\n"
					                   "\tbootpos <position>\n"
					                   "\taddkernel <version> <image> [initrd…] <\"\">\n"
					                   "\tdelkernel <version>\n"
					                   "\taddvariant <variant>\n"
					                   "\tdelvariant <variant>",
					                   KLAPKI_VERSION, argv0);

				default:
					return fmt::format("{}: unknown flag {}", argv0, opts[0]);
			}
	}

	std::vector<ops::op_t> ops;
	if(verbose)
		ops.emplace_back(ops::dump{});
	while(*argv) {
		auto pop = op::from_cmdline(argv0, argv);
		if(auto err = std::get_if<std::string>(&pop))
			return std::move(*err);
		else {
			ops.emplace_back(std::move(std::get<ops::op_t>(pop)));
			if(verbose)
				ops.emplace_back(ops::dump{});
		}
	}

	return config{argv0, get_host(), verbose, commit, TRY(find_esp()), std::move(ops), get_wisom_root()};
}