Switch to using a systemd generator for /var
If one wants to set up a mount for `/var` in `/etc/fstab`, it won't be mounted since `ostree-prepare-root` set up a bind mount for `/var` to `/sysroot/ostree/$stateroot/var`, and systemd will take the already extant mount over what's in `/etc/fstab`. There are a few options to fix this, but what I settled on is parsing `/etc/fstab` in a generator (exactly like `systemd-fstab-generator` does), except here we look for an explicit mount for `/var`, and if one *isn't* found, synthesize the default ostree mount to the stateroot. Another nice property is that if an admin creates a `var.mount` unit in `/etc` for example, that will also override our mount. Note that today ostree doesn't hard depend on systemd, so this behavior only kicks in if we're built with systemd *and* libmount support (for parsing `/etc/fstab`). I didn't really test that case though. Initially I started writing this as a "pure libc" program, but at one point decided to use `libostree.so` to find the booted deployment. That didn't work out because `/boot` wasn't necessarily mounted and hence we couldn't find the bootloader config. A leftover artifact from this is that the generator code calls into libostree via the "cmd private" infrastructure. But it's an easy way to share code, and doesn't hurt. Closes: #859 Approved by: jlebon
This commit is contained in:
1 parent
d815ba2a81
commit
30705889cb
10 files changed
+390
-65
No files matched your search
@@ -25,6 +25,10 @@
|
||||
#include <err.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/statvfs.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
static inline int
|
||||
path_is_on_readonly_fs (char *path)
|
||||
@@ -37,4 +41,66 @@ path_is_on_readonly_fs (char *path)
|
||||
return (stvfsbuf.f_flag & ST_RDONLY) != 0;
|
||||
}
|
||||
|
||||
static inline char *
|
||||
read_proc_cmdline (void)
|
||||
{
|
||||
FILE *f = fopen("/proc/cmdline", "r");
|
||||
char *cmdline = NULL;
|
||||
size_t len;
|
||||
|
||||
if (!f)
|
||||
goto out;
|
||||
|
||||
/* Note that /proc/cmdline will not end in a newline, so getline
|
||||
* will fail unelss we provide a length.
|
||||
*/
|
||||
if (getline (&cmdline, &len, f) < 0)
|
||||
goto out;
|
||||
/* ... but the length will be the size of the malloc buffer, not
|
||||
* strlen(). Fix that.
|
||||
*/
|
||||
len = strlen (cmdline);
|
||||
|
||||
if (cmdline[len-1] == '\n')
|
||||
cmdline[len-1] = '\0';
|
||||
out:
|
||||
if (f)
|
||||
fclose (f);
|
||||
return cmdline;
|
||||
}
|
||||
|
||||
static inline char *
|
||||
read_proc_cmdline_ostree (void)
|
||||
{
|
||||
char *cmdline = NULL;
|
||||
const char *iter;
|
||||
char *ret = NULL;
|
||||
|
||||
cmdline = read_proc_cmdline ();
|
||||
if (!cmdline)
|
||||
err (EXIT_FAILURE, "failed to read /proc/cmdline");
|
||||
|
||||
iter = cmdline;
|
||||
while (iter != NULL)
|
||||
{
|
||||
const char *next = strchr (iter, ' ');
|
||||
const char *next_nonspc = next;
|
||||
while (next_nonspc && *next_nonspc == ' ')
|
||||
next_nonspc += 1;
|
||||
if (strncmp (iter, "ostree=", strlen ("ostree=")) == 0)
|
||||
{
|
||||
const char *start = iter + strlen ("ostree=");
|
||||
if (next)
|
||||
ret = strndup (start, next - start);
|
||||
else
|
||||
ret = strdup (start);
|
||||
break;
|
||||
}
|
||||
iter = next_nonspc;
|
||||
}
|
||||
|
||||
free (cmdline);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* __OSTREE_MOUNT_UTIL_H_ */
|
||||
@@ -46,68 +46,6 @@
|
||||
|
||||
#include "ostree-mount-util.h"
|
||||
|
||||
static char *
|
||||
read_proc_cmdline (void)
|
||||
{
|
||||
FILE *f = fopen("/proc/cmdline", "r");
|
||||
char *cmdline = NULL;
|
||||
size_t len;
|
||||
|
||||
if (!f)
|
||||
goto out;
|
||||
|
||||
/* Note that /proc/cmdline will not end in a newline, so getline
|
||||
* will fail unelss we provide a length.
|
||||
*/
|
||||
if (getline (&cmdline, &len, f) < 0)
|
||||
goto out;
|
||||
/* ... but the length will be the size of the malloc buffer, not
|
||||
* strlen(). Fix that.
|
||||
*/
|
||||
len = strlen (cmdline);
|
||||
|
||||
if (cmdline[len-1] == '\n')
|
||||
cmdline[len-1] = '\0';
|
||||
out:
|
||||
if (f)
|
||||
fclose (f);
|
||||
return cmdline;
|
||||
}
|
||||
|
||||
static char *
|
||||
parse_ostree_cmdline (void)
|
||||
{
|
||||
char *cmdline = NULL;
|
||||
const char *iter;
|
||||
char *ret = NULL;
|
||||
|
||||
cmdline = read_proc_cmdline ();
|
||||
if (!cmdline)
|
||||
err (EXIT_FAILURE, "failed to read /proc/cmdline");
|
||||
|
||||
iter = cmdline;
|
||||
while (iter != NULL)
|
||||
{
|
||||
const char *next = strchr (iter, ' ');
|
||||
const char *next_nonspc = next;
|
||||
while (next_nonspc && *next_nonspc == ' ')
|
||||
next_nonspc += 1;
|
||||
if (strncmp (iter, "ostree=", strlen ("ostree=")) == 0)
|
||||
{
|
||||
const char *start = iter + strlen ("ostree=");
|
||||
if (next)
|
||||
ret = strndup (start, next - start);
|
||||
else
|
||||
ret = strdup (start);
|
||||
break;
|
||||
}
|
||||
iter = next_nonspc;
|
||||
}
|
||||
|
||||
free (cmdline);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* This is an API for other projects to determine whether or not the
|
||||
* currently running system is ostree-controlled.
|
||||
*/
|
||||
@@ -132,7 +70,7 @@ resolve_deploy_path (const char * root_mountpoint)
|
||||
struct stat stbuf;
|
||||
char *ostree_target, *deploy_path;
|
||||
|
||||
ostree_target = parse_ostree_cmdline ();
|
||||
ostree_target = read_proc_cmdline_ostree ();
|
||||
if (!ostree_target)
|
||||
errx (EXIT_FAILURE, "No OSTree target; expected ostree=/ostree/boot.N/...");
|
||||
|
||||
@@ -211,9 +149,12 @@ main(int argc, char *argv[])
|
||||
if (chdir (deploy_path) < 0)
|
||||
err (EXIT_FAILURE, "failed to chdir to deploy_path");
|
||||
|
||||
/* In the systemd case, this is handled by ostree-system-generator */
|
||||
#ifndef HAVE_SYSTEMD_AND_LIBMOUNT
|
||||
/* Link to the deployment's /var */
|
||||
if (mount ("../../var", "var", NULL, MS_MGC_VAL|MS_BIND, NULL) < 0)
|
||||
err (EXIT_FAILURE, "failed to bind mount ../../var to var");
|
||||
#endif
|
||||
|
||||
/* If /boot is on the same partition, use a bind mount to make it visible
|
||||
* at /boot inside the deployment. */
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
||||
*
|
||||
* Copyright (C) 2017 Colin Walters <walters@verbum.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <err.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "ostree-cmdprivate.h"
|
||||
#include "ostree-mount-util.h"
|
||||
|
||||
static const char *arg_dest = "/tmp";
|
||||
static const char *arg_dest_late = "/tmp";
|
||||
|
||||
/* This program is a simple stub that calls the implementation that
|
||||
* lives inside libostree.
|
||||
*/
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
/* Important: if this isn't an ostree-booted system, do nothing; people could
|
||||
* have the package installed as a dependency for flatpak or whatever.
|
||||
*/
|
||||
{ struct stat stbuf;
|
||||
if (fstatat (AT_FDCWD, "/run/ostree-booted", &stbuf, 0) < 0)
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if (argc > 1 && argc != 4)
|
||||
errx (EXIT_FAILURE, "This program takes three or no arguments");
|
||||
|
||||
if (argc > 1)
|
||||
arg_dest = argv[1];
|
||||
if (argc > 3)
|
||||
arg_dest_late = argv[3];
|
||||
|
||||
char *ostree_cmdline = read_proc_cmdline_ostree ();
|
||||
if (!ostree_cmdline)
|
||||
errx (EXIT_FAILURE, "Failed to find ostree= kernel argument");
|
||||
|
||||
{ g_autoptr(GError) local_error = NULL;
|
||||
if (!ostree_cmd__private__()->ostree_system_generator (ostree_cmdline, arg_dest, NULL, arg_dest_late, &local_error))
|
||||
errx (EXIT_FAILURE, "%s", local_error->message);
|
||||
}
|
||||
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
||||
Reference in new issue
Block a user