gpt-auto-generator.c revision 73b80ec2d999c45ce13f3e034704249d80829f7e
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering This file is part of systemd.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering Copyright 2013 Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering (at your option) any later version.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering systemd is distributed in the hope that it will be useful, but
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering Lesser General Public License for more details.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poetteringstatic bool arg_enabled = true;
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poetteringstatic bool arg_root_enabled = true;
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poetteringstatic bool arg_root_rw = false;
14bf2c9d375db6a4670bc0ef0e521e35a939a498Lennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(blkid_probe, blkid_free_probe);
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek#define _cleanup_blkid_freep_probe_ _cleanup_(blkid_free_probep)
3db604b907323b8df0fc810216f6112056d26a02Lennart Poetteringstatic int verify_gpt_partition(const char *node, sd_id128_t *type, unsigned *nr, char **fstype) {
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek _cleanup_blkid_freep_probe_ blkid_probe b = NULL;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering const char *v;
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek return errno != 0 ? -errno : -ENOMEM;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
b94801803417c23d099cb7e508754181ecd27f9cZbigniew Jędrzejewski-Szmek if (r == -2 || r == 1) /* no result or uncertain */
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek else if (r != 0)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = blkid_probe_lookup_value(b, "PART_ENTRY_SCHEME", &v, NULL);
091526ab20485492124852dcf629787f35816df8Zbigniew Jędrzejewski-Szmek /* return 0 if we're not on GPT */
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = blkid_probe_lookup_value(b, "PART_ENTRY_NUMBER", &v, NULL);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = blkid_probe_lookup_value(b, "TYPE", &v, NULL);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringstatic int add_swap(const char *path, const char *fstype) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering _cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering log_debug("Adding swap: %s %s", path, fstype);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering name = unit_name_from_path(path, ".swap");
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering unit = strjoin(arg_dest, "/", name, NULL);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering log_error("Failed to create unit file %s: %m", unit);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering "Description=Swap Partition\n"
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering "Documentation=man:systemd-gpt-auto-generator(8)\n\n"
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering log_error("Failed to write unit file %s: %m", unit);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering log_error("Failed to create symlink %s: %m", lnk);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic int add_cryptsetup(const char *id, const char *what, char **device) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_free_ char *e = NULL, *n = NULL, *p = NULL, *d = NULL, *to = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering d = unit_name_from_path(what, ".device");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering n = unit_name_build("systemd-cryptsetup", e, ".service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering log_error("Failed to create unit file %s: %m", p);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "Description=Cryptography Setup for %%I\n"
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering "Documentation=man:systemd-gpt-auto-generator(8) man:systemd-cryptsetup@.service(8)\n"
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "DefaultDependencies=no\n"
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "Conflicts=umount.target\n"
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "BindsTo=dev-mapper-%%i.device %s\n"
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "Before=umount.target cryptsetup.target\n"
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "IgnoreOnIsolate=true\n"
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "After=systemd-readahead-collect.service systemd-readahead-replay.service\n\n"
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "Type=oneshot\n"
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "RemainAfterExit=yes\n"
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "TimeoutSec=0\n" /* the binary handles timeouts anyway */
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s'\n"
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering log_error("Failed to write file %s: %m", p);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering to = strjoin(arg_dest, "/", d, ".wants/", n, NULL);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering log_error("Failed to create symlink %s: %m", to);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering to = strjoin(arg_dest, "/cryptsetup.target.requires/", n, NULL);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering log_error("Failed to create symlink %s: %m", to);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering to = strjoin(arg_dest, "/dev-mapper-", e, ".device.requires/", n, NULL);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering log_error("Failed to create symlink %s: %m", to);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering p = strjoin(arg_dest, "/dev-mapper-", e, ".device.d/50-job-timeout-sec-0.conf", NULL);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering "JobTimeoutSec=0\n"); /* the binary handles timeouts anyway */
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering log_error("Failed to write device drop-in: %s", strerror(-r));
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering const char *post) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_free_ char *unit = NULL, *lnk = NULL, *crypto_what = NULL, *p = NULL;
6d26dfe11c853d612b84abe858520bbcb62c2e96Lennart Poettering if (path_is_mount_point(where, true) <= 0 &&
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering log_debug("%s already populated, ignoring.", where);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering log_debug("Adding %s: %s %s", where, what, strna(fstype));
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering r = add_cryptsetup(id, what, &crypto_what);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering unit = unit_name_from_path(where, ".mount");
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering log_error("Failed to create unit file %s: %m", unit);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering "Description=%s\n"
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering "Documentation=man:systemd-gpt-auto-generator(8)\n",
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering r = generator_write_fsck_deps(f, arg_dest, what, where, fstype);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering log_error("Failed to write unit file %s: %m", p);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering lnk = strjoin(arg_dest, "/", post, ".requires/", unit, NULL);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering log_error("Failed to create symlink %s: %m", lnk);
3db604b907323b8df0fc810216f6112056d26a02Lennart Poetteringstatic int enumerate_partitions(struct udev *udev, dev_t dev) {
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek _cleanup_udev_device_unref_ struct udev_device *d = NULL;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering _cleanup_free_ char *home = NULL, *home_fstype = NULL, *srv = NULL, *srv_fstype = NULL;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering unsigned home_nr = (unsigned) -1, srv_nr = (unsigned )-1;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering d = udev_device_new_from_devnum(udev, 'b', dev);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = udev_enumerate_add_match_parent(e, parent);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = udev_enumerate_add_match_subsystem(e, "block");
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering log_error("Failed to enumerate partitions on /dev/block/%u:%u: %s", major(dev), minor(dev), strerror(-r));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering first = udev_enumerate_get_list_entry(e);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering _cleanup_udev_device_unref_ struct udev_device *q;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (udev_device_get_devnum(q) == udev_device_get_devnum(d))
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (udev_device_get_devnum(q) == udev_device_get_devnum(parent))
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering k = verify_gpt_partition(node, &type_id, &nr, &fstype);
843f737ade9c73609a2280dd3dd16e18222a5dcbŁukasz Stelmach /* skip child devices which are not detected properly */
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering log_error("Failed to verify GPT partition %s: %s", node, strerror(-k));
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering } else if (sd_id128_equal(type_id, GPT_HOME)) {
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering /* We only care for the first /home partition */
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering } else if (sd_id128_equal(type_id, GPT_SRV)) {
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering /* We only care for the first /srv partition */
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering k = add_mount("home", home, "/home", home_fstype, NULL, "Home Partition", SPECIAL_LOCAL_FS_TARGET);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering k = add_mount("srv", srv, "/srv", srv_fstype, NULL, "Server Data Partition", SPECIAL_LOCAL_FS_TARGET);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringstatic int get_btrfs_block_device(const char *path, dev_t *dev) {
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek struct btrfs_ioctl_fs_info_args fsi = {};
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (ioctl(fd, BTRFS_IOC_FS_INFO, &fsi) < 0)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering /* We won't do this for btrfs RAID */
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek struct btrfs_ioctl_dev_info_args di = {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (ioctl(fd, BTRFS_IOC_DEV_INFO, &di) < 0) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringstatic int get_block_device(const char *path, dev_t *dev) {
c51cf05646a11c65daf65c1123c77efb068f4f7bZbigniew Jędrzejewski-Szmek if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC))
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return get_btrfs_block_device(path, dev);
3db604b907323b8df0fc810216f6112056d26a02Lennart Poetteringstatic int devno_to_devnode(struct udev *udev, dev_t devno, char **ret) {
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek _cleanup_udev_device_unref_ struct udev_device *d;
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering const char *t;
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering d = udev_device_new_from_devnum(udev, 'b', devno);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poetteringstatic int parse_proc_cmdline_item(const char *key, const char *value) {
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering if (STR_IN_SET(key, "systemd.gpt_auto", "rd.systemd.gpt_auto") && value) {
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering log_warning("Failed to parse gpt-auto switch %s. Ignoring.", value);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering } else if (streq(key, "root") && value) {
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering /* Disable root disk logic if there's a root= value
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering * specified (unless it happens to be "gpt-auto") */
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering arg_root_enabled = streq(value, "gpt-auto");
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering else if (startswith(key, "systemd.gpt-auto.") || startswith(key, "rd.systemd.gpt-auto."))
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering log_warning("Unknown kernel switch %s. Ignoring.", key);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poetteringstatic int add_root_mount(void) {
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering log_debug("Not a EFI boot, not creating root mount.");
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering r = efi_loader_get_device_part_uuid(NULL);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering log_debug("EFI loader partition unknown, exiting.");
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering } else if (r < 0) {
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering log_error("Failed to read ESP partition UUID: %s", strerror(-r));
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering /* OK, we have an ESP partition, this is fantastic, so let's
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering * wait for a root device to show up. A udev rule will create
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering * the link for us under the right name. */
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering "Root Partition",
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET : SPECIAL_LOCAL_FS_TARGET);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poetteringstatic int add_mounts(void) {
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering _cleanup_udev_unref_ struct udev *udev = NULL;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering log_error("Failed to determine block device of root file system: %s", strerror(-r));
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering } else if (r == 0) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering log_debug("Root file system not on a (single) block device.");
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering r = devno_to_devnode(udev, devno, &node);
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering log_error("Failed to determine block device node from major/minor: %s", strerror(-r));
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering r = verify_gpt_partition(node, NULL, NULL, NULL);
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering log_error("Failed to verify GPT partition %s: %s", node, strerror(-r));
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering return enumerate_partitions(udev, devno);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering log_error("This program takes three or no arguments.");
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering if (parse_proc_cmdline(parse_proc_cmdline_item) < 0)