summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2023-04-01 14:33:23 +0200
committerGitHub <noreply@github.com>2023-04-01 14:33:23 +0200
commit407566527dd65d67b6fd29a8103ad87901f18c75 (patch)
tree8ec7275ec3212b40c1e673abdef5db47e24bc4e3 /src
parent7f72dca7620fd4cbd50b483d419f074b912595ec (diff)
parent05eb2c60bd7308550cbbfe463c953d60be5f4375 (diff)
downloadsystemd-407566527dd65d67b6fd29a8103ad87901f18c75.tar.gz
Merge pull request #25836 from DaanDeMeyer/reset
firstboot: Add --reset option
Diffstat (limited to 'src')
-rw-r--r--src/firstboot/firstboot.c62
1 files changed, 58 insertions, 4 deletions
diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c
index ba7e08913f..3e68ed1cb0 100644
--- a/src/firstboot/firstboot.c
+++ b/src/firstboot/firstboot.c
@@ -72,6 +72,7 @@ static bool arg_force = false;
static bool arg_delete_root_password = false;
static bool arg_root_password_is_hashed = false;
static bool arg_welcome = true;
+static bool arg_reset = false;
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
@@ -334,7 +335,7 @@ static int process_locale(int rfd) {
assert(rfd >= 0);
pfd = chase_and_open_parent_at(rfd, "/etc/locale.conf",
- CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN,
+ CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
&f);
if (pfd < 0)
return log_error_errno(pfd, "Failed to chase /etc/locale.conf: %m");
@@ -423,7 +424,7 @@ static int process_keymap(int rfd) {
assert(rfd >= 0);
pfd = chase_and_open_parent_at(rfd, "/etc/vconsole.conf",
- CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN,
+ CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
&f);
if (pfd < 0)
return log_error_errno(pfd, "Failed to chase /etc/vconsole.conf: %m");
@@ -647,7 +648,7 @@ static int process_machine_id(int rfd) {
assert(rfd >= 0);
pfd = chase_and_open_parent_at(rfd, "/etc/machine-id",
- CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN,
+ CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
&f);
if (pfd < 0)
return log_error_errno(pfd, "Failed to chase /etc/machine-id: %m");
@@ -1064,7 +1065,7 @@ static int process_kernel_cmdline(int rfd) {
assert(rfd >= 0);
pfd = chase_and_open_parent_at(rfd, "/etc/kernel/cmdline",
- CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN,
+ CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
&f);
if (pfd < 0)
return log_error_errno(pfd, "Failed to chase /etc/kernel/cmdline: %m");
@@ -1089,6 +1090,48 @@ static int process_kernel_cmdline(int rfd) {
return 0;
}
+static int reset_one(int rfd, const char *path) {
+ _cleanup_close_ int pfd = -EBADF;
+ _cleanup_free_ char *f = NULL;
+
+ assert(rfd >= 0);
+ assert(path);
+
+ pfd = chase_and_open_parent_at(rfd, path, CHASE_AT_RESOLVE_IN_ROOT|CHASE_WARN|CHASE_NOFOLLOW, &f);
+ if (pfd == -ENOENT)
+ return 0;
+ if (pfd < 0)
+ return log_error_errno(pfd, "Failed to resolve %s: %m", path);
+
+ if (unlinkat(pfd, f, 0) < 0)
+ return errno == ENOENT ? 0 : log_error_errno(errno, "Failed to remove %s: %m", path);
+
+ log_info("Removed %s", path);
+ return 0;
+}
+
+static int process_reset(int rfd) {
+ int r;
+
+ assert(rfd >= 0);
+
+ if (!arg_reset)
+ return 0;
+
+ FOREACH_STRING(p,
+ "/etc/locale.conf",
+ "/etc/vconsole.conf",
+ "/etc/hostname",
+ "/etc/machine-id",
+ "/etc/kernel/cmdline") {
+ r = reset_one(rfd, p);
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
static int help(void) {
_cleanup_free_ char *link = NULL;
int r;
@@ -1130,6 +1173,7 @@ static int help(void) {
" --force Overwrite existing files\n"
" --delete-root-password Delete root password\n"
" --welcome=no Disable the welcome text\n"
+ " --reset Remove existing files\n"
"\nSee the %s for details.\n",
program_invocation_short_name,
link);
@@ -1171,6 +1215,7 @@ static int parse_argv(int argc, char *argv[]) {
ARG_FORCE,
ARG_DELETE_ROOT_PASSWORD,
ARG_WELCOME,
+ ARG_RESET,
};
static const struct option options[] = {
@@ -1206,6 +1251,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "force", no_argument, NULL, ARG_FORCE },
{ "delete-root-password", no_argument, NULL, ARG_DELETE_ROOT_PASSWORD },
{ "welcome", required_argument, NULL, ARG_WELCOME },
+ { "reset", no_argument, NULL, ARG_RESET },
{}
};
@@ -1408,6 +1454,10 @@ static int parse_argv(int argc, char *argv[]) {
arg_welcome = r;
break;
+ case ARG_RESET:
+ arg_reset = true;
+ break;
+
case '?':
return -EINVAL;
@@ -1497,6 +1547,10 @@ static int run(int argc, char *argv[]) {
return r;
}
+ r = process_reset(rfd);
+ if (r < 0)
+ return r;
+
r = process_locale(rfd);
if (r < 0)
return r;