gpt-auto-generator.c revision e48fdd84432bbf9c2ecc339183258c7c33116032
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 Poettering * - Properly handle cryptsetup partitions
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering * - Define new partition type for encrypted swap
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering * - Make /home automount rather than mount
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(blkid_probe, blkid_free_probe);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering#define _cleanup_blkid_freep_probe_ _cleanup_(blkid_free_probep)
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic int verify_gpt_partition(const char *node, sd_id128_t *type, unsigned *nr, char **fstype) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering _cleanup_blkid_freep_probe_ blkid_probe b = NULL;
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering const char *v;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering if (r == -2 || r == 1) /* no result or uncertain */
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering else if (r != 0)
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering r = blkid_probe_lookup_value(b, "PART_ENTRY_SCHEME", &v, NULL);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering /* return 0 if we're not on GPT */
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering r = blkid_probe_lookup_value(b, "PART_ENTRY_NUMBER", &v, NULL);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering r = blkid_probe_lookup_value(b, "TYPE", &v, NULL);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poetteringstatic int add_swap(const char *path, const char *fstype) {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering _cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering log_debug("Adding swap: %s %s", path, fstype);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering name = unit_name_from_path(path, ".swap");
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering unit = strjoin(arg_dest, "/", name, NULL);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering log_error("Failed to create unit file %s: %m", unit);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_error("Failed to write unit file %s: %m", unit);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering log_error("Failed to create symlink %s: %m", lnk);
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poetteringstatic int add_mount(const char *what, const char *where, const char *fstype, const char *description) {
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering _cleanup_free_ char *unit = NULL, *lnk = NULL, *p = NULL;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_debug("%s already populated, ignoring.", where);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_debug("Adding %s: %s %s", where, what, fstype);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering unit = unit_name_from_path(where, ".mount");
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering log_error("Failed to create unit file %s: %m", unit);
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering "Description=%s\n",
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering r = generator_write_fsck_deps(f, arg_dest, what, where, fstype);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering log_error("Failed to write unit file %s: %m", unit);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering lnk = strjoin(arg_dest, "/" SPECIAL_LOCAL_FS_TARGET ".requires/", unit, NULL);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering log_error("Failed to create symlink %s: %m", lnk);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic int enumerate_partitions(struct udev *udev, dev_t dev) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering _cleanup_udev_device_unref_ struct udev_device *d = NULL;
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther _cleanup_free_ char *home = NULL, *home_fstype = NULL, *srv = NULL, *srv_fstype = NULL;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering unsigned home_nr = (unsigned) -1, srv_nr = (unsigned )-1;
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther struct udev_list_entry *first, *item;
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;