gpt-auto-generator.c revision 96115cdfe0241ae9b4e7177cd3874c0a93d00b39
/*-*- 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"
/* TODO:
*
* - Properly handle cryptsetup partitions
* - Define new partition type for encrypted swap
* - Make /home automount rather than mount
*
*/
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)
return -ENODEV;
else if (r == 1)
return -ENODEV;
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"
"[Unit]\n"
"DefaultDependencies=no\n"
"[Swap]\n"
"What=%s\n",
path);
fflush(f);
if (ferror(f)) {
return -errno;
}
if (!lnk)
return log_oom();
return -errno;
}
return 0;
}
if (dir_is_empty("/home") <= 0)
return 0;
if (!unit)
return log_oom();
if (!f) {
return -errno;
}
if (!fsck)
return log_oom();
fprintf(f,
"# Automatically generated by systemd-gpt-auto-generator\n\n"
"[Unit]\n"
"DefaultDependencies=no\n"
"Requires=%s\n"
"[Mount]\n"
"What=%s\n"
"Where=/home\n"
"Type=%s\n",
fflush(f);
if (ferror(f)) {
return -errno;
}
if (!lnk)
return log_oom();
return -errno;
}
return 0;
}
unsigned home_nr = (unsigned) -1;
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) {
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) {
log_error("Failed to verify GPT partition %s: %s",
return r;
}
if (r == 0)
continue;
if (!home)
return log_oom();
}
}
}
if (home && home_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.");
r = -EINVAL;
goto finish;
}
if (argc > 1)
log_open();
umask(0022);
if (in_initrd()) {
log_debug("In initrd, exiting.");
goto finish;
}
if (detect_container(NULL) > 0) {
log_debug("In a container, exiting.");
goto finish;
}
if (r < 0) {
goto finish;
}
if (r == 0) {
log_debug("Root file system not on a (single) block device.");
goto finish;
}
if (!udev) {
r = log_oom();
goto finish;
}
if (r < 0) {
goto finish;
}
if (r < 0) {
goto finish;
}
if (r == 0)
goto finish;
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}