gpt-auto-generator.c revision 6a3f892a23db71544d0439355f96c44350dafa8f
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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 systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#ifdef HAVE_LINUX_BTRFS_H
#endif
#include "path-util.h"
#include "util.h"
#include "mkdir.h"
#include "missing.h"
#include "sd-id128.h"
#include "libudev.h"
#include "udev-util.h"
#include "special.h"
#include "unit-name.h"
#include "virt.h"
#include "generator.h"
#include "gpt.h"
static const char *arg_dest = "/tmp";
const char *v;
int r;
errno = 0;
if (!b)
errno = 0;
r = blkid_do_safeprobe(b);
if (r == -2 || r == 1) /* no result or uncertain */
return -EBADSLT;
else if (r != 0)
errno = 0;
if (r != 0)
/* return 0 if we're not on GPT */
if (strcmp(v, "gpt") != 0)
return 0;
if (type) {
errno = 0;
if (r != 0)
r = sd_id128_from_string(v, type);
if (r < 0)
return r;
}
if (nr) {
errno = 0;
if (r != 0)
if (r < 0)
return r;
}
if (fstype) {
errno = 0;
if (r != 0)
else {
char *fst;
if (!fst)
return -ENOMEM;
}
}
return 1;
}
if (!name)
return log_oom();
if (!unit)
return log_oom();
if (!f) {
return -errno;
}
fprintf(f,
"# Automatically generated by systemd-gpt-auto-generator\n\n"
"[Swap]\n"
"What=%s\n",
path);
fflush(f);
if (ferror(f)) {
return -errno;
}
if (!lnk)
return log_oom();
return -errno;
}
return 0;
}
static int add_mount(const char *what, const char *where, const char *fstype, const char *description) {
int r;
if (dir_is_empty(where) <= 0) {
return 0;
}
if (!unit)
return log_oom();
if (!p)
return log_oom();
f = fopen(p, "wxe");
if (!f) {
return -errno;
}
fprintf(f,
"# Automatically generated by systemd-gpt-auto-generator\n\n"
"[Unit]\n"
"Description=%s\n",
if (r < 0)
return r;
fprintf(f,
"\n"
"[Mount]\n"
"What=%s\n"
"Where=%s\n"
"Type=%s\n",
fflush(f);
if (ferror(f)) {
return -errno;
}
if (!lnk)
return log_oom();
return -errno;
}
return 0;
}
int r;
e = udev_enumerate_new(udev);
if (!e)
return log_oom();
if (!d)
return log_oom();
parent = udev_device_get_parent(d);
if (!parent)
return log_oom();
r = udev_enumerate_add_match_parent(e, parent);
if (r < 0)
return log_oom();
r = udev_enumerate_add_match_subsystem(e, "block");
if (r < 0)
return log_oom();
r = udev_enumerate_scan_devices(e);
if (r < 0) {
log_error("Failed to enumerate partitions on /dev/block/%u:%u: %s", major(dev), minor(dev), strerror(-r));
return r;
}
_cleanup_udev_device_unref_ struct udev_device *q;
unsigned nr;
if (!q)
return log_oom();
if (udev_device_get_devnum(q) == udev_device_get_devnum(d))
continue;
continue;
node = udev_device_get_devnode(q);
if (!node)
return log_oom();
if (r < 0) {
/* skip child devices which are not detected properly */
if (r == -EBADSLT)
continue;
return r;
}
if (r == 0)
continue;
/* We only care for the first /home partition */
continue;
if (!home)
return log_oom();
/* We only care for the first /srv partition */
continue;
if (!srv)
return log_oom();
srv_fstype = fstype;
}
}
if (home && home_fstype)
if (srv && srv_fstype)
return r;
}
struct btrfs_ioctl_fs_info_args fsi = {};
if (fd < 0)
return -errno;
return -errno;
/* We won't do this for btrfs RAID */
return 0;
struct btrfs_ioctl_dev_info_args di = {
};
continue;
return -errno;
}
return -errno;
return -ENODEV;
return -ENODEV;
return 1;
}
return -ENODEV;
}
return -errno;
return 1;
}
return -errno;
return 0;
}
_cleanup_udev_device_unref_ struct udev_device *d;
const char *t;
char *n;
if (!d)
return log_oom();
t = udev_device_get_devnode(d);
if (!t)
return -ENODEV;
n = strdup(t);
if (!n)
return -ENOMEM;
*ret = n;
return 0;
}
int r = 0;
log_error("This program takes three or no arguments.");
return EXIT_FAILURE;
}
if (argc > 1)
log_open();
umask(0022);
if (in_initrd()) {
log_debug("In initrd, exiting.");
return EXIT_SUCCESS;
}
if (detect_container(NULL) > 0) {
log_debug("In a container, exiting.");
return EXIT_SUCCESS;
}
if (r < 0) {
return EXIT_FAILURE;
} else if (r == 0) {
log_debug("Root file system not on a (single) block device.");
return EXIT_SUCCESS;
}
if (!udev) {
log_oom();
return EXIT_FAILURE;
}
if (r < 0) {
return EXIT_FAILURE;
}
if (r < 0) {
return EXIT_FAILURE;
}
if (r == 0) {
log_debug("Not a GPT disk, exiting.");
return EXIT_SUCCESS;
}
if (r < 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}