switchroot: Replace custom error printing with err/warn functions from libc
The `warn()` libc extension has exactly the same behaviour as our own `perrorv` function, but is available in (at least) glibc and musl. As an added bonus the similar function `err()` which will exit with an error code afterwards. This implementation is tidier and allows us to get rid of our own `perrorv`. It paves the way to removing `ostree-mount-util.c` to simplify the build scripts. Original idea by @cgwalters in #477. Closes: #478 Approved by: cgwalters
This commit is contained in:
parent
42dab85728
commit
a128abd9bc
|
|
@ -22,53 +22,19 @@
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <err.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <sys/mount.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/statvfs.h>
|
#include <sys/statvfs.h>
|
||||||
|
|
||||||
#include "ostree-mount-util.h"
|
#include "ostree-mount-util.h"
|
||||||
|
|
||||||
int
|
|
||||||
perrorv (const char *format, ...)
|
|
||||||
{
|
|
||||||
va_list args;
|
|
||||||
char buf[1024];
|
|
||||||
char *p;
|
|
||||||
|
|
||||||
#ifdef _GNU_SOURCE
|
|
||||||
p = strerror_r (errno, buf, sizeof (buf));
|
|
||||||
#else
|
|
||||||
strerror_r (errno, buf, sizeof (buf));
|
|
||||||
buf[sizeof (buf) - 1] = '\0';
|
|
||||||
p = buf;
|
|
||||||
#endif /* _GNU_SOURCE */
|
|
||||||
|
|
||||||
va_start (args, format);
|
|
||||||
|
|
||||||
vfprintf (stderr, format, args);
|
|
||||||
fprintf (stderr, ": %s\n", p);
|
|
||||||
fflush (stderr);
|
|
||||||
|
|
||||||
va_end (args);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
path_is_on_readonly_fs (char *path)
|
path_is_on_readonly_fs (char *path)
|
||||||
{
|
{
|
||||||
struct statvfs stvfsbuf;
|
struct statvfs stvfsbuf;
|
||||||
|
|
||||||
if (statvfs (path, &stvfsbuf) == -1)
|
if (statvfs (path, &stvfsbuf) == -1)
|
||||||
{
|
err (EXIT_FAILURE, "statvfs(%s)", path);
|
||||||
perrorv ("statvfs(%s): ", path);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (stvfsbuf.f_flag & ST_RDONLY) != 0;
|
return (stvfsbuf.f_flag & ST_RDONLY) != 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,4 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
int perrorv (const char *format, ...) __attribute__ ((format (printf, 1, 2)));
|
|
||||||
|
|
||||||
int path_is_on_readonly_fs (char *path);
|
int path_is_on_readonly_fs (char *path);
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <err.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
|
|
@ -86,27 +87,18 @@ parse_ostree_cmdline (void)
|
||||||
{
|
{
|
||||||
// Mount proc
|
// Mount proc
|
||||||
if (mount ("proc", "/proc", "proc", 0, NULL) < 0)
|
if (mount ("proc", "/proc", "proc", 0, NULL) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to mount proc on /proc");
|
||||||
perrorv ("failed to mount proc on /proc: ");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
cmdline = read_proc_cmdline ();
|
cmdline = read_proc_cmdline ();
|
||||||
tmp_errno = errno;
|
tmp_errno = errno;
|
||||||
|
|
||||||
/* Leave the filesystem in the state that we found it: */
|
/* Leave the filesystem in the state that we found it: */
|
||||||
if (umount ("/proc"))
|
if (umount ("/proc"))
|
||||||
{
|
err (EXIT_FAILURE, "failed to umount proc from /proc");
|
||||||
perrorv ("failed to umount proc from /proc: ");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
errno = tmp_errno;
|
errno = tmp_errno;
|
||||||
if (!cmdline)
|
if (!cmdline)
|
||||||
{
|
err (EXIT_FAILURE, "failed to read /proc/cmdline");
|
||||||
perrorv ("failed to read /proc/cmdline: ");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
iter = cmdline;
|
iter = cmdline;
|
||||||
|
|
@ -158,29 +150,17 @@ resolve_deploy_path (const char * root_mountpoint)
|
||||||
|
|
||||||
ostree_target = parse_ostree_cmdline ();
|
ostree_target = parse_ostree_cmdline ();
|
||||||
if (!ostree_target)
|
if (!ostree_target)
|
||||||
{
|
errx (EXIT_FAILURE, "No OSTree target; expected ostree=/ostree/boot.N/...");
|
||||||
fprintf (stderr, "No OSTree target; expected ostree=/ostree/boot.N/...\n");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
snprintf (destpath, sizeof(destpath), "%s/%s", root_mountpoint, ostree_target);
|
snprintf (destpath, sizeof(destpath), "%s/%s", root_mountpoint, ostree_target);
|
||||||
printf ("Examining %s\n", destpath);
|
printf ("Examining %s\n", destpath);
|
||||||
if (lstat (destpath, &stbuf) < 0)
|
if (lstat (destpath, &stbuf) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "Couldn't find specified OSTree root '%s'", destpath);
|
||||||
perrorv ("Couldn't find specified OSTree root '%s': ", destpath);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
if (!S_ISLNK (stbuf.st_mode))
|
if (!S_ISLNK (stbuf.st_mode))
|
||||||
{
|
errx (EXIT_FAILURE, "OSTree target is not a symbolic link: %s", destpath);
|
||||||
fprintf (stderr, "OSTree target is not a symbolic link: %s\n", destpath);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
deploy_path = realpath (destpath, NULL);
|
deploy_path = realpath (destpath, NULL);
|
||||||
if (deploy_path == NULL)
|
if (deploy_path == NULL)
|
||||||
{
|
err (EXIT_FAILURE, "realpath(%s) failed", destpath);
|
||||||
perrorv ("realpath(%s) failed: ", destpath);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
printf ("Resolved OSTree target to: %s\n", deploy_path);
|
printf ("Resolved OSTree target to: %s\n", deploy_path);
|
||||||
return deploy_path;
|
return deploy_path;
|
||||||
}
|
}
|
||||||
|
|
@ -214,33 +194,21 @@ main(int argc, char *argv[])
|
||||||
*
|
*
|
||||||
* https://bugzilla.redhat.com/show_bug.cgi?id=847418 */
|
* https://bugzilla.redhat.com/show_bug.cgi?id=847418 */
|
||||||
if (mount (NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL) < 0)
|
if (mount (NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to make \"/\" private mount");
|
||||||
perrorv ("failed to make \"/\" private mount: %m");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Make deploy_path a bind mount, so we can move it later */
|
/* Make deploy_path a bind mount, so we can move it later */
|
||||||
if (mount (deploy_path, deploy_path, NULL, MS_BIND, NULL) < 0)
|
if (mount (deploy_path, deploy_path, NULL, MS_BIND, NULL) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to make initial bind mount %s", deploy_path);
|
||||||
perrorv ("failed to make initial bind mount %s", deploy_path);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* chdir to our new root. We need to do this after bind-mounting it over
|
/* chdir to our new root. We need to do this after bind-mounting it over
|
||||||
* itself otherwise our cwd is still on the non-bind-mounted filesystem
|
* itself otherwise our cwd is still on the non-bind-mounted filesystem
|
||||||
* below. */
|
* below. */
|
||||||
if (chdir (deploy_path) < 0)
|
if (chdir (deploy_path) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to chdir to deploy_path");
|
||||||
perrorv ("failed to chdir to deploy_path");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Link to the deployment's /var */
|
/* Link to the deployment's /var */
|
||||||
if (mount ("../../var", "var", NULL, MS_MGC_VAL|MS_BIND, NULL) < 0)
|
if (mount ("../../var", "var", NULL, MS_MGC_VAL|MS_BIND, NULL) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to bind mount ../../var to var");
|
||||||
perrorv ("failed to bind mount ../../var to var");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If /boot is on the same partition, use a bind mount to make it visible
|
/* If /boot is on the same partition, use a bind mount to make it visible
|
||||||
* at /boot inside the deployment. */
|
* at /boot inside the deployment. */
|
||||||
|
|
@ -251,10 +219,7 @@ main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
snprintf (srcpath, sizeof(srcpath), "%s/boot", root_mountpoint);
|
snprintf (srcpath, sizeof(srcpath), "%s/boot", root_mountpoint);
|
||||||
if (mount (srcpath, "boot", NULL, MS_BIND, NULL) < 0)
|
if (mount (srcpath, "boot", NULL, MS_BIND, NULL) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to bind mount %s to boot", srcpath);
|
||||||
perrorv ("failed to bind mount %s to boot", srcpath);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -271,31 +236,19 @@ main(int argc, char *argv[])
|
||||||
if (path_is_on_readonly_fs ("."))
|
if (path_is_on_readonly_fs ("."))
|
||||||
{
|
{
|
||||||
if (mount (".", ".", NULL, MS_REMOUNT | MS_SILENT, NULL) < 0)
|
if (mount (".", ".", NULL, MS_REMOUNT | MS_SILENT, NULL) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to remount rootfs writable (for overlayfs)");
|
||||||
perrorv ("failed to remount rootfs writable (for overlayfs)");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mount ("overlay", "usr", "overlay", 0, usr_ovl_options) < 0)
|
if (mount ("overlay", "usr", "overlay", 0, usr_ovl_options) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to mount /usr overlayfs");
|
||||||
perrorv ("failed to mount /usr overlayfs");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Otherwise, a read-only bind mount for /usr */
|
/* Otherwise, a read-only bind mount for /usr */
|
||||||
if (mount ("usr", "usr", NULL, MS_BIND, NULL) < 0)
|
if (mount ("usr", "usr", NULL, MS_BIND, NULL) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to bind mount (class:readonly) /usr");
|
||||||
perrorv ("failed to bind mount (class:readonly) /usr");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
if (mount ("usr", "usr", NULL, MS_BIND | MS_REMOUNT | MS_RDONLY, NULL) < 0)
|
if (mount ("usr", "usr", NULL, MS_BIND | MS_REMOUNT | MS_RDONLY, NULL) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to bind mount (class:readonly) /usr");
|
||||||
perrorv ("failed to bind mount (class:readonly) /usr");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
touch_run_ostree ();
|
touch_run_ostree ();
|
||||||
|
|
@ -309,10 +262,7 @@ main(int argc, char *argv[])
|
||||||
* sysroot, so moving sysroot would also move the deploy location. In
|
* sysroot, so moving sysroot would also move the deploy location. In
|
||||||
* reality attempting mount --move would fail with EBUSY. */
|
* reality attempting mount --move would fail with EBUSY. */
|
||||||
if (pivot_root (".", "sysroot") < 0)
|
if (pivot_root (".", "sysroot") < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to pivot_root to deployment");
|
||||||
perrorv ("failed to pivot_root to deployment");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -330,35 +280,22 @@ main(int argc, char *argv[])
|
||||||
* 3. /sysroot.tmp -> /sysroot
|
* 3. /sysroot.tmp -> /sysroot
|
||||||
*/
|
*/
|
||||||
if (mkdir ("/sysroot.tmp", 0755) < 0)
|
if (mkdir ("/sysroot.tmp", 0755) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "couldn't create temporary sysroot /sysroot.tmp");
|
||||||
perrorv ("couldn't create temporary sysroot /sysroot.tmp: ");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mount (deploy_path, "/sysroot.tmp", NULL, MS_MOVE, NULL) < 0)
|
if (mount (deploy_path, "/sysroot.tmp", NULL, MS_MOVE, NULL) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to MS_MOVE '%s' to '/sysroot.tmp'", deploy_path);
|
||||||
perrorv ("failed to MS_MOVE '%s' to '/sysroot.tmp'", deploy_path);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mount (root_mountpoint, "sysroot", NULL, MS_MOVE, NULL) < 0)
|
if (mount (root_mountpoint, "sysroot", NULL, MS_MOVE, NULL) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to MS_MOVE '%s' to 'sysroot'", root_mountpoint);
|
||||||
perrorv ("failed to MS_MOVE '%s' to 'sysroot'", root_mountpoint);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mount (".", root_mountpoint, NULL, MS_MOVE, NULL) < 0)
|
if (mount (".", root_mountpoint, NULL, MS_MOVE, NULL) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to MS_MOVE %s to %s", deploy_path, root_mountpoint);
|
||||||
perrorv ("failed to MS_MOVE %s to %s", deploy_path, root_mountpoint);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getpid() == 1)
|
if (getpid() == 1)
|
||||||
{
|
{
|
||||||
execl ("/sbin/init", "/sbin/init", NULL);
|
execl ("/sbin/init", "/sbin/init", NULL);
|
||||||
perrorv ("failed to exec init inside ostree");
|
err (EXIT_FAILURE, "failed to exec init inside ostree");
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <err.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include "ostree-mount-util.h"
|
#include "ostree-mount-util.h"
|
||||||
|
|
@ -49,15 +50,10 @@ maybe_mount_tmpfs_on_var (void)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (umount ("/var") < 0 && errno != EINVAL)
|
if (umount ("/var") < 0 && errno != EINVAL)
|
||||||
{
|
warn ("failed to unmount /var prior to mounting tmpfs, mounting over");
|
||||||
perror ("failed to unmount /var prior to mounting tmpfs, mounting over");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mount ("tmpfs", "/var", "tmpfs", 0, NULL) < 0)
|
if (mount ("tmpfs", "/var", "tmpfs", 0, NULL) < 0)
|
||||||
{
|
err (EXIT_FAILURE, "failed to mount tmpfs on /var");
|
||||||
perror ("failed to mount tmpfs on /var");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
@ -94,10 +90,7 @@ main(int argc, char *argv[])
|
||||||
* already, then assume things are OK.
|
* already, then assume things are OK.
|
||||||
*/
|
*/
|
||||||
if (errno != EINVAL)
|
if (errno != EINVAL)
|
||||||
{
|
err (EXIT_FAILURE, "failed to remount %s", target);
|
||||||
perrorv ("failed to remount %s", target);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue