gpt-auto-generator.c revision 4c8bda2442bfc6d84a5deb241dc29efcb81bf3af
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/>.
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering * - Properly handle cryptsetup partitions
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering * - Define new partition type for encrypted swap
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering * - Make /home automount rather than mount
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering#define GPT_SWAP SD_ID128_MAKE(06,57,fd,6d,a4,ab,43,c4,84,e5,09,33,c8,4b,4f,4f)
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek#define GPT_HOME SD_ID128_MAKE(93,3a,c7,e1,2e,b4,4f,13,b8,44,0e,14,e2,ae,f9,15)
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmekstatic const char *arg_dest = "/tmp";
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-SzmekDEFINE_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) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering _cleanup_blkid_freep_probe_ blkid_probe b = NULL;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering const char *v;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek else if (r == 1)
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek else if (r != 0)
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering r = blkid_probe_lookup_value(b, "PART_ENTRY_SCHEME", &v, NULL);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering /* return 0 if we're not on GPT */
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL);
96cde13ace6406582688028f3df5668a172ba628Zbigniew 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);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poetteringstatic int add_swap(const char *path, const char *fstype) {
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart 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");
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering unit = strjoin(arg_dest, "/", name, NULL);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering log_error("Failed to create unit file %s: %m", unit);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering "DefaultDependencies=no\n"
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering "Before=" SPECIAL_UMOUNT_TARGET " " SPECIAL_SWAP_TARGET "\n\n"
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering log_error("Failed to write unit file %s: %m", unit);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering log_error("Failed to create symlink %s: %m", lnk);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poetteringstatic int add_home(const char *path, const char *fstype) {
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering _cleanup_free_ char *unit = NULL, *lnk = NULL, *fsck = NULL;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering log_debug("Adding home: %s %s", path, fstype);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering unit = strappend(arg_dest, "/home.mount");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_error("Failed to create unit file %s: %m", unit);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering fsck = unit_name_from_path_instance("systemd-fsck", path, ".service");
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek "DefaultDependencies=no\n"
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering "Requires=%s\n"
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering "After=" SPECIAL_LOCAL_FS_PRE_TARGET " %s\n"
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek "Conflicts=" SPECIAL_UMOUNT_TARGET "\n"
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering "Before=" SPECIAL_UMOUNT_TARGET " " SPECIAL_LOCAL_FS_TARGET "\n\n"
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering "Where=/home\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_error("Failed to write unit file %s: %m", unit);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering lnk = strjoin(arg_dest, "/" SPECIAL_LOCAL_FS_TARGET ".requires/home.mount", NULL);
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering log_error("Failed to create symlink %s: %m", lnk);
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poetteringstatic int enumerate_partitions(struct udev *udev, dev_t dev) {
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering _cleanup_udev_device_unref_ struct udev_device *d = NULL;
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering _cleanup_free_ char *home = NULL, *home_fstype = NULL;
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering d = udev_device_new_from_devnum(udev, 'b', dev);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering r = udev_enumerate_add_match_parent(e, parent);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering r = udev_enumerate_add_match_subsystem(e, "block");
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering log_error("Failed to enumerate partitions on /dev/block/%u:%u: %s",
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering first = udev_enumerate_get_list_entry(e);
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering _cleanup_udev_device_unref_ struct udev_device *q;
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering if (udev_device_get_devnum(q) == udev_device_get_devnum(d))
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering if (udev_device_get_devnum(q) == udev_device_get_devnum(parent))
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering r = verify_gpt_partition(node, &type_id, &nr, &fstype);
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther log_error("Failed to verify GPT partition %s: %s",
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek if (sd_id128_equal(type_id, GPT_SWAP))
if (!home)
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;
r = -EINVAL;
goto finish;
log_open();
if (in_initrd()) {
goto finish;
goto finish;
goto finish;
goto finish;
if (!udev) {
r = log_oom();
goto finish;
goto finish;
goto finish;
goto finish;