gpt-auto-generator.c revision 1af7211984a8dba3c5ba40fae794c4c55f5e6bd3
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering This file is part of systemd.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Copyright 2013 Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering (at your option) any later version.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering systemd is distributed in the hope that it will be useful, but
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Lesser General Public License for more details.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
6c03089c32c251d823173bda4d809a9e643219f0Lennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(blkid_probe, blkid_free_probe);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering#define _cleanup_blkid_freep_probe_ _cleanup_(blkid_free_probep)
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic int verify_gpt_partition(const char *node, sd_id128_t *type, unsigned *nr, char **fstype) {
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek _cleanup_blkid_freep_probe_ blkid_probe b = NULL;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering const char *v;
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering if (r == -2 || r == 1) /* no result or uncertain */
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek else if (r != 0)
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering r = blkid_probe_lookup_value(b, "PART_ENTRY_SCHEME", &v, NULL);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek /* return 0 if we're not on GPT */
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek r = blkid_probe_lookup_value(b, "PART_ENTRY_NUMBER", &v, NULL);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering r = blkid_probe_lookup_value(b, "TYPE", &v, NULL);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poetteringstatic int add_swap(const char *path, const char *fstype) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering _cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL;
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering log_debug("Adding swap: %s %s", path, fstype);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering name = unit_name_from_path(path, ".swap");
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering unit = strjoin(arg_dest, "/", name, NULL);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering log_error("Failed to create unit file %s: %m", unit);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering log_error("Failed to write unit file %s: %m", unit);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering log_error("Failed to create symlink %s: %m", lnk);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic int add_cryptsetup(const char *id, const char *what, char **device) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering _cleanup_free_ char *e = NULL, *n = NULL, *p = NULL, *d = NULL, *to = NULL;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering d = unit_name_from_path(what, ".device");
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering n = unit_name_build("systemd-cryptsetup", e, ".service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_error("Failed to create unit file %s: %m", p);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering "Description=Cryptography Setup for %%I\n"
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering "Documentation=man:systemd-cryptsetup@.service(8)\n"
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering "DefaultDependencies=no\n"
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering "Conflicts=umount.target\n"
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek "BindsTo=dev-mapper-%%i.device %s\n"
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek "Before=umount.target cryptsetup.target\n"
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering "IgnoreOnIsolate=true\n"
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek "After=systemd-readahead-collect.service systemd-readahead-replay.service\n\n"
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering "Type=oneshot\n"
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering "RemainAfterExit=yes\n"
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering "TimeoutSec=0\n" /* the binary handles timeouts anyway */
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s'\n"
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_error("Failed to write file %s: %m", p);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering to = strjoin(arg_dest, "/", d, ".wants/", n, NULL);
e13bb5d2b133f9ae51c0a2d20aa51071c780e9aeKay Sievers log_error("Failed to create symlink %s: %m", to);
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering to = strjoin(arg_dest, "/cryptsetup.target.requires/", n, NULL);
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering log_error("Failed to create symlink %s: %m", to);
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering to = strjoin(arg_dest, "/dev-mapper-", e, ".device.requires/", n, NULL);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering log_error("Failed to create symlink %s: %m", to);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering p = strjoin(arg_dest, "/dev-mapper-", e, ".device.d/50-job-timeout-sec-0.conf", NULL);
c96cc5822c165e86be78ed96dac6573986032fabLennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
c96cc5822c165e86be78ed96dac6573986032fabLennart Poettering "JobTimeoutSec=0\n"); /* the binary handles timeouts anyway */
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering log_error("Failed to write device drop-in: %s", strerror(-r));
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poetteringstatic int add_mount(const char *id, const char *what, const char *where, const char *fstype, const char *description) {
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering _cleanup_free_ char *unit = NULL, *lnk = NULL, *crypto_what = NULL, *p = NULL;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering log_debug("%s already populated, ignoring.", where);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering log_debug("Adding %s: %s %s", where, what, fstype);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering r = add_cryptsetup(id, what, &crypto_what);
if (!unit)
return log_oom();
return log_oom();
return -errno;
fprintf(f,
fprintf(f,
if (fstype) {
fprintf(f,
fstype);
fflush(f);
if (ferror(f)) {
return -errno;
if (!lnk)
return log_oom();
return -errno;
return log_oom();
return log_oom();
if (!parent)
return log_oom();
return log_oom();
return log_oom();
r = udev_enumerate_scan_devices(e);
log_error("Failed to enumerate partitions on /dev/block/%u:%u: %s", major(dev), minor(dev), strerror(-r));
unsigned nr;
return log_oom();
if (!node)
return log_oom();
if (r == -EBADSLT)
if (!home)
return log_oom();
if (!srv)
return log_oom();
if (fd < 0)
return -errno;
return -errno;
return -errno;
return -errno;
return -ENODEV;
return -ENODEV;
return -ENODEV;
return -errno;
return -errno;
return log_oom();
t = udev_device_get_devnode(d);
return -ENODEV;
n = strdup(t);
return -ENOMEM;
*ret = n;
return EXIT_FAILURE;
log_open();
if (in_initrd()) {
return EXIT_SUCCESS;
return EXIT_SUCCESS;
return EXIT_FAILURE;
return EXIT_SUCCESS;
if (!udev) {
log_oom();
return EXIT_FAILURE;
return EXIT_FAILURE;
return EXIT_FAILURE;
return EXIT_SUCCESS;
return EXIT_FAILURE;
return EXIT_SUCCESS;