gpt-auto-generator.c revision 4b357e15876b730343db08719c877fdb45b6ad42
/*-*- 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>
#include <blkid.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 "special.h"
#include "unit-name.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";
_cleanup_free_ char *t = NULL;
blkid_probe b = NULL;
const char *v;
int r;
if (r < 0)
return -ENOMEM;
errno = 0;
b = blkid_new_probe_from_filename(t);
if (!b) {
if (errno != 0)
return -errno;
return -ENOMEM;
}
errno = 0;
r = blkid_do_safeprobe(b);
if (r == -2) {
r = -ENODEV;
goto finish;
} else if (r == 1) {
r = -ENODEV;
goto finish;
} else if (r != 0) {
goto finish;
}
errno = 0;
if (r != 0) {
goto finish;
}
if (strcmp(v, "gpt") != 0) {
r = 0;
goto finish;
}
if (type) {
errno = 0;
if (r != 0) {
goto finish;
}
r = sd_id128_from_string(v, type);
if (r < 0)
return r;
}
if (nr) {
errno = 0;
if (r != 0) {
goto finish;
}
if (r < 0)
return r;
}
if (fstype) {
char *fst;
errno = 0;
if (r != 0)
else {
if (!fst) {
r = -ENOMEM;
goto finish;
}
}
}
return 1;
if (b)
blkid_free_probe(b);
return r;
}
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;
}
fprintf(f,
"# Automatically generated by systemd-gpt-auto-generator\n\n"
"[Unit]\n"
"DefaultDependencies=no\n"
"[Mount]\n"
"What=%s\n"
"Where=/home\n"
"Type=%s\n"
"FsckPassNo=2\n",
fflush(f);
if (ferror(f)) {
return -errno;
}
if (!lnk)
return log_oom();
return -errno;
}
return 0;
}
struct udev_enumerate *e = NULL;
unsigned home_nr = (unsigned) -1;
int r;
if (!udev)
return log_oom();
e = udev_enumerate_new(udev);
if (!e) {
r = log_oom();
goto finish;
}
if (!d) {
r = log_oom();
goto finish;
}
parent = udev_device_get_parent(d);
if (!parent) {
r = log_oom();
goto finish;
}
r = udev_enumerate_add_match_parent(e, parent);
if (r < 0) {
r = log_oom();
goto finish;
}
r = udev_enumerate_add_match_subsystem(e, "block");
if (r < 0) {
r = log_oom();
goto finish;
}
r = udev_enumerate_scan_devices(e);
if (r < 0) {
goto finish;
}
struct udev_device *q;
unsigned nr;
if (!q) {
r = log_oom();
goto finish;
}
if (udev_device_get_devnum(q) == udev_device_get_devnum(d))
goto skip;
goto skip;
node = udev_device_get_devnode(q);
if (!node) {
r = log_oom();
goto finish;
}
if (r < 0) {
goto finish;
}
if (r == 0)
goto skip;
if (!home) {
r = log_oom();
goto finish;
}
}
}
skip:
}
if (home && home_fstype)
if (d)
if (e)
if (udev)
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;
}
int r;
log_error("This program takes three or no arguments.");
return EXIT_FAILURE;
}
if (argc > 1)
log_open();
umask(0022);
if (in_initrd())
return EXIT_SUCCESS;
if (r < 0) {
return EXIT_FAILURE;
}
if (r == 0) {
log_debug("Root file system not on a (single) block device.");
return EXIT_SUCCESS;
}
if (r < 0) {
return EXIT_FAILURE;
}
if (r == 0)
return EXIT_SUCCESS;
r = enumerate_partitions(dev);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}