1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <linux/loop.h>
9 #if HAVE_VALGRIND_MEMCHECK_H
10 #include <valgrind/memcheck.h>
13 #include "sd-daemon.h"
14 #include "sd-device.h"
19 #include "blkid-util.h"
20 #include "blockdev-util.h"
21 #include "btrfs-util.h"
22 #include "chattr-util.h"
23 #include "cryptsetup-util.h"
24 #include "device-util.h"
25 #include "devnum-util.h"
26 #include "dissect-image.h"
28 #include "errno-util.h"
30 #include "fdisk-util.h"
32 #include "filesystems.h"
33 #include "format-util.h"
35 #include "fsck-util.h"
36 #include "glyph-util.h"
37 #include "home-util.h"
39 #include "homework-blob.h"
40 #include "homework-luks.h"
41 #include "homework-mount.h"
42 #include "homework-password-cache.h"
44 #include "json-util.h"
45 #include "keyring-util.h"
46 #include "loop-util.h"
47 #include "memory-util.h"
48 #include "missing_magic.h"
49 #include "missing_syscall.h"
51 #include "mkfs-util.h"
52 #include "openssl-util.h"
53 #include "parse-util.h"
54 #include "path-util.h"
55 #include "process-util.h"
56 #include "random-util.h"
57 #include "resize-fs.h"
58 #include "string-util.h"
60 #include "sync-util.h"
61 #include "time-util.h"
62 #include "tmpfile-util.h"
63 #include "udev-util.h"
64 #include "user-record-util.h"
65 #include "user-record.h"
66 #include "user-util.h"
68 /* Round down to the nearest 4K size. Given that newer hardware generally prefers 4K sectors, let's align our
69 * partitions to that too. In the worst case we'll waste 3.5K per partition that way, but I think I can live
71 #define DISK_SIZE_ROUND_DOWN(x) ((x) & ~UINT64_C(4095))
73 /* Rounds up to the nearest 4K boundary. Returns UINT64_MAX on overflow */
74 #define DISK_SIZE_ROUND_UP(x) \
77 _x > UINT64_MAX - 4095U ? UINT64_MAX : (_x + 4095U) & ~UINT64_C(4095); \
80 /* How much larger will the image on disk be than the fs inside it, i.e. the space we pay for the GPT and
81 * LUKS2 envelope. (As measured on cryptsetup 2.4.1) */
82 #define GPT_LUKS2_OVERHEAD UINT64_C(18874368)
84 static int resize_image_loop(UserRecord
*h
, HomeSetup
*setup
, uint64_t old_image_size
, uint64_t new_image_size
, uint64_t *ret_image_size
);
86 int run_mark_dirty(int fd
, bool b
) {
90 /* Sets or removes the 'user.home-dirty' xattr on the specified file. We use this to detect when a
91 * home directory was not properly unmounted. */
95 r
= fd_verify_regular(fd
);
100 ret
= fsetxattr(fd
, "user.home-dirty", &x
, 1, XATTR_CREATE
);
101 if (ret
< 0 && errno
!= EEXIST
)
102 return log_debug_errno(errno
, "Could not mark home directory as dirty: %m");
107 return log_debug_errno(r
, "Failed to synchronize image before marking it clean: %m");
109 ret
= fremovexattr(fd
, "user.home-dirty");
110 if (ret
< 0 && !ERRNO_IS_XATTR_ABSENT(errno
))
111 return log_debug_errno(errno
, "Could not mark home directory as clean: %m");
116 return log_debug_errno(r
, "Failed to synchronize dirty flag to disk: %m");
121 int run_mark_dirty_by_path(const char *path
, bool b
) {
122 _cleanup_close_
int fd
= -EBADF
;
126 fd
= open(path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
);
128 return log_debug_errno(errno
, "Failed to open %s to mark dirty or clean: %m", path
);
130 return run_mark_dirty(fd
, b
);
133 static int probe_file_system_by_fd(
136 sd_id128_t
*ret_uuid
) {
138 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
139 const char *fstype
= NULL
, *uuid
= NULL
;
147 b
= blkid_new_probe();
152 r
= blkid_probe_set_device(b
, fd
, 0, 0);
154 return errno_or_else(ENOMEM
);
156 (void) blkid_probe_enable_superblocks(b
, 1);
157 (void) blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
|BLKID_SUBLKS_UUID
);
160 r
= blkid_do_safeprobe(b
);
161 if (r
== _BLKID_SAFEPROBE_ERROR
)
162 return errno_or_else(EIO
);
163 if (IN_SET(r
, _BLKID_SAFEPROBE_AMBIGUOUS
, _BLKID_SAFEPROBE_NOT_FOUND
))
166 assert(r
== _BLKID_SAFEPROBE_FOUND
);
168 (void) blkid_probe_lookup_value(b
, "TYPE", &fstype
, NULL
);
172 (void) blkid_probe_lookup_value(b
, "UUID", &uuid
, NULL
);
176 r
= sd_id128_from_string(uuid
, &id
);
180 r
= strdup_to(ret_fstype
, fstype
);
187 static int probe_file_system_by_path(const char *path
, char **ret_fstype
, sd_id128_t
*ret_uuid
) {
188 _cleanup_close_
int fd
= -EBADF
;
190 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
192 return negative_errno();
194 return probe_file_system_by_fd(fd
, ret_fstype
, ret_uuid
);
197 static int block_get_size_by_fd(int fd
, uint64_t *ret
) {
203 if (fstat(fd
, &st
) < 0)
206 if (!S_ISBLK(st
.st_mode
))
209 return blockdev_get_device_size(fd
, ret
);
212 static int block_get_size_by_path(const char *path
, uint64_t *ret
) {
213 _cleanup_close_
int fd
= -EBADF
;
215 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
219 return block_get_size_by_fd(fd
, ret
);
222 static int run_fsck(const char *node
, const char *fstype
) {
229 r
= fsck_exists_for_fstype(fstype
);
231 return log_error_errno(r
, "Failed to check if fsck for file system %s exists: %m", fstype
);
233 log_warning("No fsck for file system %s installed, ignoring.", fstype
);
237 r
= safe_fork("(fsck)",
238 FORK_RESET_SIGNALS
|FORK_RLIMIT_NOFILE_SAFE
|FORK_DEATHSIG_SIGTERM
|FORK_LOG
|FORK_STDOUT_TO_STDERR
|FORK_CLOSE_ALL_FDS
,
244 execlp("fsck", "fsck", "-aTl", node
, NULL
);
246 log_error_errno(errno
, "Failed to execute fsck: %m");
247 _exit(FSCK_OPERATIONAL_ERROR
);
250 exit_status
= wait_for_terminate_and_check("fsck", fsck_pid
, WAIT_LOG_ABNORMAL
);
253 if ((exit_status
& ~FSCK_ERROR_CORRECTED
) != 0) {
254 log_warning("fsck failed with exit status %i.", exit_status
);
256 if ((exit_status
& (FSCK_SYSTEM_SHOULD_REBOOT
|FSCK_ERRORS_LEFT_UNCORRECTED
)) != 0)
257 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "File system is corrupted, refusing.");
259 log_warning("Ignoring fsck error.");
262 log_info("File system check completed.");
267 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(key_serial_t
, keyring_unlink
, -1);
269 static int upload_to_keyring(UserRecord
*h
, const void *vk
, size_t vks
, key_serial_t
*ret
) {
271 _cleanup_free_
char *name
= NULL
;
278 /* We upload the LUKS volume key into the kernel session keyring, under the assumption that
279 * systemd-homed gets its own private session keyring (i.e. the default service behavior, given
280 * that KeyringMode=private is the default). That way, the key will survive between invocations
281 * of systemd-homework. */
283 name
= strjoin("homework-user-", h
->user_name
);
287 serial
= add_key("user", name
, vk
, vks
, KEY_SPEC_SESSION_KEYRING
);
296 static int luks_try_passwords(
298 struct crypt_device
*cd
,
301 size_t *volume_key_size
) {
308 assert(volume_key_size
);
310 STRV_FOREACH(pp
, passwords
) {
311 size_t vks
= *volume_key_size
;
313 r
= sym_crypt_volume_key_get(
321 *volume_key_size
= vks
;
325 log_debug_errno(r
, "Password %zu didn't work for unlocking LUKS superblock: %m", (size_t) (pp
- passwords
));
331 static int luks_get_volume_key(
333 struct crypt_device
*cd
,
334 const PasswordCache
*cache
,
336 size_t *volume_key_size
,
337 key_serial_t
*ret_key_serial
) {
346 assert(volume_key_size
);
348 if (cache
&& cache
->volume_key
) {
349 /* Shortcut: If volume key was loaded from the keyring then just use it */
350 if (cache
->volume_key_size
> *volume_key_size
)
351 return log_error_errno(SYNTHETIC_ERRNO(ENOBUFS
),
352 "LUKS volume key from kernel keyring too big for buffer (need %zu bytes, have %zu).",
353 cache
->volume_key_size
, *volume_key_size
);
354 memcpy(volume_key
, cache
->volume_key
, cache
->volume_key_size
);
355 *volume_key_size
= cache
->volume_key_size
;
357 *ret_key_serial
= -1; /* Key came from keyring. No need to re-upload it */
361 vks
= *volume_key_size
;
363 FOREACH_ARGUMENT(list
,
364 cache
? cache
->pkcs11_passwords
: NULL
,
365 cache
? cache
->fido2_passwords
: NULL
,
368 r
= luks_try_passwords(h
, cd
, list
, volume_key
, &vks
);
374 /* We got a volume key! */
376 if (ret_key_serial
) {
377 r
= upload_to_keyring(h
, volume_key
, vks
, ret_key_serial
);
379 log_warning_errno(r
, "Failed to upload LUKS volume key to kernel keyring, ignoring: %m");
380 *ret_key_serial
= -1;
384 *volume_key_size
= vks
;
391 static int luks_setup(
397 const char *cipher_mode
,
398 uint64_t volume_key_size
,
399 const PasswordCache
*cache
,
401 struct crypt_device
**ret
,
402 sd_id128_t
*ret_found_uuid
,
403 void **ret_volume_key
,
404 size_t *ret_volume_key_size
,
405 key_serial_t
*ret_key_serial
) {
407 _cleanup_(keyring_unlinkp
) key_serial_t key_serial
= -1;
408 _cleanup_(sym_crypt_freep
) struct crypt_device
*cd
= NULL
;
409 _cleanup_(erase_and_freep
) void *vk
= NULL
;
419 r
= sym_crypt_init(&cd
, node
);
421 return log_error_errno(r
, "Failed to allocate libcryptsetup context: %m");
423 cryptsetup_enable_logging(cd
);
425 r
= sym_crypt_load(cd
, CRYPT_LUKS2
, NULL
);
427 return log_error_errno(r
, "Failed to load LUKS superblock: %m");
429 r
= sym_crypt_get_volume_key_size(cd
);
431 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to determine LUKS volume key size.");
434 if (!sd_id128_is_null(uuid
) || ret_found_uuid
) {
437 s
= sym_crypt_get_uuid(cd
);
439 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE
), "LUKS superblock has no UUID.");
441 r
= sd_id128_from_string(s
, &p
);
443 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE
), "LUKS superblock has invalid UUID.");
445 /* Check that the UUID matches, if specified */
446 if (!sd_id128_is_null(uuid
) &&
447 !sd_id128_equal(uuid
, p
))
448 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE
), "LUKS superblock has wrong UUID.");
451 if (cipher
&& !streq_ptr(cipher
, sym_crypt_get_cipher(cd
)))
452 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE
), "LUKS superblock declares wrong cipher.");
454 if (cipher_mode
&& !streq_ptr(cipher_mode
, sym_crypt_get_cipher_mode(cd
)))
455 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE
), "LUKS superblock declares wrong cipher mode.");
457 if (volume_key_size
!= UINT64_MAX
&& vks
!= volume_key_size
)
458 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE
), "LUKS superblock declares wrong volume key size.");
464 r
= luks_get_volume_key(h
, cd
, cache
, vk
, &vks
, ret_key_serial
? &key_serial
: NULL
);
466 return log_error_errno(r
, "No valid password for LUKS superblock.");
468 return log_error_errno(r
, "Failed to unlock LUKS superblock: %m");
470 r
= sym_crypt_activate_by_volume_key(
474 discard
? CRYPT_ACTIVATE_ALLOW_DISCARDS
: 0);
476 return log_error_errno(r
, "Failed to unlock LUKS superblock: %m");
478 log_info("Setting up LUKS device /dev/mapper/%s completed.", dm_name
);
482 if (ret_found_uuid
) /* Return the UUID actually found if the caller wants to know */
485 *ret_volume_key
= TAKE_PTR(vk
);
486 if (ret_volume_key_size
)
487 *ret_volume_key_size
= vks
;
489 *ret_key_serial
= TAKE_KEY_SERIAL(key_serial
);
494 static int make_dm_names(UserRecord
*h
, HomeSetup
*setup
) {
496 assert(h
->user_name
);
499 if (!setup
->dm_name
) {
500 setup
->dm_name
= strjoin("home-", h
->user_name
);
505 if (!setup
->dm_node
) {
506 setup
->dm_node
= path_join("/dev/mapper/", setup
->dm_name
);
514 static int acquire_open_luks_device(
519 _cleanup_(sym_crypt_freep
) struct crypt_device
*cd
= NULL
;
524 assert(!setup
->crypt_device
);
526 r
= dlopen_cryptsetup();
530 r
= make_dm_names(h
, setup
);
534 r
= sym_crypt_init_by_name(&cd
, setup
->dm_name
);
535 if ((ERRNO_IS_NEG_DEVICE_ABSENT(r
) || r
== -EINVAL
) && graceful
)
538 return log_error_errno(r
, "Failed to initialize cryptsetup context for %s: %m", setup
->dm_name
);
540 cryptsetup_enable_logging(cd
);
542 setup
->crypt_device
= TAKE_PTR(cd
);
546 static int luks_open(
549 const PasswordCache
*cache
,
550 sd_id128_t
*ret_found_uuid
,
551 void **ret_volume_key
,
552 size_t *ret_volume_key_size
) {
554 _cleanup_(erase_and_freep
) void *vk
= NULL
;
561 assert(!setup
->crypt_device
);
563 /* Opens a LUKS device that is already set up. Re-validates the password while doing so (which also
564 * provides us with the volume key, which we want). */
566 r
= acquire_open_luks_device(h
, setup
, /* graceful= */ false);
570 r
= sym_crypt_load(setup
->crypt_device
, CRYPT_LUKS2
, NULL
);
572 return log_error_errno(r
, "Failed to load LUKS superblock: %m");
574 r
= sym_crypt_get_volume_key_size(setup
->crypt_device
);
576 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to determine LUKS volume key size.");
579 if (ret_found_uuid
) {
582 s
= sym_crypt_get_uuid(setup
->crypt_device
);
584 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE
), "LUKS superblock has no UUID.");
586 r
= sd_id128_from_string(s
, &p
);
588 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE
), "LUKS superblock has invalid UUID.");
595 r
= luks_get_volume_key(h
, setup
->crypt_device
, cache
, vk
, &vks
, NULL
);
597 return log_error_errno(r
, "No valid password for LUKS superblock.");
599 return log_error_errno(r
, "Failed to unlock LUKS superblock: %m");
601 log_info("Discovered used LUKS device /dev/mapper/%s, and validated password.", setup
->dm_name
);
603 /* This is needed so that crypt_resize() can operate correctly for pre-existing LUKS devices. We need
604 * to tell libcryptsetup the volume key explicitly, so that it is in the kernel keyring. */
605 r
= sym_crypt_activate_by_volume_key(setup
->crypt_device
, NULL
, vk
, vks
, CRYPT_ACTIVATE_KEYRING_KEY
);
607 return log_error_errno(r
, "Failed to upload volume key again: %m");
609 log_info("Successfully re-activated LUKS device.");
614 *ret_volume_key
= TAKE_PTR(vk
);
615 if (ret_volume_key_size
)
616 *ret_volume_key_size
= vks
;
621 static int fs_validate(
625 sd_id128_t
*ret_found_uuid
) {
627 _cleanup_free_
char *fstype
= NULL
;
628 sd_id128_t u
= SD_ID128_NULL
; /* avoid false maybe-unitialized warning */
634 r
= probe_file_system_by_path(dm_node
, &fstype
, &u
);
636 return log_error_errno(r
, "Failed to probe file system: %m");
638 /* Limit the set of supported file systems a bit, as protection against little tested kernel file
639 * systems. Also, we only support the resize ioctls for these file systems. */
640 if (!supported_fstype(fstype
))
641 return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT
), "Image contains unsupported file system: %s", strna(fstype
));
643 if (!sd_id128_is_null(uuid
) &&
644 !sd_id128_equal(uuid
, u
))
645 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE
), "File system has wrong UUID.");
647 log_info("Probing file system completed (found %s).", fstype
);
649 *ret_fstype
= TAKE_PTR(fstype
);
651 if (ret_found_uuid
) /* Return the UUID actually found if the caller wants to know */
657 static int luks_validate(
660 sd_id128_t partition_uuid
,
661 sd_id128_t
*ret_partition_uuid
,
662 uint64_t *ret_offset
,
663 uint64_t *ret_size
) {
665 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
666 sd_id128_t found_partition_uuid
= SD_ID128_NULL
;
667 const char *fstype
= NULL
, *pttype
= NULL
;
668 blkid_loff_t offset
= 0, size
= 0;
678 b
= blkid_new_probe();
683 r
= blkid_probe_set_device(b
, fd
, 0, 0);
685 return errno_or_else(ENOMEM
);
687 (void) blkid_probe_enable_superblocks(b
, 1);
688 (void) blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
689 (void) blkid_probe_enable_partitions(b
, 1);
690 (void) blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
693 r
= blkid_do_safeprobe(b
);
694 if (r
== _BLKID_SAFEPROBE_ERROR
)
695 return errno_or_else(EIO
);
696 if (IN_SET(r
, _BLKID_SAFEPROBE_AMBIGUOUS
, _BLKID_SAFEPROBE_NOT_FOUND
))
699 assert(r
== _BLKID_SAFEPROBE_FOUND
);
701 (void) blkid_probe_lookup_value(b
, "TYPE", &fstype
, NULL
);
702 if (streq_ptr(fstype
, "crypto_LUKS")) {
703 /* Directly a LUKS image */
705 *ret_size
= UINT64_MAX
; /* full disk */
706 *ret_partition_uuid
= SD_ID128_NULL
;
711 (void) blkid_probe_lookup_value(b
, "PTTYPE", &pttype
, NULL
);
712 if (!streq_ptr(pttype
, "gpt"))
716 pl
= blkid_probe_get_partitions(b
);
718 return errno_or_else(ENOMEM
);
721 n
= blkid_partlist_numof_partitions(pl
);
723 return errno_or_else(EIO
);
725 for (int i
= 0; i
< n
; i
++) {
726 sd_id128_t id
= SD_ID128_NULL
;
730 pp
= blkid_partlist_get_partition(pl
, i
);
732 return errno_or_else(EIO
);
734 if (sd_id128_string_equal(blkid_partition_get_type_string(pp
), SD_GPT_USER_HOME
) <= 0)
737 if (!streq_ptr(blkid_partition_get_name(pp
), label
))
740 r
= blkid_partition_get_uuid_id128(pp
, &id
);
742 log_debug_errno(r
, "Failed to read partition UUID, ignoring: %m");
743 else if (!sd_id128_is_null(partition_uuid
) && !sd_id128_equal(id
, partition_uuid
))
749 offset
= blkid_partition_get_start(pp
);
750 size
= blkid_partition_get_size(pp
);
751 found_partition_uuid
= id
;
761 if ((uint64_t) offset
> UINT64_MAX
/ 512U)
765 if ((uint64_t) size
> UINT64_MAX
/ 512U)
768 *ret_offset
= offset
* 512U;
769 *ret_size
= size
* 512U;
770 *ret_partition_uuid
= found_partition_uuid
;
775 static int crypt_device_to_evp_cipher(struct crypt_device
*cd
, const EVP_CIPHER
**ret
) {
776 _cleanup_free_
char *cipher_name
= NULL
;
777 const char *cipher
, *cipher_mode
, *e
;
778 size_t key_size
, key_bits
;
779 const EVP_CIPHER
*cc
;
784 /* Let's find the right OpenSSL EVP_CIPHER object that matches the encryption settings of the LUKS
787 cipher
= sym_crypt_get_cipher(cd
);
789 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Cannot get cipher from LUKS device.");
791 cipher_mode
= sym_crypt_get_cipher_mode(cd
);
793 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Cannot get cipher mode from LUKS device.");
795 e
= strchr(cipher_mode
, '-');
797 cipher_mode
= strndupa_safe(cipher_mode
, e
- cipher_mode
);
799 r
= sym_crypt_get_volume_key_size(cd
);
801 return log_error_errno(r
< 0 ? r
: SYNTHETIC_ERRNO(EINVAL
), "Cannot get volume key size from LUKS device.");
804 key_bits
= key_size
* 8;
805 if (streq(cipher_mode
, "xts"))
808 if (asprintf(&cipher_name
, "%s-%zu-%s", cipher
, key_bits
, cipher_mode
) < 0)
811 cc
= EVP_get_cipherbyname(cipher_name
);
813 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "Selected cipher mode '%s' not supported, can't encrypt JSON record.", cipher_name
);
815 /* Verify that our key length calculations match what OpenSSL thinks */
816 r
= EVP_CIPHER_key_length(cc
);
817 if (r
< 0 || (uint64_t) r
!= key_size
)
818 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Key size of selected cipher doesn't meet our expectations.");
824 static int luks_validate_home_record(
825 struct crypt_device
*cd
,
827 const void *volume_key
,
828 PasswordCache
*cache
,
829 UserRecord
**ret_luks_home_record
) {
836 for (int token
= 0; token
< sym_crypt_token_max(CRYPT_LUKS2
); token
++) {
837 _cleanup_(sd_json_variant_unrefp
) sd_json_variant
*v
= NULL
, *rr
= NULL
;
838 _cleanup_(EVP_CIPHER_CTX_freep
) EVP_CIPHER_CTX
*context
= NULL
;
839 _cleanup_(user_record_unrefp
) UserRecord
*lhr
= NULL
;
840 _cleanup_free_
void *encrypted
= NULL
, *iv
= NULL
;
841 size_t decrypted_size
, encrypted_size
, iv_size
;
842 int decrypted_size_out1
, decrypted_size_out2
;
843 _cleanup_free_
char *decrypted
= NULL
;
844 const char *text
, *type
;
845 crypt_token_info state
;
846 sd_json_variant
*jr
, *jiv
;
847 const EVP_CIPHER
*cc
;
849 state
= sym_crypt_token_status(cd
, token
, &type
);
850 if (state
== CRYPT_TOKEN_INACTIVE
) /* First unconfigured token, give up */
852 if (IN_SET(state
, CRYPT_TOKEN_INTERNAL
, CRYPT_TOKEN_INTERNAL_UNKNOWN
, CRYPT_TOKEN_EXTERNAL
))
854 if (state
!= CRYPT_TOKEN_EXTERNAL_UNKNOWN
)
855 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unexpected token state of token %i: %i", token
, (int) state
);
857 if (!streq(type
, "systemd-homed"))
860 r
= sym_crypt_token_json_get(cd
, token
, &text
);
862 return log_error_errno(r
, "Failed to read LUKS token %i: %m", token
);
864 unsigned line
= 0, column
= 0;
865 r
= sd_json_parse(text
, SD_JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
867 return log_error_errno(r
, "Failed to parse LUKS token JSON data %u:%u: %m", line
, column
);
869 jr
= sd_json_variant_by_key(v
, "record");
871 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "LUKS token lacks 'record' field.");
872 jiv
= sd_json_variant_by_key(v
, "iv");
874 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "LUKS token lacks 'iv' field.");
876 r
= sd_json_variant_unbase64(jr
, &encrypted
, &encrypted_size
);
878 return log_error_errno(r
, "Failed to base64 decode record: %m");
880 r
= sd_json_variant_unbase64(jiv
, &iv
, &iv_size
);
882 return log_error_errno(r
, "Failed to base64 decode IV: %m");
884 r
= crypt_device_to_evp_cipher(cd
, &cc
);
887 if (iv_size
> INT_MAX
|| EVP_CIPHER_iv_length(cc
) != (int) iv_size
)
888 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "IV size doesn't match.");
890 context
= EVP_CIPHER_CTX_new();
894 if (EVP_DecryptInit_ex(context
, cc
, NULL
, volume_key
, iv
) != 1)
895 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to initialize decryption context.");
897 decrypted_size
= encrypted_size
+ EVP_CIPHER_key_length(cc
) * 2;
898 decrypted
= new(char, decrypted_size
);
902 if (EVP_DecryptUpdate(context
, (uint8_t*) decrypted
, &decrypted_size_out1
, encrypted
, encrypted_size
) != 1)
903 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to decrypt JSON record.");
905 assert((size_t) decrypted_size_out1
<= decrypted_size
);
907 if (EVP_DecryptFinal_ex(context
, (uint8_t*) decrypted
+ decrypted_size_out1
, &decrypted_size_out2
) != 1)
908 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to finish decryption of JSON record.");
910 assert((size_t) decrypted_size_out1
+ (size_t) decrypted_size_out2
< decrypted_size
);
911 decrypted_size
= (size_t) decrypted_size_out1
+ (size_t) decrypted_size_out2
;
913 if (memchr(decrypted
, 0, decrypted_size
))
914 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Inner NUL byte in JSON record, refusing.");
916 decrypted
[decrypted_size
] = 0;
918 r
= sd_json_parse(decrypted
, SD_JSON_PARSE_SENSITIVE
, &rr
, NULL
, NULL
);
920 return log_error_errno(r
, "Failed to parse decrypted JSON record, refusing.");
922 lhr
= user_record_new();
926 r
= user_record_load(lhr
, rr
, USER_RECORD_LOAD_EMBEDDED
|USER_RECORD_PERMISSIVE
);
928 return log_error_errno(r
, "Failed to parse user record: %m");
930 if (!user_record_compatible(h
, lhr
))
931 return log_error_errno(SYNTHETIC_ERRNO(EREMCHG
), "LUKS home record not compatible with host record, refusing.");
933 r
= user_record_authenticate(lhr
, h
, cache
, /* strict_verify= */ true);
936 assert(r
> 0); /* Insist that a password was verified */
938 *ret_luks_home_record
= TAKE_PTR(lhr
);
942 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Couldn't find home record in LUKS2 header, refusing.");
945 static int format_luks_token_text(
946 struct crypt_device
*cd
,
948 const void *volume_key
,
951 int r
, encrypted_size_out1
= 0, encrypted_size_out2
= 0, iv_size
, key_size
;
952 _cleanup_(EVP_CIPHER_CTX_freep
) EVP_CIPHER_CTX
*context
= NULL
;
953 _cleanup_(sd_json_variant_unrefp
) sd_json_variant
*v
= NULL
;
954 _cleanup_free_
void *iv
= NULL
, *encrypted
= NULL
;
955 size_t text_length
, encrypted_size
;
956 _cleanup_free_
char *text
= NULL
;
957 const EVP_CIPHER
*cc
;
964 r
= crypt_device_to_evp_cipher(cd
, &cc
);
968 key_size
= EVP_CIPHER_key_length(cc
);
969 iv_size
= EVP_CIPHER_iv_length(cc
);
972 iv
= malloc(iv_size
);
976 r
= crypto_random_bytes(iv
, iv_size
);
978 return log_error_errno(r
, "Failed to generate IV: %m");
981 context
= EVP_CIPHER_CTX_new();
985 if (EVP_EncryptInit_ex(context
, cc
, NULL
, volume_key
, iv
) != 1)
986 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to initialize encryption context.");
988 r
= sd_json_variant_format(hr
->json
, 0, &text
);
990 return log_error_errno(r
, "Failed to format user record for LUKS: %m");
992 text_length
= strlen(text
);
993 encrypted_size
= text_length
+ 2*key_size
- 1;
995 encrypted
= malloc(encrypted_size
);
999 if (EVP_EncryptUpdate(context
, encrypted
, &encrypted_size_out1
, (uint8_t*) text
, text_length
) != 1)
1000 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to encrypt JSON record.");
1002 assert((size_t) encrypted_size_out1
<= encrypted_size
);
1004 if (EVP_EncryptFinal_ex(context
, (uint8_t*) encrypted
+ encrypted_size_out1
, &encrypted_size_out2
) != 1)
1005 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to finish encryption of JSON record.");
1007 assert((size_t) encrypted_size_out1
+ (size_t) encrypted_size_out2
<= encrypted_size
);
1011 SD_JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-homed")),
1012 SD_JSON_BUILD_PAIR("keyslots", SD_JSON_BUILD_EMPTY_ARRAY
),
1013 SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_BASE64(encrypted
, encrypted_size_out1
+ encrypted_size_out2
)),
1014 SD_JSON_BUILD_PAIR("iv", SD_JSON_BUILD_BASE64(iv
, iv_size
)));
1016 return log_error_errno(r
, "Failed to prepare LUKS JSON token object: %m");
1018 r
= sd_json_variant_format(v
, 0, ret
);
1020 return log_error_errno(r
, "Failed to format encrypted user record for LUKS: %m");
1025 int home_store_header_identity_luks(
1028 UserRecord
*old_home
) {
1030 _cleanup_(user_record_unrefp
) UserRecord
*header_home
= NULL
;
1031 _cleanup_free_
char *text
= NULL
;
1036 if (!setup
->crypt_device
)
1039 assert(setup
->volume_key
);
1041 /* Let's store the user's identity record in the LUKS2 "token" header data fields, in an encrypted
1042 * fashion. Why that? If we'd rely on the record being embedded in the payload file system itself we
1043 * would have to mount the file system before we can validate the JSON record, its signatures and
1044 * whether it matches what we are looking for. However, kernel file system implementations are
1045 * generally not ready to be used on untrusted media. Hence let's store the record independently of
1046 * the file system, so that we can validate it first, and only then mount the file system. To keep
1047 * things simple we use the same encryption settings for this record as for the file system itself. */
1049 r
= user_record_clone(h
, USER_RECORD_EXTRACT_EMBEDDED
|USER_RECORD_PERMISSIVE
, &header_home
);
1051 return log_error_errno(r
, "Failed to determine new header record: %m");
1053 if (old_home
&& user_record_equal(old_home
, header_home
)) {
1054 log_debug("Not updating header home record.");
1058 r
= format_luks_token_text(setup
->crypt_device
, header_home
, setup
->volume_key
, &text
);
1062 for (int token
= 0; token
< sym_crypt_token_max(CRYPT_LUKS2
); token
++) {
1063 crypt_token_info state
;
1066 state
= sym_crypt_token_status(setup
->crypt_device
, token
, &type
);
1067 if (state
== CRYPT_TOKEN_INACTIVE
) /* First unconfigured token, we are done */
1069 if (IN_SET(state
, CRYPT_TOKEN_INTERNAL
, CRYPT_TOKEN_INTERNAL_UNKNOWN
, CRYPT_TOKEN_EXTERNAL
))
1070 continue; /* Not ours */
1071 if (state
!= CRYPT_TOKEN_EXTERNAL_UNKNOWN
)
1072 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unexpected token state of token %i: %i", token
, (int) state
);
1074 if (!streq(type
, "systemd-homed"))
1077 r
= sym_crypt_token_json_set(setup
->crypt_device
, token
, text
);
1079 return log_error_errno(r
, "Failed to set JSON token for slot %i: %m", token
);
1081 /* Now, let's free the text so that for all further matching tokens we all crypt_json_token_set()
1082 * with a NULL text in order to invalidate the tokens. */
1087 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Didn't find any record token to update.");
1089 log_info("Wrote LUKS header user record.");
1094 int run_fitrim(int root_fd
) {
1095 struct fstrim_range range
= {
1099 /* If discarding is on, discard everything right after mounting, so that the discard setting takes
1100 * effect on activation. (Also, optionally, trim on logout) */
1102 assert(root_fd
>= 0);
1104 if (ioctl(root_fd
, FITRIM
, &range
) < 0) {
1105 if (ERRNO_IS_NOT_SUPPORTED(errno
) || errno
== EBADF
) {
1106 log_debug_errno(errno
, "File system does not support FITRIM, not trimming.");
1110 return log_warning_errno(errno
, "Failed to invoke FITRIM, ignoring: %m");
1113 log_info("Discarded unused %s.", FORMAT_BYTES(range
.len
));
1117 int run_fallocate(int backing_fd
, const struct stat
*st
) {
1120 assert(backing_fd
>= 0);
1122 /* If discarding is off, let's allocate the whole image before mounting, so that the setting takes
1123 * effect on activation */
1126 if (fstat(backing_fd
, &stbuf
) < 0)
1127 return log_error_errno(errno
, "Failed to fstat(): %m");
1132 if (!S_ISREG(st
->st_mode
))
1135 if (st
->st_blocks
>= DIV_ROUND_UP(st
->st_size
, 512)) {
1136 log_info("Backing file is fully allocated already.");
1140 if (fallocate(backing_fd
, FALLOC_FL_KEEP_SIZE
, 0, st
->st_size
) < 0) {
1142 if (ERRNO_IS_NOT_SUPPORTED(errno
)) {
1143 log_debug_errno(errno
, "fallocate() not supported on file system, ignoring.");
1147 if (ERRNO_IS_DISK_SPACE(errno
)) {
1148 log_debug_errno(errno
, "Not enough disk space to fully allocate home.");
1149 return -ENOSPC
; /* make recognizable */
1152 return log_error_errno(errno
, "Failed to allocate backing file blocks: %m");
1155 log_info("Allocated additional %s.",
1156 FORMAT_BYTES((DIV_ROUND_UP(st
->st_size
, 512) - st
->st_blocks
) * 512));
1160 int run_fallocate_by_path(const char *backing_path
) {
1161 _cleanup_close_
int backing_fd
= -EBADF
;
1163 backing_fd
= open(backing_path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
1165 return log_error_errno(errno
, "Failed to open '%s' for fallocate(): %m", backing_path
);
1167 return run_fallocate(backing_fd
, NULL
);
1170 static int lock_image_fd(int image_fd
, const char *ip
) {
1173 /* If the $SYSTEMD_LUKS_LOCK environment variable is set we'll take an exclusive BSD lock on the
1174 * image file, and send it to our parent. homed will keep it open to ensure no other instance of
1175 * homed (across the network or such) will also mount the file. */
1177 assert(image_fd
>= 0);
1180 r
= getenv_bool("SYSTEMD_LUKS_LOCK");
1184 return log_error_errno(r
, "Failed to parse $SYSTEMD_LUKS_LOCK environment variable: %m");
1188 if (flock(image_fd
, LOCK_EX
|LOCK_NB
) < 0) {
1190 if (errno
== EAGAIN
)
1191 log_error_errno(errno
, "Image file '%s' already locked, can't use.", ip
);
1193 log_error_errno(errno
, "Failed to lock image file '%s': %m", ip
);
1195 return errno
!= EAGAIN
? -errno
: -EADDRINUSE
; /* Make error recognizable */
1198 log_info("Successfully locked image file '%s'.", ip
);
1200 /* Now send it to our parent to keep safe while the home dir is active */
1201 r
= sd_pid_notify_with_fds(0, false, "SYSTEMD_LUKS_LOCK_FD=1", &image_fd
, 1);
1203 log_warning_errno(r
, "Failed to send LUKS lock fd to parent, ignoring: %m");
1208 static int open_image_file(
1210 const char *force_image_path
,
1211 struct stat
*ret_stat
) {
1213 _cleanup_close_
int image_fd
= -EBADF
;
1218 assert(h
|| force_image_path
);
1220 ip
= force_image_path
?: user_record_image_path(h
);
1222 image_fd
= open(ip
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
1224 return log_error_errno(errno
, "Failed to open image file %s: %m", ip
);
1226 if (fstat(image_fd
, &st
) < 0)
1227 return log_error_errno(errno
, "Failed to fstat() image file: %m");
1228 if (!S_ISREG(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
1229 return log_error_errno(
1230 S_ISDIR(st
.st_mode
) ? SYNTHETIC_ERRNO(EISDIR
) : SYNTHETIC_ERRNO(EBADFD
),
1231 "Image file %s is not a regular file or block device: %m", ip
);
1233 /* Locking block devices doesn't really make sense, as this might interfere with
1234 * udev's workings, and these locks aren't network propagated anyway, hence not what
1235 * we are after here. */
1236 if (S_ISREG(st
.st_mode
)) {
1237 r
= lock_image_fd(image_fd
, ip
);
1245 return TAKE_FD(image_fd
);
1248 int home_setup_luks(
1250 HomeSetupFlags flags
,
1251 const char *force_image_path
,
1253 PasswordCache
*cache
,
1254 UserRecord
**ret_luks_home
) {
1256 sd_id128_t found_partition_uuid
, found_fs_uuid
= SD_ID128_NULL
, found_luks_uuid
= SD_ID128_NULL
;
1257 _cleanup_(user_record_unrefp
) UserRecord
*luks_home
= NULL
;
1258 _cleanup_(erase_and_freep
) void *volume_key
= NULL
;
1259 size_t volume_key_size
= 0;
1260 uint64_t offset
, size
;
1266 assert(user_record_storage(h
) == USER_LUKS
);
1268 r
= dlopen_cryptsetup();
1272 r
= make_dm_names(h
, setup
);
1276 /* Reuse the image fd if it has already been opened by an earlier step */
1277 if (setup
->image_fd
< 0) {
1278 setup
->image_fd
= open_image_file(h
, force_image_path
, &st
);
1279 if (setup
->image_fd
< 0)
1280 return setup
->image_fd
;
1281 } else if (fstat(setup
->image_fd
, &st
) < 0)
1282 return log_error_errno(errno
, "Failed to stat image: %m");
1284 if (FLAGS_SET(flags
, HOME_SETUP_ALREADY_ACTIVATED
)) {
1285 struct loop_info64 info
;
1288 if (!setup
->crypt_device
) {
1299 if (ret_luks_home
) {
1300 r
= luks_validate_home_record(setup
->crypt_device
, h
, volume_key
, cache
, &luks_home
);
1305 n
= sym_crypt_get_device_name(setup
->crypt_device
);
1307 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to determine backing device for DM %s.", setup
->dm_name
);
1310 r
= loop_device_open_from_path(n
, O_RDWR
, LOCK_UN
, &setup
->loop
);
1312 return log_error_errno(r
, "Failed to open loopback device %s: %m", n
);
1315 if (ioctl(setup
->loop
->fd
, LOOP_GET_STATUS64
, &info
) < 0) {
1316 _cleanup_free_
char *sysfs
= NULL
;
1318 if (!IN_SET(errno
, ENOTTY
, EINVAL
))
1319 return log_error_errno(errno
, "Failed to get block device metrics of %s: %m", n
);
1321 if (fstat(setup
->loop
->fd
, &st
) < 0)
1322 return log_error_errno(r
, "Failed to stat block device %s: %m", n
);
1323 assert(S_ISBLK(st
.st_mode
));
1325 if (asprintf(&sysfs
, "/sys/dev/block/" DEVNUM_FORMAT_STR
"/partition", DEVNUM_FORMAT_VAL(st
.st_rdev
)) < 0)
1328 if (access(sysfs
, F_OK
) < 0) {
1329 if (errno
!= ENOENT
)
1330 return log_error_errno(errno
, "Failed to determine whether %s exists: %m", sysfs
);
1334 _cleanup_free_
char *buffer
= NULL
;
1336 if (asprintf(&sysfs
, "/sys/dev/block/" DEVNUM_FORMAT_STR
"/start", DEVNUM_FORMAT_VAL(st
.st_rdev
)) < 0)
1339 r
= read_one_line_file(sysfs
, &buffer
);
1341 return log_error_errno(r
, "Failed to read partition start offset: %m");
1343 r
= safe_atou64(buffer
, &offset
);
1345 return log_error_errno(r
, "Failed to parse partition start offset: %m");
1347 if (offset
> UINT64_MAX
/ 512U)
1348 return log_error_errno(SYNTHETIC_ERRNO(E2BIG
), "Offset too large for 64 byte range, refusing.");
1353 size
= setup
->loop
->device_size
;
1355 #if HAVE_VALGRIND_MEMCHECK_H
1356 VALGRIND_MAKE_MEM_DEFINED(&info
, sizeof(info
));
1359 offset
= info
.lo_offset
;
1360 size
= info
.lo_sizelimit
;
1363 found_partition_uuid
= found_fs_uuid
= SD_ID128_NULL
;
1365 log_info("Discovered used loopback device %s.", setup
->loop
->node
);
1367 if (setup
->root_fd
< 0) {
1368 setup
->root_fd
= open(user_record_home_directory(h
), O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1369 if (setup
->root_fd
< 0)
1370 return log_error_errno(errno
, "Failed to open home directory: %m");
1373 _cleanup_free_
char *fstype
= NULL
, *subdir
= NULL
;
1376 /* When we aren't reopening the home directory we are allocating it fresh, hence the relevant
1377 * objects can't be allocated yet. */
1378 assert(setup
->root_fd
< 0);
1379 assert(!setup
->crypt_device
);
1380 assert(!setup
->loop
);
1382 ip
= force_image_path
?: user_record_image_path(h
);
1384 subdir
= path_join(HOME_RUNTIME_WORK_DIR
, user_record_user_name_and_realm(h
));
1388 r
= luks_validate(setup
->image_fd
, user_record_user_name_and_realm(h
), h
->partition_uuid
, &found_partition_uuid
, &offset
, &size
);
1390 return log_error_errno(r
, "Failed to validate disk label: %m");
1392 /* Everything before this point left the image untouched. We are now starting to make
1393 * changes, hence mark the image dirty */
1394 if (run_mark_dirty(setup
->image_fd
, true) > 0)
1395 setup
->do_mark_clean
= true;
1397 if (!user_record_luks_discard(h
)) {
1398 r
= run_fallocate(setup
->image_fd
, &st
);
1403 r
= loop_device_make(
1408 h
->luks_sector_size
== UINT64_MAX
? UINT32_MAX
: user_record_luks_sector_size(h
), /* if sector size is not specified, select UINT32_MAX, i.e. auto-probe */
1409 /* loop_flags= */ 0,
1413 log_error_errno(r
, "Loopback block device support is not available on this system.");
1414 return -ENOLINK
; /* make recognizable */
1417 return log_error_errno(r
, "Failed to allocate loopback context: %m");
1419 log_info("Setting up loopback device %s completed.", setup
->loop
->node
?: ip
);
1422 setup
->loop
->node
?: ip
,
1426 h
->luks_cipher_mode
,
1427 h
->luks_volume_key_size
,
1429 user_record_luks_discard(h
) || user_record_luks_offline_discard(h
),
1430 &setup
->crypt_device
,
1434 &setup
->key_serial
);
1438 setup
->undo_dm
= true;
1440 if (ret_luks_home
) {
1441 r
= luks_validate_home_record(setup
->crypt_device
, h
, volume_key
, cache
, &luks_home
);
1446 r
= fs_validate(setup
->dm_node
, h
->file_system_uuid
, &fstype
, &found_fs_uuid
);
1450 r
= run_fsck(setup
->dm_node
, fstype
);
1454 r
= home_unshare_and_mount(setup
->dm_node
, fstype
, user_record_luks_discard(h
), user_record_mount_flags(h
), h
->luks_extra_mount_options
);
1458 setup
->undo_mount
= true;
1460 setup
->root_fd
= open(subdir
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1461 if (setup
->root_fd
< 0)
1462 return log_error_errno(errno
, "Failed to open home directory: %m");
1464 if (user_record_luks_discard(h
))
1465 (void) run_fitrim(setup
->root_fd
);
1467 setup
->do_offline_fallocate
= !(setup
->do_offline_fitrim
= user_record_luks_offline_discard(h
));
1470 if (!sd_id128_is_null(found_partition_uuid
))
1471 setup
->found_partition_uuid
= found_partition_uuid
;
1472 if (!sd_id128_is_null(found_luks_uuid
))
1473 setup
->found_luks_uuid
= found_luks_uuid
;
1474 if (!sd_id128_is_null(found_fs_uuid
))
1475 setup
->found_fs_uuid
= found_fs_uuid
;
1477 setup
->partition_offset
= offset
;
1478 setup
->partition_size
= size
;
1481 erase_and_free(setup
->volume_key
);
1482 setup
->volume_key
= TAKE_PTR(volume_key
);
1483 setup
->volume_key_size
= volume_key_size
;
1487 *ret_luks_home
= TAKE_PTR(luks_home
);
1492 static void print_size_summary(uint64_t host_size
, uint64_t encrypted_size
, const struct statfs
*sfs
) {
1495 log_info("Image size is %s, file system size is %s, file system payload size is %s, file system free is %s.",
1496 FORMAT_BYTES(host_size
),
1497 FORMAT_BYTES(encrypted_size
),
1498 FORMAT_BYTES((uint64_t) sfs
->f_blocks
* (uint64_t) sfs
->f_frsize
),
1499 FORMAT_BYTES((uint64_t) sfs
->f_bfree
* (uint64_t) sfs
->f_frsize
));
1502 static int home_auto_grow_luks(
1505 PasswordCache
*cache
) {
1512 if (!IN_SET(user_record_auto_resize_mode(h
), AUTO_RESIZE_GROW
, AUTO_RESIZE_SHRINK_AND_GROW
))
1515 assert(setup
->root_fd
>= 0);
1517 if (fstatfs(setup
->root_fd
, &sfs
) < 0)
1518 return log_error_errno(errno
, "Failed to statfs home directory: %m");
1520 if (!fs_can_online_shrink_and_grow(sfs
.f_type
)) {
1521 log_debug("Not auto-grow file system, since selected file system cannot do both online shrink and grow.");
1525 log_debug("Initiating auto-grow...");
1527 return home_resize_luks(
1529 HOME_SETUP_ALREADY_ACTIVATED
|
1530 HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES
|
1531 HOME_SETUP_RESIZE_DONT_SHRINK
|
1532 HOME_SETUP_RESIZE_DONT_UNDO
,
1538 int home_activate_luks(
1540 HomeSetupFlags flags
,
1542 PasswordCache
*cache
,
1543 UserRecord
**ret_home
) {
1545 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
, *luks_home_record
= NULL
;
1546 uint64_t host_size
, encrypted_size
;
1547 const char *hdo
, *hd
;
1552 assert(user_record_storage(h
) == USER_LUKS
);
1556 r
= dlopen_cryptsetup();
1560 assert_se(hdo
= user_record_home_directory(h
));
1561 hd
= strdupa_safe(hdo
); /* copy the string out, since it might change later in the home record object */
1563 r
= home_get_state_luks(h
, setup
);
1567 return log_error_errno(SYNTHETIC_ERRNO(EEXIST
), "Device mapper device %s already exists, refusing.", setup
->dm_node
);
1569 r
= home_setup_luks(
1579 r
= home_auto_grow_luks(h
, setup
, cache
);
1583 r
= block_get_size_by_fd(setup
->loop
->fd
, &host_size
);
1585 return log_error_errno(r
, "Failed to get loopback block device size: %m");
1587 r
= block_get_size_by_path(setup
->dm_node
, &encrypted_size
);
1589 return log_error_errno(r
, "Failed to get LUKS block device size: %m");
1602 r
= home_extend_embedded_identity(new_home
, h
, setup
);
1606 setup
->root_fd
= safe_close(setup
->root_fd
);
1608 r
= home_move_mount(user_record_user_name_and_realm(h
), hd
);
1612 setup
->undo_mount
= false;
1613 setup
->do_offline_fitrim
= false;
1615 loop_device_relinquish(setup
->loop
);
1617 r
= sym_crypt_deactivate_by_name(NULL
, setup
->dm_name
, CRYPT_DEACTIVATE_DEFERRED
);
1619 log_warning_errno(r
, "Failed to relinquish DM device, ignoring: %m");
1621 setup
->undo_dm
= false;
1622 setup
->do_offline_fallocate
= false;
1623 setup
->do_mark_clean
= false;
1624 setup
->do_drop_caches
= false;
1625 TAKE_KEY_SERIAL(setup
->key_serial
); /* Leave key in kernel keyring */
1627 log_info("Activation completed.");
1629 print_size_summary(host_size
, encrypted_size
, &sfs
);
1631 *ret_home
= TAKE_PTR(new_home
);
1635 int home_deactivate_luks(UserRecord
*h
, HomeSetup
*setup
) {
1636 bool we_detached
= false;
1642 /* Note that the DM device and loopback device are set to auto-detach, hence strictly speaking we
1643 * don't have to explicitly have to detach them. However, we do that nonetheless (in case of the DM
1644 * device), to avoid races: by explicitly detaching them we know when the detaching is complete. We
1645 * don't bother about the loopback device because unlike the DM device it doesn't have a fixed
1648 if (!setup
->crypt_device
) {
1649 r
= acquire_open_luks_device(h
, setup
, /* graceful= */ true);
1651 return log_error_errno(r
, "Failed to initialize cryptsetup context for %s: %m", setup
->dm_name
);
1653 log_debug("LUKS device %s has already been detached.", setup
->dm_name
);
1656 if (setup
->crypt_device
) {
1657 log_info("Discovered used LUKS device %s.", setup
->dm_node
);
1659 cryptsetup_enable_logging(setup
->crypt_device
);
1661 r
= sym_crypt_deactivate_by_name(setup
->crypt_device
, setup
->dm_name
, 0);
1662 if (ERRNO_IS_NEG_DEVICE_ABSENT(r
) || r
== -EINVAL
)
1663 log_debug_errno(r
, "LUKS device %s is already detached.", setup
->dm_node
);
1665 return log_info_errno(r
, "LUKS device %s couldn't be deactivated: %m", setup
->dm_node
);
1667 log_info("LUKS device detaching completed.");
1672 (void) wait_for_block_device_gone(setup
, USEC_PER_SEC
* 30);
1673 setup
->undo_dm
= false;
1675 if (user_record_luks_offline_discard(h
))
1676 log_debug("Not allocating on logout.");
1678 (void) run_fallocate_by_path(user_record_image_path(h
));
1680 run_mark_dirty_by_path(user_record_image_path(h
), false);
1684 int home_trim_luks(UserRecord
*h
, HomeSetup
*setup
) {
1687 assert(setup
->root_fd
>= 0);
1689 if (!user_record_luks_offline_discard(h
)) {
1690 log_debug("Not trimming on logout.");
1694 (void) run_fitrim(setup
->root_fd
);
1698 static struct crypt_pbkdf_type
* build_good_pbkdf(struct crypt_pbkdf_type
*buffer
, UserRecord
*hr
) {
1702 bool benchmark
= user_record_luks_pbkdf_force_iterations(hr
) == UINT64_MAX
;
1704 *buffer
= (struct crypt_pbkdf_type
) {
1705 .hash
= user_record_luks_pbkdf_hash_algorithm(hr
),
1706 .type
= user_record_luks_pbkdf_type(hr
),
1707 .time_ms
= benchmark
? user_record_luks_pbkdf_time_cost_usec(hr
) / USEC_PER_MSEC
: 0,
1708 .iterations
= benchmark
? 0 : user_record_luks_pbkdf_force_iterations(hr
),
1709 .max_memory_kb
= user_record_luks_pbkdf_memory_cost(hr
) / 1024,
1710 .parallel_threads
= user_record_luks_pbkdf_parallel_threads(hr
),
1711 .flags
= benchmark
? 0 : CRYPT_PBKDF_NO_BENCHMARK
,
1717 static struct crypt_pbkdf_type
* build_minimal_pbkdf(struct crypt_pbkdf_type
*buffer
, UserRecord
*hr
) {
1721 /* For PKCS#11 derived keys (which are generated randomly and are of high quality already) we use a
1722 * minimal PBKDF and CRYPT_PBKDF_NO_BENCHMARK flag to skip benchmark. */
1723 *buffer
= (struct crypt_pbkdf_type
) {
1724 .hash
= user_record_luks_pbkdf_hash_algorithm(hr
),
1725 .type
= CRYPT_KDF_PBKDF2
,
1726 .iterations
= 1000, /* recommended minimum count for pbkdf2
1727 * according to NIST SP 800-132, ch. 5.2 */
1728 .flags
= CRYPT_PBKDF_NO_BENCHMARK
1734 static int luks_format(
1736 const char *dm_name
,
1739 const PasswordCache
*cache
,
1740 char **effective_passwords
,
1743 struct crypt_device
**ret
) {
1745 _cleanup_(user_record_unrefp
) UserRecord
*reduced
= NULL
;
1746 _cleanup_(sym_crypt_freep
) struct crypt_device
*cd
= NULL
;
1747 _cleanup_(erase_and_freep
) void *volume_key
= NULL
;
1748 struct crypt_pbkdf_type good_pbkdf
, minimal_pbkdf
;
1749 _cleanup_free_
char *text
= NULL
;
1750 size_t volume_key_size
;
1758 r
= sym_crypt_init(&cd
, node
);
1760 return log_error_errno(r
, "Failed to allocate libcryptsetup context: %m");
1762 cryptsetup_enable_logging(cd
);
1764 /* Normally we'd, just leave volume key generation to libcryptsetup. However, we can't, since we
1765 * can't extract the volume key from the library again, but we need it in order to encrypt the JSON
1766 * record. Hence, let's generate it on our own, so that we can keep track of it. */
1768 volume_key_size
= user_record_luks_volume_key_size(hr
);
1769 volume_key
= malloc(volume_key_size
);
1773 r
= crypto_random_bytes(volume_key
, volume_key_size
);
1775 return log_error_errno(r
, "Failed to generate volume key: %m");
1777 #if HAVE_CRYPT_SET_METADATA_SIZE
1778 /* Increase the metadata space to 4M, the largest LUKS2 supports */
1779 r
= sym_crypt_set_metadata_size(cd
, 4096U*1024U, 0);
1781 return log_error_errno(r
, "Failed to change LUKS2 metadata size: %m");
1784 build_good_pbkdf(&good_pbkdf
, hr
);
1785 build_minimal_pbkdf(&minimal_pbkdf
, hr
);
1787 r
= sym_crypt_format(
1790 user_record_luks_cipher(hr
),
1791 user_record_luks_cipher_mode(hr
),
1792 SD_ID128_TO_UUID_STRING(uuid
),
1795 &(struct crypt_params_luks2
) {
1797 .subsystem
= "systemd-home",
1798 .sector_size
= user_record_luks_sector_size(hr
),
1799 .pbkdf
= &good_pbkdf
,
1802 return log_error_errno(r
, "Failed to format LUKS image: %m");
1804 log_info("LUKS formatting completed.");
1806 STRV_FOREACH(pp
, effective_passwords
) {
1808 if (password_cache_contains(cache
, *pp
)) { /* is this a fido2 or pkcs11 password? */
1809 log_debug("Using minimal PBKDF for slot %i", slot
);
1810 r
= sym_crypt_set_pbkdf_type(cd
, &minimal_pbkdf
);
1812 log_debug("Using good PBKDF for slot %i", slot
);
1813 r
= sym_crypt_set_pbkdf_type(cd
, &good_pbkdf
);
1816 return log_error_errno(r
, "Failed to tweak PBKDF for slot %i: %m", slot
);
1818 r
= sym_crypt_keyslot_add_by_volume_key(
1826 return log_error_errno(r
, "Failed to set up LUKS password for slot %i: %m", slot
);
1828 log_info("Writing password to LUKS keyslot %i completed.", slot
);
1832 r
= sym_crypt_activate_by_volume_key(
1837 discard
? CRYPT_ACTIVATE_ALLOW_DISCARDS
: 0);
1839 return log_error_errno(r
, "Failed to activate LUKS superblock: %m");
1841 log_info("LUKS activation by volume key succeeded.");
1843 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_EMBEDDED
|USER_RECORD_PERMISSIVE
, &reduced
);
1845 return log_error_errno(r
, "Failed to prepare home record for LUKS: %m");
1847 r
= format_luks_token_text(cd
, reduced
, volume_key
, &text
);
1851 r
= sym_crypt_token_json_set(cd
, CRYPT_ANY_TOKEN
, text
);
1853 return log_error_errno(r
, "Failed to set LUKS JSON token: %m");
1855 log_info("Writing user record as LUKS token completed.");
1858 *ret
= TAKE_PTR(cd
);
1863 static int make_partition_table(
1865 uint32_t sector_size
,
1868 uint64_t *ret_offset
,
1870 sd_id128_t
*ret_disk_uuid
) {
1872 _cleanup_(fdisk_unref_partitionp
) struct fdisk_partition
*p
= NULL
, *q
= NULL
;
1873 _cleanup_(fdisk_unref_parttypep
) struct fdisk_parttype
*t
= NULL
;
1874 _cleanup_(fdisk_unref_contextp
) struct fdisk_context
*c
= NULL
;
1875 _cleanup_free_
char *disk_uuid_as_string
= NULL
;
1876 uint64_t offset
, size
, first_lba
, start
, last_lba
, end
;
1877 sd_id128_t disk_uuid
;
1885 t
= fdisk_new_parttype();
1889 r
= fdisk_parttype_set_typestr(t
, SD_GPT_USER_HOME_STR
);
1891 return log_error_errno(r
, "Failed to initialize partition type: %m");
1893 r
= fdisk_new_context_at(fd
, /* path= */ NULL
, /* read_only= */ false, sector_size
, &c
);
1895 return log_error_errno(r
, "Failed to open device: %m");
1897 r
= fdisk_create_disklabel(c
, "gpt");
1899 return log_error_errno(r
, "Failed to create GPT disk label: %m");
1901 p
= fdisk_new_partition();
1905 r
= fdisk_partition_set_type(p
, t
);
1907 return log_error_errno(r
, "Failed to set partition type: %m");
1909 r
= fdisk_partition_partno_follow_default(p
, 1);
1911 return log_error_errno(r
, "Failed to place partition at first free partition index: %m");
1913 first_lba
= fdisk_get_first_lba(c
); /* Boundary where usable space starts */
1914 assert(first_lba
<= UINT64_MAX
/512);
1915 start
= DISK_SIZE_ROUND_UP(first_lba
* 512); /* Round up to multiple of 4K */
1917 log_debug("Starting partition at offset %" PRIu64
, start
);
1919 if (start
== UINT64_MAX
)
1920 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Overflow while rounding up start LBA.");
1922 last_lba
= fdisk_get_last_lba(c
); /* One sector before boundary where usable space ends */
1923 assert(last_lba
< UINT64_MAX
/512);
1924 end
= DISK_SIZE_ROUND_DOWN((last_lba
+ 1) * 512); /* Round down to multiple of 4K */
1927 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Resulting partition size zero or negative.");
1929 r
= fdisk_partition_set_start(p
, start
/ 512);
1931 return log_error_errno(r
, "Failed to place partition at offset %" PRIu64
": %m", start
);
1933 r
= fdisk_partition_set_size(p
, (end
- start
) / 512);
1935 return log_error_errno(r
, "Failed to end partition at offset %" PRIu64
": %m", end
);
1937 r
= fdisk_partition_set_name(p
, label
);
1939 return log_error_errno(r
, "Failed to set partition name: %m");
1941 r
= fdisk_partition_set_uuid(p
, SD_ID128_TO_UUID_STRING(uuid
));
1943 return log_error_errno(r
, "Failed to set partition UUID: %m");
1945 r
= fdisk_add_partition(c
, p
, NULL
);
1947 return log_error_errno(r
, "Failed to add partition: %m");
1949 r
= fdisk_write_disklabel(c
);
1951 return log_error_errno(r
, "Failed to write disk label: %m");
1953 r
= fdisk_get_disklabel_id(c
, &disk_uuid_as_string
);
1955 return log_error_errno(r
, "Failed to determine disk label UUID: %m");
1957 r
= sd_id128_from_string(disk_uuid_as_string
, &disk_uuid
);
1959 return log_error_errno(r
, "Failed to parse disk label UUID: %m");
1961 r
= fdisk_get_partition(c
, 0, &q
);
1963 return log_error_errno(r
, "Failed to read created partition metadata: %m");
1965 assert(fdisk_partition_has_start(q
));
1966 offset
= fdisk_partition_get_start(q
);
1967 if (offset
> UINT64_MAX
/ 512U)
1968 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Partition offset too large.");
1970 assert(fdisk_partition_has_size(q
));
1971 size
= fdisk_partition_get_size(q
);
1972 if (size
> UINT64_MAX
/ 512U)
1973 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Partition size too large.");
1975 *ret_offset
= offset
* 512U;
1976 *ret_size
= size
* 512U;
1977 *ret_disk_uuid
= disk_uuid
;
1982 static bool supported_fs_size(const char *fstype
, uint64_t host_size
) {
1985 m
= minimal_size_by_fs_name(fstype
);
1986 if (m
== UINT64_MAX
)
1989 return host_size
>= m
;
1992 static int wait_for_devlink(const char *path
) {
1993 _cleanup_close_
int inotify_fd
= -EBADF
;
1997 /* let's wait for a device link to show up in /dev, with a timeout. This is good to do since we
1998 * return a /dev/disk/by-uuid/… link to our callers and they likely want to access it right-away,
1999 * hence let's wait until udev has caught up with our changes, and wait for the symlink to be
2002 until
= usec_add(now(CLOCK_MONOTONIC
), 45 * USEC_PER_SEC
);
2005 _cleanup_free_
char *dn
= NULL
;
2008 r
= access_nofollow(path
, F_OK
);
2010 return 0; /* Found it */
2012 return log_error_errno(r
, "Failed to determine whether %s exists: %m", path
);
2014 if (inotify_fd
< 0) {
2015 /* We need to wait for the device symlink to show up, let's create an inotify watch for it */
2016 inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
2018 return log_error_errno(errno
, "Failed to allocate inotify fd: %m");
2021 r
= path_extract_directory(path
, &dn
);
2023 return log_error_errno(r
, "Failed to extract directory from device node path '%s': %m", path
);
2025 _cleanup_free_
char *ndn
= NULL
;
2027 log_info("Watching %s", dn
);
2029 if (inotify_add_watch(inotify_fd
, dn
, IN_CREATE
|IN_MOVED_TO
|IN_ONLYDIR
|IN_DELETE_SELF
|IN_MOVE_SELF
) < 0) {
2030 if (errno
!= ENOENT
)
2031 return log_error_errno(errno
, "Failed to add watch on %s: %m", dn
);
2035 r
= path_extract_directory(dn
, &ndn
);
2036 if (r
== -EADDRNOTAVAIL
) /* Arrived at the top? */
2039 return log_error_errno(r
, "Failed to extract directory from device node path '%s': %m", dn
);
2041 free_and_replace(dn
, ndn
);
2044 w
= now(CLOCK_MONOTONIC
);
2046 return log_error_errno(SYNTHETIC_ERRNO(ETIMEDOUT
), "Device link %s still hasn't shown up, giving up.", path
);
2048 r
= fd_wait_for_event(inotify_fd
, POLLIN
, until
- w
);
2049 if (ERRNO_IS_NEG_TRANSIENT(r
))
2052 return log_error_errno(r
, "Failed to watch inotify: %m");
2054 (void) flush_fd(inotify_fd
);
2058 static int calculate_initial_image_size(UserRecord
*h
, int image_fd
, const char *fstype
, uint64_t *ret
) {
2059 uint64_t upper_boundary
, lower_boundary
;
2063 assert(image_fd
>= 0);
2066 if (fstatfs(image_fd
, &sfs
) < 0)
2067 return log_error_errno(errno
, "statfs() on image failed: %m");
2069 upper_boundary
= DISK_SIZE_ROUND_DOWN((uint64_t) sfs
.f_bsize
* sfs
.f_bavail
);
2071 if (h
->disk_size
!= UINT64_MAX
)
2072 *ret
= MIN(DISK_SIZE_ROUND_DOWN(h
->disk_size
), upper_boundary
);
2073 else if (h
->disk_size_relative
== UINT64_MAX
) {
2075 if (upper_boundary
> UINT64_MAX
/ USER_DISK_SIZE_DEFAULT_PERCENT
)
2076 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "Disk size too large.");
2078 *ret
= DISK_SIZE_ROUND_DOWN(upper_boundary
* USER_DISK_SIZE_DEFAULT_PERCENT
/ 100);
2080 log_info("Sizing home to %u%% of available disk space, which is %s.",
2081 USER_DISK_SIZE_DEFAULT_PERCENT
,
2082 FORMAT_BYTES(*ret
));
2084 *ret
= DISK_SIZE_ROUND_DOWN((uint64_t) ((double) upper_boundary
* (double) CLAMP(h
->disk_size_relative
, 0U, UINT32_MAX
) / (double) UINT32_MAX
));
2086 log_info("Sizing home to %" PRIu64
".%01" PRIu64
"%% of available disk space, which is %s.",
2087 (h
->disk_size_relative
* 100) / UINT32_MAX
,
2088 ((h
->disk_size_relative
* 1000) / UINT32_MAX
) % 10,
2089 FORMAT_BYTES(*ret
));
2092 lower_boundary
= minimal_size_by_fs_name(fstype
);
2093 if (lower_boundary
!= UINT64_MAX
) {
2094 assert(GPT_LUKS2_OVERHEAD
< UINT64_MAX
- lower_boundary
);
2095 lower_boundary
+= GPT_LUKS2_OVERHEAD
;
2097 if (lower_boundary
== UINT64_MAX
|| lower_boundary
< USER_DISK_SIZE_MIN
)
2098 lower_boundary
= USER_DISK_SIZE_MIN
;
2100 if (*ret
< lower_boundary
)
2101 *ret
= lower_boundary
;
2106 static int home_truncate(
2117 trunc
= user_record_luks_discard(h
);
2119 r
= fallocate(fd
, 0, 0, size
);
2120 if (r
< 0 && ERRNO_IS_NOT_SUPPORTED(errno
)) {
2121 /* Some file systems do not support fallocate(), let's gracefully degrade
2122 * (ZFS, reiserfs, …) and fall back to truncation */
2123 log_notice_errno(errno
, "Backing file system does not support fallocate(), falling back to ftruncate(), i.e. implicitly using non-discard mode.");
2129 r
= ftruncate(fd
, size
);
2132 if (ERRNO_IS_DISK_SPACE(errno
)) {
2133 log_debug_errno(errno
, "Not enough disk space to allocate home of size %s.", FORMAT_BYTES(size
));
2134 return -ENOSPC
; /* make recognizable */
2137 return log_error_errno(errno
, "Failed to truncate home image: %m");
2140 return !trunc
; /* Return == 0 if we managed to truncate, > 0 if we managed to allocate */
2143 int home_create_luks(
2146 const PasswordCache
*cache
,
2147 char **effective_passwords
,
2148 UserRecord
**ret_home
) {
2150 _cleanup_free_
char *subdir
= NULL
, *disk_uuid_path
= NULL
;
2151 uint64_t encrypted_size
,
2152 host_size
= 0, partition_offset
= 0, partition_size
= 0; /* Unnecessary initialization to appease gcc */
2153 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
;
2154 sd_id128_t partition_uuid
, fs_uuid
, luks_uuid
, disk_uuid
;
2155 _cleanup_close_
int mount_fd
= -EBADF
;
2156 const char *fstype
, *ip
;
2159 _cleanup_strv_free_
char **extra_mkfs_options
= NULL
;
2162 assert(h
->storage
< 0 || h
->storage
== USER_LUKS
);
2164 assert(!setup
->temporary_image_path
);
2165 assert(setup
->image_fd
< 0);
2168 r
= dlopen_cryptsetup();
2172 assert_se(ip
= user_record_image_path(h
));
2174 fstype
= user_record_file_system_type(h
);
2175 if (!supported_fstype(fstype
))
2176 return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT
), "Unsupported file system type: %s", fstype
);
2178 r
= mkfs_exists(fstype
);
2180 return log_error_errno(r
, "Failed to check if mkfs binary for %s exists: %m", fstype
);
2182 if (h
->file_system_type
|| streq(fstype
, "ext4") || !supported_fstype("ext4"))
2183 return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT
), "mkfs binary for file system type %s does not exist.", fstype
);
2185 /* If the record does not explicitly declare a file system to use, and the compiled-in
2186 * default does not actually exist, than do an automatic fallback onto ext4, as the baseline
2187 * fs of Linux. We won't search for a working fs type here beyond ext4, i.e. nothing fancier
2188 * than a single, conservative fallback to baseline. This should be useful in minimal
2189 * environments where mkfs.btrfs or so are not made available, but mkfs.ext4 as Linux' most
2190 * boring, most basic fs is. */
2191 log_info("Formatting tool for compiled-in default file system %s not available, falling back to ext4 instead.", fstype
);
2195 if (sd_id128_is_null(h
->partition_uuid
)) {
2196 r
= sd_id128_randomize(&partition_uuid
);
2198 return log_error_errno(r
, "Failed to acquire partition UUID: %m");
2200 partition_uuid
= h
->partition_uuid
;
2202 if (sd_id128_is_null(h
->luks_uuid
)) {
2203 r
= sd_id128_randomize(&luks_uuid
);
2205 return log_error_errno(r
, "Failed to acquire LUKS UUID: %m");
2207 luks_uuid
= h
->luks_uuid
;
2209 if (sd_id128_is_null(h
->file_system_uuid
)) {
2210 r
= sd_id128_randomize(&fs_uuid
);
2212 return log_error_errno(r
, "Failed to acquire file system UUID: %m");
2214 fs_uuid
= h
->file_system_uuid
;
2216 r
= make_dm_names(h
, setup
);
2220 r
= access(setup
->dm_node
, F_OK
);
2222 if (errno
!= ENOENT
)
2223 return log_error_errno(errno
, "Failed to determine whether %s exists: %m", setup
->dm_node
);
2225 return log_error_errno(SYNTHETIC_ERRNO(EEXIST
), "Device mapper device %s already exists, refusing.", setup
->dm_node
);
2227 if (path_startswith(ip
, "/dev/")) {
2228 _cleanup_free_
char *sysfs
= NULL
;
2229 uint64_t block_device_size
;
2232 /* Let's place the home directory on a real device, i.e. a USB stick or such */
2234 setup
->image_fd
= open_image_file(h
, ip
, &st
);
2235 if (setup
->image_fd
< 0)
2236 return setup
->image_fd
;
2238 if (!S_ISBLK(st
.st_mode
))
2239 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK
), "Device is not a block device, refusing.");
2241 if (asprintf(&sysfs
, "/sys/dev/block/" DEVNUM_FORMAT_STR
"/partition", DEVNUM_FORMAT_VAL(st
.st_rdev
)) < 0)
2243 if (access(sysfs
, F_OK
) < 0) {
2244 if (errno
!= ENOENT
)
2245 return log_error_errno(errno
, "Failed to check whether %s exists: %m", sysfs
);
2247 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK
), "Operating on partitions is currently not supported, sorry. Please specify a top-level block device.");
2249 if (flock(setup
->image_fd
, LOCK_EX
) < 0) /* make sure udev doesn't read from it while we operate on the device */
2250 return log_error_errno(errno
, "Failed to lock block device %s: %m", ip
);
2252 r
= blockdev_get_device_size(setup
->image_fd
, &block_device_size
);
2254 return log_error_errno(r
, "Failed to read block device size: %m");
2256 if (h
->disk_size
== UINT64_MAX
) {
2258 /* If a relative disk size is requested, apply it relative to the block device size */
2259 if (h
->disk_size_relative
< UINT32_MAX
)
2260 host_size
= CLAMP(DISK_SIZE_ROUND_DOWN(block_device_size
* h
->disk_size_relative
/ UINT32_MAX
),
2261 USER_DISK_SIZE_MIN
, USER_DISK_SIZE_MAX
);
2263 host_size
= block_device_size
; /* Otherwise, take the full device */
2265 } else if (h
->disk_size
> block_device_size
)
2266 return log_error_errno(SYNTHETIC_ERRNO(EMSGSIZE
), "Selected disk size larger than backing block device, refusing.");
2268 host_size
= DISK_SIZE_ROUND_DOWN(h
->disk_size
);
2270 if (!supported_fs_size(fstype
, LESS_BY(host_size
, GPT_LUKS2_OVERHEAD
)))
2271 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
),
2272 "Selected file system size too small for %s.", fstype
);
2274 /* After creation we should reference this partition by its UUID instead of the block
2275 * device. That's preferable since the user might have specified a device node such as
2276 * /dev/sdb to us, which might look very different when replugged. */
2277 if (asprintf(&disk_uuid_path
, "/dev/disk/by-uuid/" SD_ID128_UUID_FORMAT_STR
, SD_ID128_FORMAT_VAL(luks_uuid
)) < 0)
2280 if (user_record_luks_discard(h
) || user_record_luks_offline_discard(h
)) {
2281 /* If we want online or offline discard, discard once before we start using things. */
2283 if (ioctl(setup
->image_fd
, BLKDISCARD
, (uint64_t[]) { 0, block_device_size
}) < 0)
2284 log_full_errno(errno
== EOPNOTSUPP
? LOG_DEBUG
: LOG_WARNING
, errno
,
2285 "Failed to issue full-device BLKDISCARD on device, ignoring: %m");
2287 log_info("Full device discard completed.");
2290 _cleanup_free_
char *t
= NULL
;
2292 r
= mkdir_parents(ip
, 0755);
2294 return log_error_errno(r
, "Failed to create parent directory of %s: %m", ip
);
2296 r
= tempfn_random(ip
, "homework", &t
);
2298 return log_error_errno(r
, "Failed to derive temporary file name for %s: %m", ip
);
2300 setup
->image_fd
= open(t
, O_RDWR
|O_CREAT
|O_EXCL
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0600);
2301 if (setup
->image_fd
< 0)
2302 return log_error_errno(errno
, "Failed to create home image %s: %m", t
);
2304 setup
->temporary_image_path
= TAKE_PTR(t
);
2306 r
= chattr_full(setup
->image_fd
, NULL
, FS_NOCOW_FL
|FS_NOCOMP_FL
, FS_NOCOW_FL
|FS_NOCOMP_FL
, NULL
, NULL
, CHATTR_FALLBACK_BITWISE
);
2307 if (r
< 0 && r
!= -ENOANO
) /* ENOANO → some bits didn't work; which we skip logging about because chattr_full() already debug logs about those flags */
2308 log_full_errno(ERRNO_IS_IOCTL_NOT_SUPPORTED(r
) ? LOG_DEBUG
: LOG_WARNING
, r
,
2309 "Failed to set file attributes on %s, ignoring: %m", setup
->temporary_image_path
);
2311 r
= calculate_initial_image_size(h
, setup
->image_fd
, fstype
, &host_size
);
2315 r
= resize_image_loop(h
, setup
, 0, host_size
, &host_size
);
2319 log_info("Allocating image file completed.");
2322 r
= make_partition_table(
2324 user_record_luks_sector_size(h
),
2325 user_record_user_name_and_realm(h
),
2333 log_info("Writing of partition table completed.");
2335 r
= loop_device_make(
2340 user_record_luks_sector_size(h
),
2345 if (r
== -ENOENT
) { /* this means /dev/loop-control doesn't exist, i.e. we are in a container
2346 * or similar and loopback bock devices are not available, return a
2347 * recognizable error in this case. */
2348 log_error_errno(r
, "Loopback block device support is not available on this system.");
2349 return -ENOLINK
; /* Make recognizable */
2352 return log_error_errno(r
, "Failed to set up loopback device for %s: %m", setup
->temporary_image_path
);
2355 log_info("Setting up loopback device %s completed.", setup
->loop
->node
?: ip
);
2357 r
= luks_format(setup
->loop
->node
,
2360 user_record_user_name_and_realm(h
),
2362 effective_passwords
,
2363 user_record_luks_discard(h
) || user_record_luks_offline_discard(h
),
2365 &setup
->crypt_device
);
2369 setup
->undo_dm
= true;
2371 r
= block_get_size_by_path(setup
->dm_node
, &encrypted_size
);
2373 return log_error_errno(r
, "Failed to get encrypted block device size: %m");
2375 log_info("Setting up LUKS device %s completed.", setup
->dm_node
);
2377 r
= mkfs_options_from_env("HOME", fstype
, &extra_mkfs_options
);
2379 return log_error_errno(r
, "Failed to determine mkfs command line options for '%s': %m", fstype
);
2381 r
= make_filesystem(setup
->dm_node
,
2383 user_record_user_name_and_realm(h
),
2386 (user_record_luks_discard(h
) ? MKFS_DISCARD
: 0) | MKFS_QUIET
,
2387 /* sector_size = */ 0,
2388 /* compression = */ NULL
,
2389 /* compression_level= */ NULL
,
2390 extra_mkfs_options
);
2394 log_info("Formatting file system completed.");
2396 r
= home_unshare_and_mount(setup
->dm_node
, fstype
, user_record_luks_discard(h
), user_record_mount_flags(h
), h
->luks_extra_mount_options
);
2400 setup
->undo_mount
= true;
2402 subdir
= path_join(HOME_RUNTIME_WORK_DIR
, user_record_user_name_and_realm(h
));
2406 /* Prefer using a btrfs subvolume if we can, fall back to directory otherwise */
2407 r
= btrfs_subvol_make_fallback(AT_FDCWD
, subdir
, 0700);
2409 return log_error_errno(r
, "Failed to create user directory in mounted image file: %m");
2411 setup
->root_fd
= open(subdir
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
2412 if (setup
->root_fd
< 0)
2413 return log_error_errno(errno
, "Failed to open user directory in mounted image file: %m");
2415 (void) home_shift_uid(setup
->root_fd
, NULL
, UID_NOBODY
, h
->uid
, &mount_fd
);
2417 if (mount_fd
>= 0) {
2418 /* If we have established a new mount, then we can use that as new root fd to our home directory. */
2419 safe_close(setup
->root_fd
);
2421 setup
->root_fd
= fd_reopen(mount_fd
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
2422 if (setup
->root_fd
< 0)
2423 return log_error_errno(setup
->root_fd
, "Unable to convert mount fd into proper directory fd: %m");
2425 mount_fd
= safe_close(mount_fd
);
2428 r
= home_populate(h
, setup
->root_fd
);
2432 r
= home_sync_and_statfs(setup
->root_fd
, &sfs
);
2436 r
= user_record_clone(h
, USER_RECORD_LOAD_MASK_SECRET
|USER_RECORD_LOG
|USER_RECORD_PERMISSIVE
, &new_home
);
2438 return log_error_errno(r
, "Failed to clone record: %m");
2440 r
= user_record_add_binding(
2443 disk_uuid_path
?: ip
,
2447 sym_crypt_get_cipher(setup
->crypt_device
),
2448 sym_crypt_get_cipher_mode(setup
->crypt_device
),
2449 luks_volume_key_size_convert(setup
->crypt_device
),
2455 return log_error_errno(r
, "Failed to add binding to record: %m");
2457 if (user_record_luks_offline_discard(h
)) {
2458 r
= run_fitrim(setup
->root_fd
);
2463 setup
->root_fd
= safe_close(setup
->root_fd
);
2465 r
= home_setup_undo_mount(setup
, LOG_ERR
);
2469 r
= home_setup_undo_dm(setup
, LOG_ERR
);
2473 setup
->loop
= loop_device_unref(setup
->loop
);
2475 if (!user_record_luks_offline_discard(h
)) {
2476 r
= run_fallocate(setup
->image_fd
, NULL
/* refresh stat() data */);
2481 /* Sync everything to disk before we move things into place under the final name. */
2482 if (fsync(setup
->image_fd
) < 0)
2483 return log_error_errno(r
, "Failed to synchronize image to disk: %m");
2486 /* Reread partition table if this is a block device */
2487 (void) ioctl(setup
->image_fd
, BLKRRPART
, 0);
2489 assert(setup
->temporary_image_path
);
2491 if (rename(setup
->temporary_image_path
, ip
) < 0)
2492 return log_error_errno(errno
, "Failed to rename image file: %m");
2494 setup
->temporary_image_path
= mfree(setup
->temporary_image_path
);
2496 /* If we operate on a file, sync the containing directory too. */
2497 r
= fsync_directory_of_file(setup
->image_fd
);
2499 return log_error_errno(r
, "Failed to synchronize directory of image file to disk: %m");
2501 log_info("Moved image file into place.");
2504 /* Let's close the image fd now. If we are operating on a real block device this will release the BSD
2505 * lock that ensures udev doesn't interfere with what we are doing */
2506 setup
->image_fd
= safe_close(setup
->image_fd
);
2509 (void) wait_for_devlink(disk_uuid_path
);
2511 log_info("Creation completed.");
2513 print_size_summary(host_size
, encrypted_size
, &sfs
);
2515 log_debug("GPT + LUKS2 overhead is %" PRIu64
" (expected %" PRIu64
")", host_size
- encrypted_size
, GPT_LUKS2_OVERHEAD
);
2517 *ret_home
= TAKE_PTR(new_home
);
2521 int home_get_state_luks(UserRecord
*h
, HomeSetup
*setup
) {
2527 r
= make_dm_names(h
, setup
);
2531 r
= access(setup
->dm_node
, F_OK
);
2532 if (r
< 0 && errno
!= ENOENT
)
2533 return log_error_errno(errno
, "Failed to determine whether %s exists: %m", setup
->dm_node
);
2543 static int can_resize_fs(int fd
, uint64_t old_size
, uint64_t new_size
) {
2548 /* Filter out bogus requests early */
2549 if (old_size
== 0 || old_size
== UINT64_MAX
||
2550 new_size
== 0 || new_size
== UINT64_MAX
)
2551 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid resize parameters.");
2553 if ((old_size
& 511) != 0 || (new_size
& 511) != 0)
2554 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Resize parameters not multiple of 512.");
2556 if (fstatfs(fd
, &sfs
) < 0)
2557 return log_error_errno(errno
, "Failed to fstatfs() file system: %m");
2559 if (is_fs_type(&sfs
, BTRFS_SUPER_MAGIC
)) {
2561 if (new_size
< BTRFS_MINIMAL_SIZE
)
2562 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "New file system size too small for btrfs (needs to be 256M at least.");
2564 /* btrfs can grow and shrink online */
2566 } else if (is_fs_type(&sfs
, XFS_SUPER_MAGIC
)) {
2568 if (new_size
< XFS_MINIMAL_SIZE
)
2569 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "New file system size too small for xfs (needs to be 14M at least).");
2571 /* XFS can grow, but not shrink */
2572 if (new_size
< old_size
)
2573 return log_error_errno(SYNTHETIC_ERRNO(EMSGSIZE
), "Shrinking this type of file system is not supported.");
2575 } else if (is_fs_type(&sfs
, EXT4_SUPER_MAGIC
)) {
2577 if (new_size
< EXT4_MINIMAL_SIZE
)
2578 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "New file system size too small for ext4 (needs to be 1M at least).");
2580 /* ext4 can grow online, and shrink offline */
2581 if (new_size
< old_size
)
2582 return CAN_RESIZE_OFFLINE
;
2585 return log_error_errno(SYNTHETIC_ERRNO(ESOCKTNOSUPPORT
), "Resizing this type of file system is not supported.");
2587 return CAN_RESIZE_ONLINE
;
2590 static int ext4_offline_resize_fs(
2594 unsigned long flags
,
2595 const char *extra_mount_options
) {
2597 _cleanup_free_
char *size_str
= NULL
;
2598 bool re_open
= false, re_mount
= false;
2599 pid_t resize_pid
, fsck_pid
;
2603 assert(setup
->dm_node
);
2605 /* First, unmount the file system */
2606 if (setup
->root_fd
>= 0) {
2607 setup
->root_fd
= safe_close(setup
->root_fd
);
2611 if (setup
->undo_mount
) {
2612 r
= home_setup_undo_mount(setup
, LOG_ERR
);
2619 log_info("Temporary unmounting of file system completed.");
2621 /* resize2fs requires that the file system is force checked first, do so. */
2622 r
= safe_fork("(e2fsck)",
2623 FORK_RESET_SIGNALS
|FORK_RLIMIT_NOFILE_SAFE
|FORK_DEATHSIG_SIGTERM
|FORK_LOG
|FORK_STDOUT_TO_STDERR
|FORK_CLOSE_ALL_FDS
,
2629 execlp("e2fsck", "e2fsck", "-fp", setup
->dm_node
, NULL
);
2631 log_error_errno(errno
, "Failed to execute e2fsck: %m");
2632 _exit(EXIT_FAILURE
);
2635 exit_status
= wait_for_terminate_and_check("e2fsck", fsck_pid
, WAIT_LOG_ABNORMAL
);
2636 if (exit_status
< 0)
2638 if ((exit_status
& ~FSCK_ERROR_CORRECTED
) != 0) {
2639 log_warning("e2fsck failed with exit status %i.", exit_status
);
2641 if ((exit_status
& (FSCK_SYSTEM_SHOULD_REBOOT
|FSCK_ERRORS_LEFT_UNCORRECTED
)) != 0)
2642 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "File system is corrupted, refusing.");
2644 log_warning("Ignoring fsck error.");
2647 log_info("Forced file system check completed.");
2649 /* We use 512 sectors here, because resize2fs doesn't do byte sizes */
2650 if (asprintf(&size_str
, "%" PRIu64
"s", new_size
/ 512) < 0)
2653 /* Resize the thing */
2654 r
= safe_fork("(e2resize)",
2655 FORK_RESET_SIGNALS
|FORK_RLIMIT_NOFILE_SAFE
|FORK_DEATHSIG_SIGTERM
|FORK_LOG
|FORK_WAIT
|FORK_STDOUT_TO_STDERR
|FORK_CLOSE_ALL_FDS
,
2661 execlp("resize2fs", "resize2fs", setup
->dm_node
, size_str
, NULL
);
2663 log_error_errno(errno
, "Failed to execute resize2fs: %m");
2664 _exit(EXIT_FAILURE
);
2667 log_info("Offline file system resize completed.");
2669 /* Re-establish mounts and reopen the directory */
2671 r
= home_mount_node(setup
->dm_node
, "ext4", discard
, flags
, extra_mount_options
);
2675 setup
->undo_mount
= true;
2679 setup
->root_fd
= open(HOME_RUNTIME_WORK_DIR
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
2680 if (setup
->root_fd
< 0)
2681 return log_error_errno(errno
, "Failed to reopen file system: %m");
2684 log_info("File system mounted again.");
2689 static int prepare_resize_partition(
2691 uint64_t partition_offset
,
2692 uint64_t old_partition_size
,
2693 sd_id128_t
*ret_disk_uuid
,
2694 struct fdisk_table
**ret_table
,
2695 struct fdisk_partition
**ret_partition
) {
2697 _cleanup_(fdisk_unref_contextp
) struct fdisk_context
*c
= NULL
;
2698 _cleanup_(fdisk_unref_tablep
) struct fdisk_table
*t
= NULL
;
2699 _cleanup_free_
char *disk_uuid_as_string
= NULL
;
2700 struct fdisk_partition
*found
= NULL
;
2701 sd_id128_t disk_uuid
;
2702 size_t n_partitions
;
2706 assert(ret_disk_uuid
);
2709 assert((partition_offset
& 511) == 0);
2710 assert((old_partition_size
& 511) == 0);
2711 assert(UINT64_MAX
- old_partition_size
>= partition_offset
);
2713 if (partition_offset
== 0) {
2714 /* If the offset is at the beginning we assume no partition table, let's exit early. */
2715 log_debug("Not rewriting partition table, operating on naked device.");
2716 *ret_disk_uuid
= SD_ID128_NULL
;
2718 *ret_partition
= NULL
;
2722 r
= fdisk_new_context_at(fd
, /* path= */ NULL
, /* read_only= */ false, UINT32_MAX
, &c
);
2724 return log_error_errno(r
, "Failed to open device: %m");
2726 if (!fdisk_is_labeltype(c
, FDISK_DISKLABEL_GPT
))
2727 return log_error_errno(SYNTHETIC_ERRNO(ENOMEDIUM
), "Disk has no GPT partition table.");
2729 r
= fdisk_get_disklabel_id(c
, &disk_uuid_as_string
);
2731 return log_error_errno(r
, "Failed to acquire disk UUID: %m");
2733 r
= sd_id128_from_string(disk_uuid_as_string
, &disk_uuid
);
2735 return log_error_errno(r
, "Failed parse disk UUID: %m");
2737 r
= fdisk_get_partitions(c
, &t
);
2739 return log_error_errno(r
, "Failed to acquire partition table: %m");
2741 n_partitions
= fdisk_table_get_nents(t
);
2742 for (size_t i
= 0; i
< n_partitions
; i
++) {
2743 struct fdisk_partition
*p
;
2745 p
= fdisk_table_get_partition(t
, i
);
2747 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to read partition metadata.");
2749 if (fdisk_partition_is_used(p
) <= 0)
2751 if (fdisk_partition_has_start(p
) <= 0 || fdisk_partition_has_size(p
) <= 0 || fdisk_partition_has_end(p
) <= 0)
2752 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Found partition without a size.");
2754 if (fdisk_partition_get_start(p
) == partition_offset
/ 512U &&
2755 fdisk_partition_get_size(p
) == old_partition_size
/ 512U) {
2758 return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ
), "Partition found twice, refusing.");
2761 } else if (fdisk_partition_get_end(p
) > partition_offset
/ 512U)
2762 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Can't extend, not last partition in image.");
2766 return log_error_errno(SYNTHETIC_ERRNO(ENOPKG
), "Failed to find matching partition to resize.");
2768 *ret_disk_uuid
= disk_uuid
;
2769 *ret_table
= TAKE_PTR(t
);
2770 *ret_partition
= found
;
2775 static int get_maximum_partition_size(
2777 struct fdisk_partition
*p
,
2778 uint64_t *ret_maximum_partition_size
) {
2780 _cleanup_(fdisk_unref_contextp
) struct fdisk_context
*c
= NULL
;
2781 uint64_t start_lba
, start
, last_lba
, end
;
2786 assert(ret_maximum_partition_size
);
2788 r
= fdisk_new_context_at(fd
, /* path= */ NULL
, /* read_only= */ true, /* sector_size= */ UINT32_MAX
, &c
);
2790 return log_error_errno(r
, "Failed to create fdisk context: %m");
2792 start_lba
= fdisk_partition_get_start(p
);
2793 assert(start_lba
<= UINT64_MAX
/512);
2794 start
= start_lba
* 512;
2796 last_lba
= fdisk_get_last_lba(c
); /* One sector before boundary where usable space ends */
2797 assert(last_lba
< UINT64_MAX
/512);
2798 end
= DISK_SIZE_ROUND_DOWN((last_lba
+ 1) * 512); /* Round down to multiple of 4K */
2801 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Last LBA is before partition start.");
2803 *ret_maximum_partition_size
= DISK_SIZE_ROUND_DOWN(end
- start
);
2808 static int ask_cb(struct fdisk_context
*c
, struct fdisk_ask
*ask
, void *userdata
) {
2813 switch (fdisk_ask_get_type(ask
)) {
2815 case FDISK_ASKTYPE_STRING
:
2816 result
= new(char, 37);
2820 fdisk_ask_string_set_result(ask
, sd_id128_to_uuid_string(*(sd_id128_t
*) userdata
, result
));
2824 log_debug("Unexpected question from libfdisk, ignoring.");
2830 static int apply_resize_partition(
2832 sd_id128_t disk_uuids
,
2833 struct fdisk_table
*t
,
2834 struct fdisk_partition
*p
,
2835 size_t new_partition_size
) {
2837 _cleanup_(fdisk_unref_contextp
) struct fdisk_context
*c
= NULL
;
2838 _cleanup_free_
void *two_zero_lbas
= NULL
;
2846 if (!t
) /* no partition table to apply, exit early */
2851 /* Before writing our partition patch the final size in */
2852 r
= fdisk_partition_size_explicit(p
, 1);
2854 return log_error_errno(r
, "Failed to enable explicit partition size: %m");
2856 r
= fdisk_partition_set_size(p
, new_partition_size
/ 512U);
2858 return log_error_errno(r
, "Failed to change partition size: %m");
2860 r
= probe_sector_size(fd
, &ssz
);
2862 return log_error_errno(r
, "Failed to determine current sector size: %m");
2864 two_zero_lbas
= malloc0(ssz
* 2);
2868 /* libfdisk appears to get confused by the existing PMBR. Let's explicitly flush it out. */
2869 n
= pwrite(fd
, two_zero_lbas
, ssz
* 2, 0);
2871 return log_error_errno(errno
, "Failed to wipe partition table: %m");
2872 if ((size_t) n
!= ssz
* 2)
2873 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short write while wiping partition table.");
2875 r
= fdisk_new_context_at(fd
, /* path= */ NULL
, /* read_only= */ false, ssz
, &c
);
2877 return log_error_errno(r
, "Failed to open device: %m");
2879 r
= fdisk_create_disklabel(c
, "gpt");
2881 return log_error_errno(r
, "Failed to create GPT disk label: %m");
2883 r
= fdisk_apply_table(c
, t
);
2885 return log_error_errno(r
, "Failed to apply partition table: %m");
2887 r
= fdisk_set_ask(c
, ask_cb
, &disk_uuids
);
2889 return log_error_errno(r
, "Failed to set libfdisk query function: %m");
2891 r
= fdisk_set_disklabel_id(c
);
2893 return log_error_errno(r
, "Failed to change disklabel ID: %m");
2895 r
= fdisk_write_disklabel(c
);
2897 return log_error_errno(r
, "Failed to write disk label: %m");
2902 /* Always keep at least 16M free, so that we can safely log in and update the user record while doing so */
2903 #define HOME_MIN_FREE (16U*1024U*1024U)
2905 static int get_smallest_fs_size(int fd
, uint64_t *ret
) {
2906 uint64_t minsz
, needed
;
2912 /* Determines the minimal disk size we might be able to shrink the file system referenced by the fd to. */
2914 if (syncfs(fd
) < 0) /* let's sync before we query the size, so that the values returned are accurate */
2915 return log_error_errno(errno
, "Failed to synchronize home file system: %m");
2917 if (fstatfs(fd
, &sfs
) < 0)
2918 return log_error_errno(errno
, "Failed to statfs() home file system: %m");
2920 /* Let's determine the minimal file system size of the used fstype */
2921 minsz
= minimal_size_by_fs_magic(sfs
.f_type
);
2922 if (minsz
== UINT64_MAX
)
2923 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "Don't know minimum file system size of file system type '%s' of home directory.", fs_type_to_string(sfs
.f_type
));
2925 if (minsz
< USER_DISK_SIZE_MIN
)
2926 minsz
= USER_DISK_SIZE_MIN
;
2928 if (sfs
.f_bfree
> sfs
.f_blocks
)
2929 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Detected amount of free blocks is greater than the total amount of file system blocks. Refusing.");
2931 /* Calculate how much disk space is currently in use. */
2932 needed
= sfs
.f_blocks
- sfs
.f_bfree
;
2933 if (needed
> UINT64_MAX
/ sfs
.f_bsize
)
2934 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "File system size out of range.");
2936 needed
*= sfs
.f_bsize
;
2938 /* Add some safety margin of free space we'll always keep */
2939 if (needed
> UINT64_MAX
- HOME_MIN_FREE
) /* Check for overflow */
2940 needed
= UINT64_MAX
;
2942 needed
+= HOME_MIN_FREE
;
2944 *ret
= DISK_SIZE_ROUND_UP(MAX(needed
, minsz
));
2948 static int get_largest_image_size(int fd
, const struct stat
*st
, uint64_t *ret
) {
2949 uint64_t used
, avail
, sum
;
2957 /* Determines the maximum file size we might be able to grow the image file referenced by the fd to. */
2959 r
= stat_verify_regular(st
);
2961 return log_error_errno(r
, "Image file is not a regular file, refusing: %m");
2964 return log_error_errno(errno
, "Failed to synchronize file system backing image file: %m");
2966 if (fstatfs(fd
, &sfs
) < 0)
2967 return log_error_errno(errno
, "Failed to statfs() image file: %m");
2969 used
= (uint64_t) st
->st_blocks
* 512;
2970 avail
= (uint64_t) sfs
.f_bsize
* sfs
.f_bavail
;
2972 if (avail
> UINT64_MAX
- used
)
2977 *ret
= DISK_SIZE_ROUND_DOWN(MIN(sum
, USER_DISK_SIZE_MAX
));
2981 static int resize_fs_loop(
2985 uint64_t old_fs_size
,
2986 uint64_t new_fs_size
,
2987 uint64_t *ret_fs_size
) {
2989 uint64_t current_fs_size
;
2990 unsigned n_iterations
= 0;
2995 assert(setup
->root_fd
>= 0);
2997 /* A bisection loop trying to find the closest size to what the user asked for. (Well, we bisect like
2998 * this only when we *shrink* the fs — if we grow the fs there's no need to bisect.) */
3000 current_fs_size
= old_fs_size
;
3001 for (uint64_t lower_boundary
= new_fs_size
, upper_boundary
= old_fs_size
, try_fs_size
= new_fs_size
;;) {
3006 /* Now resize the file system */
3007 if (resize_type
== CAN_RESIZE_ONLINE
) {
3008 r
= resize_fs(setup
->root_fd
, try_fs_size
, NULL
);
3010 if (!ERRNO_IS_DISK_SPACE(r
) || new_fs_size
> old_fs_size
) /* Not a disk space issue? Not trying to shrink? */
3011 return log_error_errno(r
, "Failed to resize file system: %m");
3013 log_debug_errno(r
, "Shrinking from %s to %s didn't work, not enough space for contained data.", FORMAT_BYTES(current_fs_size
), FORMAT_BYTES(try_fs_size
));
3016 log_debug("Successfully resized from %s to %s.", FORMAT_BYTES(current_fs_size
), FORMAT_BYTES(try_fs_size
));
3017 current_fs_size
= try_fs_size
;
3021 /* If we hit a disk space issue and are shrinking the fs, then maybe it helps to
3022 * increase the image size. */
3024 r
= ext4_offline_resize_fs(setup
, try_fs_size
, user_record_luks_discard(h
), user_record_mount_flags(h
), h
->luks_extra_mount_options
);
3028 /* For now, when we fail to shrink an ext4 image we'll not try again via the
3029 * bisection logic. We might add that later, but given this involves shelling out
3030 * multiple programs, it's a bit too cumbersome for my taste. */
3033 current_fs_size
= try_fs_size
;
3036 if (new_fs_size
> old_fs_size
) /* If we are growing we are done after one iteration */
3039 /* If we are shrinking then let's adjust our bisection boundaries and try again. */
3041 upper_boundary
= MIN(upper_boundary
, try_fs_size
);
3043 lower_boundary
= MAX(lower_boundary
, try_fs_size
);
3045 /* OK, this attempt to shrink didn't work. Let's try between the old size and what worked. */
3046 if (lower_boundary
>= upper_boundary
) {
3047 log_debug("Image can't be shrunk further (range to try is empty).");
3051 /* Let's find a new value to try half-way between the lower boundary and the upper boundary
3053 try_fs_size
= DISK_SIZE_ROUND_DOWN(lower_boundary
+ (upper_boundary
- lower_boundary
) / 2);
3054 if (try_fs_size
<= lower_boundary
|| try_fs_size
>= upper_boundary
) {
3055 log_debug("Image can't be shrunk further (remaining range to try too small).");
3060 log_debug("Bisection loop completed after %u iterations.", n_iterations
);
3063 *ret_fs_size
= current_fs_size
;
3068 static int resize_image_loop(
3071 uint64_t old_image_size
,
3072 uint64_t new_image_size
,
3073 uint64_t *ret_image_size
) {
3075 uint64_t current_image_size
;
3076 unsigned n_iterations
= 0;
3081 assert(setup
->image_fd
>= 0);
3083 /* A bisection loop trying to find the closest size to what the user asked for. (Well, we bisect like
3084 * this only when we *grow* the image — if we shrink the image then there's no need to bisect.) */
3086 current_image_size
= old_image_size
;
3087 for (uint64_t lower_boundary
= old_image_size
, upper_boundary
= new_image_size
, try_image_size
= new_image_size
;;) {
3092 r
= home_truncate(h
, setup
->image_fd
, try_image_size
);
3094 if (!ERRNO_IS_DISK_SPACE(r
) || new_image_size
< old_image_size
) /* Not a disk space issue? Not trying to grow? */
3097 log_debug_errno(r
, "Growing from %s to %s didn't work, not enough space on backing disk.", FORMAT_BYTES(current_image_size
), FORMAT_BYTES(try_image_size
));
3099 } else if (r
> 0) { /* Success: allocation worked */
3100 log_debug("Resizing from %s to %s via allocation worked successfully.", FORMAT_BYTES(current_image_size
), FORMAT_BYTES(try_image_size
));
3101 current_image_size
= try_image_size
;
3103 } else { /* Success, but through truncation, not allocation. */
3104 log_debug("Resizing from %s to %s via truncation worked successfully.", FORMAT_BYTES(old_image_size
), FORMAT_BYTES(try_image_size
));
3105 current_image_size
= try_image_size
;
3106 break; /* there's no point in the bisection logic if this was plain truncation and
3107 * not allocation, let's exit immediately. */
3110 if (new_image_size
< old_image_size
) /* If we are shrinking we are done after one iteration */
3113 /* If we are growing then let's adjust our bisection boundaries and try again */
3115 lower_boundary
= MAX(lower_boundary
, try_image_size
);
3117 upper_boundary
= MIN(upper_boundary
, try_image_size
);
3119 if (lower_boundary
>= upper_boundary
) {
3120 log_debug("Image can't be grown further (range to try is empty).");
3124 try_image_size
= DISK_SIZE_ROUND_DOWN(lower_boundary
+ (upper_boundary
- lower_boundary
) / 2);
3125 if (try_image_size
<= lower_boundary
|| try_image_size
>= upper_boundary
) {
3126 log_debug("Image can't be grown further (remaining range to try too small).");
3131 log_debug("Bisection loop completed after %u iterations.", n_iterations
);
3134 *ret_image_size
= current_image_size
;
3139 int home_resize_luks(
3141 HomeSetupFlags flags
,
3143 PasswordCache
*cache
,
3144 UserRecord
**ret_home
) {
3146 uint64_t old_image_size
, new_image_size
, old_fs_size
, new_fs_size
, crypto_offset
, crypto_offset_bytes
,
3147 new_partition_size
, smallest_fs_size
, resized_fs_size
;
3148 _cleanup_(user_record_unrefp
) UserRecord
*header_home
= NULL
, *embedded_home
= NULL
, *new_home
= NULL
;
3149 _cleanup_(fdisk_unref_tablep
) struct fdisk_table
*table
= NULL
;
3150 struct fdisk_partition
*partition
= NULL
;
3151 _cleanup_close_
int opened_image_fd
= -EBADF
;
3152 _cleanup_free_
char *whole_disk
= NULL
;
3153 int r
, resize_type
, image_fd
= -EBADF
, reconciled
= USER_RECONCILE_IDENTICAL
;
3154 sd_id128_t disk_uuid
;
3155 const char *ip
, *ipo
;
3159 INTENTION_DONT_KNOW
= 0, /* These happen to match the return codes of CMP() */
3160 INTENTION_SHRINK
= -1,
3162 } intention
= INTENTION_DONT_KNOW
;
3165 assert(user_record_storage(h
) == USER_LUKS
);
3168 r
= dlopen_cryptsetup();
3172 assert_se(ipo
= user_record_image_path(h
));
3173 ip
= strdupa_safe(ipo
); /* copy out since original might change later in home record object */
3175 if (setup
->image_fd
< 0) {
3176 setup
->image_fd
= open_image_file(h
, NULL
, &st
);
3177 if (setup
->image_fd
< 0)
3178 return setup
->image_fd
;
3180 if (fstat(setup
->image_fd
, &st
) < 0)
3181 return log_error_errno(errno
, "Failed to stat image file %s: %m", ip
);
3184 image_fd
= setup
->image_fd
;
3186 if (S_ISBLK(st
.st_mode
)) {
3189 r
= block_get_whole_disk(st
.st_rdev
, &parent
);
3191 return log_error_errno(r
, "Failed to acquire whole block device for %s: %m", ip
);
3193 /* If we shall resize a file system on a partition device, then let's figure out the
3194 * whole disk device and operate on that instead, since we need to rewrite the
3195 * partition table to resize the partition. */
3197 log_info("Operating on partition device %s, using parent device.", ip
);
3199 opened_image_fd
= r
= device_open_from_devnum(S_IFBLK
, parent
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
, &whole_disk
);
3201 return log_error_errno(r
, "Failed to open whole block device for %s: %m", ip
);
3203 image_fd
= opened_image_fd
;
3205 if (fstat(image_fd
, &st
) < 0)
3206 return log_error_errno(errno
, "Failed to stat whole block device %s: %m", whole_disk
);
3208 log_info("Operating on whole block device %s.", ip
);
3210 r
= blockdev_get_device_size(image_fd
, &old_image_size
);
3212 return log_error_errno(r
, "Failed to determine size of original block device: %m");
3214 if (flock(image_fd
, LOCK_EX
) < 0) /* make sure udev doesn't read from it while we operate on the device */
3215 return log_error_errno(errno
, "Failed to lock block device %s: %m", ip
);
3217 new_image_size
= old_image_size
; /* we can't resize physical block devices */
3219 r
= stat_verify_regular(&st
);
3221 return log_error_errno(r
, "Image %s is not a block device nor regular file: %m", ip
);
3223 old_image_size
= st
.st_size
;
3225 /* Note an asymmetry here: when we operate on loopback files the specified disk size we get we
3226 * apply onto the loopback file as a whole. When we operate on block devices we instead apply
3227 * to the partition itself only. */
3229 if (FLAGS_SET(flags
, HOME_SETUP_RESIZE_MINIMIZE
)) {
3231 intention
= INTENTION_SHRINK
;
3233 uint64_t new_image_size_rounded
;
3235 new_image_size_rounded
= DISK_SIZE_ROUND_DOWN(h
->disk_size
);
3237 if (old_image_size
>= new_image_size_rounded
&& old_image_size
<= h
->disk_size
) {
3238 /* If exact match, or a match after we rounded down, don't do a thing */
3239 log_info("Image size already matching, skipping operation.");
3243 new_image_size
= new_image_size_rounded
;
3244 intention
= CMP(new_image_size
, old_image_size
); /* Is this a shrink */
3248 r
= home_setup_luks(
3254 FLAGS_SET(flags
, HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES
) ? NULL
: &header_home
);
3258 if (!FLAGS_SET(flags
, HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES
)) {
3259 reconciled
= home_load_embedded_identity(h
, setup
->root_fd
, header_home
, USER_RECONCILE_REQUIRE_NEWER_OR_EQUAL
, cache
, &embedded_home
, &new_home
);
3264 r
= home_maybe_shift_uid(h
, flags
, setup
);
3268 log_info("offset = %" PRIu64
", size = %" PRIu64
", image = %" PRIu64
, setup
->partition_offset
, setup
->partition_size
, old_image_size
);
3270 if ((UINT64_MAX
- setup
->partition_offset
) < setup
->partition_size
||
3271 setup
->partition_offset
+ setup
->partition_size
> old_image_size
)
3272 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Old partition doesn't fit in backing storage, refusing.");
3274 /* Get target partition information in here for new_partition_size calculation */
3275 r
= prepare_resize_partition(
3277 setup
->partition_offset
,
3278 setup
->partition_size
,
3285 if (S_ISREG(st
.st_mode
)) {
3286 uint64_t partition_table_extra
, largest_size
;
3288 partition_table_extra
= old_image_size
- setup
->partition_size
;
3290 r
= get_largest_image_size(setup
->image_fd
, &st
, &largest_size
);
3293 if (new_image_size
> largest_size
)
3294 new_image_size
= largest_size
;
3296 if (new_image_size
< partition_table_extra
)
3297 new_image_size
= partition_table_extra
;
3299 new_partition_size
= DISK_SIZE_ROUND_DOWN(new_image_size
- partition_table_extra
);
3301 assert(S_ISBLK(st
.st_mode
));
3303 if (FLAGS_SET(flags
, HOME_SETUP_RESIZE_MINIMIZE
)) {
3304 new_partition_size
= 0;
3305 intention
= INTENTION_SHRINK
;
3307 uint64_t new_partition_size_rounded
= DISK_SIZE_ROUND_DOWN(h
->disk_size
);
3309 if (h
->disk_size
== UINT64_MAX
&& partition
) {
3310 r
= get_maximum_partition_size(image_fd
, partition
, &new_partition_size_rounded
);
3315 if (setup
->partition_size
>= new_partition_size_rounded
&&
3316 setup
->partition_size
<= h
->disk_size
) {
3317 log_info("Partition size already matching, skipping operation.");
3321 new_partition_size
= new_partition_size_rounded
;
3322 intention
= CMP(new_partition_size
, setup
->partition_size
);
3326 if ((UINT64_MAX
- setup
->partition_offset
) < new_partition_size
||
3327 setup
->partition_offset
+ new_partition_size
> new_image_size
)
3328 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "New partition doesn't fit into backing storage, refusing.");
3330 crypto_offset
= sym_crypt_get_data_offset(setup
->crypt_device
);
3331 if (crypto_offset
> UINT64_MAX
/512U)
3332 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "LUKS2 data offset out of range, refusing.");
3333 crypto_offset_bytes
= (uint64_t) crypto_offset
* 512U;
3334 if (setup
->partition_size
<= crypto_offset_bytes
)
3335 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Weird, old crypto payload offset doesn't actually fit in partition size?");
3337 /* Make sure at least the LUKS header fit in */
3338 if (new_partition_size
<= crypto_offset_bytes
) {
3341 add
= DISK_SIZE_ROUND_UP(crypto_offset_bytes
) - new_partition_size
;
3342 new_partition_size
+= add
;
3343 if (S_ISREG(st
.st_mode
))
3344 new_image_size
+= add
;
3347 old_fs_size
= setup
->partition_size
- crypto_offset_bytes
;
3348 new_fs_size
= DISK_SIZE_ROUND_DOWN(new_partition_size
- crypto_offset_bytes
);
3350 r
= get_smallest_fs_size(setup
->root_fd
, &smallest_fs_size
);
3354 if (new_fs_size
< smallest_fs_size
) {
3357 add
= DISK_SIZE_ROUND_UP(smallest_fs_size
) - new_fs_size
;
3359 new_partition_size
+= add
;
3360 if (S_ISREG(st
.st_mode
))
3361 new_image_size
+= add
;
3364 if (new_fs_size
== old_fs_size
) {
3365 log_info("New file system size identical to old file system size, skipping operation.");
3369 if (FLAGS_SET(flags
, HOME_SETUP_RESIZE_DONT_GROW
) && new_fs_size
> old_fs_size
) {
3370 log_info("New file system size would be larger than old, but shrinking requested, skipping operation.");
3374 if (FLAGS_SET(flags
, HOME_SETUP_RESIZE_DONT_SHRINK
) && new_fs_size
< old_fs_size
) {
3375 log_info("New file system size would be smaller than old, but growing requested, skipping operation.");
3379 if (CMP(new_fs_size
, old_fs_size
) != intention
) {
3381 log_info("Shrink operation would enlarge file system, skipping operation.");
3383 assert(intention
> 0);
3384 log_info("Grow operation would shrink file system, skipping operation.");
3389 /* Before we start doing anything, let's figure out if we actually can */
3390 resize_type
= can_resize_fs(setup
->root_fd
, old_fs_size
, new_fs_size
);
3391 if (resize_type
< 0)
3393 if (resize_type
== CAN_RESIZE_OFFLINE
&& FLAGS_SET(flags
, HOME_SETUP_ALREADY_ACTIVATED
))
3394 return log_error_errno(SYNTHETIC_ERRNO(ETXTBSY
), "File systems of this type can only be resized offline, but is currently online.");
3396 log_info("Ready to resize image size %s %s %s, partition size %s %s %s, file system size %s %s %s.",
3397 FORMAT_BYTES(old_image_size
),
3398 glyph(GLYPH_ARROW_RIGHT
),
3399 FORMAT_BYTES(new_image_size
),
3400 FORMAT_BYTES(setup
->partition_size
),
3401 glyph(GLYPH_ARROW_RIGHT
),
3402 FORMAT_BYTES(new_partition_size
),
3403 FORMAT_BYTES(old_fs_size
),
3404 glyph(GLYPH_ARROW_RIGHT
),
3405 FORMAT_BYTES(new_fs_size
));
3407 if (new_fs_size
> old_fs_size
) { /* → Grow */
3409 if (S_ISREG(st
.st_mode
)) {
3410 uint64_t resized_image_size
;
3412 /* Grow file size */
3413 r
= resize_image_loop(h
, setup
, old_image_size
, new_image_size
, &resized_image_size
);
3417 if (resized_image_size
== old_image_size
) {
3418 log_info("Couldn't change image size.");
3422 assert(resized_image_size
> old_image_size
);
3424 log_info("Growing of image file from %s to %s completed.", FORMAT_BYTES(old_image_size
), FORMAT_BYTES(resized_image_size
));
3426 if (resized_image_size
< new_image_size
) {
3429 /* If the growing we managed to do is smaller than what we wanted we need to
3430 * adjust the partition/file system sizes we are going for, too */
3431 sub
= new_image_size
- resized_image_size
;
3432 assert(new_partition_size
>= sub
);
3433 new_partition_size
-= sub
;
3434 assert(new_fs_size
>= sub
);
3438 new_image_size
= resized_image_size
;
3440 assert(S_ISBLK(st
.st_mode
));
3441 assert(new_image_size
== old_image_size
);
3444 /* Make sure loopback device sees the new bigger size */
3445 r
= loop_device_refresh_size(setup
->loop
, UINT64_MAX
, new_partition_size
);
3447 log_debug_errno(r
, "Device is not a loopback device, not refreshing size.");
3449 return log_error_errno(r
, "Failed to refresh loopback device size: %m");
3451 log_info("Refreshing loop device size completed.");
3453 r
= apply_resize_partition(image_fd
, disk_uuid
, table
, partition
, new_partition_size
);
3457 log_info("Growing of partition completed.");
3459 if (S_ISBLK(st
.st_mode
) && ioctl(image_fd
, BLKRRPART
, 0) < 0)
3460 log_debug_errno(errno
, "BLKRRPART failed on block device, ignoring: %m");
3462 /* Tell LUKS about the new bigger size too */
3463 r
= sym_crypt_resize(setup
->crypt_device
, setup
->dm_name
, new_fs_size
/ 512U);
3465 return log_error_errno(r
, "Failed to grow LUKS device: %m");
3467 log_info("LUKS device growing completed.");
3471 if (!FLAGS_SET(flags
, HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES
)) {
3472 r
= home_store_embedded_identity(new_home
, setup
->root_fd
, embedded_home
);
3476 r
= home_reconcile_blob_dirs(new_home
, setup
->root_fd
, reconciled
);
3481 if (S_ISREG(st
.st_mode
)) {
3482 if (user_record_luks_discard(h
))
3483 /* Before we shrink, let's trim the file system, so that we need less space on disk during the shrinking */
3484 (void) run_fitrim(setup
->root_fd
);
3486 /* If discard is off, let's ensure all backing blocks are allocated, so that our resize operation doesn't fail half-way */
3487 r
= run_fallocate(image_fd
, &st
);
3494 /* Now try to resize the file system. The requested size might not always be possible, in which case
3495 * we'll try to get as close as we can get. The result is returned in 'resized_fs_size' */
3496 r
= resize_fs_loop(h
, setup
, resize_type
, old_fs_size
, new_fs_size
, &resized_fs_size
);
3500 if (resized_fs_size
== old_fs_size
) {
3501 log_info("Couldn't change file system size.");
3505 log_info("File system resizing from %s to %s completed.", FORMAT_BYTES(old_fs_size
), FORMAT_BYTES(resized_fs_size
));
3507 if (resized_fs_size
> new_fs_size
) {
3510 /* If the shrinking we managed to do is larger than what we wanted we need to adjust the partition/image sizes. */
3511 add
= resized_fs_size
- new_fs_size
;
3512 new_partition_size
+= add
;
3513 if (S_ISREG(st
.st_mode
))
3514 new_image_size
+= add
;
3517 new_fs_size
= resized_fs_size
;
3519 /* Immediately sync afterwards */
3520 r
= home_sync_and_statfs(setup
->root_fd
, NULL
);
3524 if (new_fs_size
< old_fs_size
) { /* → Shrink */
3526 /* Shrink the LUKS device now, matching the new file system size */
3527 r
= sym_crypt_resize(setup
->crypt_device
, setup
->dm_name
, new_fs_size
/ 512);
3529 return log_error_errno(r
, "Failed to shrink LUKS device: %m");
3531 log_info("LUKS device shrinking completed.");
3533 /* Refresh the loop devices size */
3534 r
= loop_device_refresh_size(setup
->loop
, UINT64_MAX
, new_partition_size
);
3536 log_debug_errno(r
, "Device is not a loopback device, not refreshing size.");
3538 return log_error_errno(r
, "Failed to refresh loopback device size: %m");
3540 log_info("Refreshing loop device size completed.");
3542 if (S_ISREG(st
.st_mode
)) {
3543 /* Shrink the image file */
3544 if (ftruncate(image_fd
, new_image_size
) < 0)
3545 return log_error_errno(errno
, "Failed to shrink image file %s: %m", ip
);
3547 log_info("Shrinking of image file completed.");
3549 assert(S_ISBLK(st
.st_mode
));
3550 assert(new_image_size
== old_image_size
);
3553 r
= apply_resize_partition(image_fd
, disk_uuid
, table
, partition
, new_partition_size
);
3557 log_info("Shrinking of partition completed.");
3559 if (S_ISBLK(st
.st_mode
) && ioctl(image_fd
, BLKRRPART
, 0) < 0)
3560 log_debug_errno(errno
, "BLKRRPART failed on block device, ignoring: %m");
3562 } else { /* → Grow */
3563 if (!FLAGS_SET(flags
, HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES
)) {
3564 r
= home_store_embedded_identity(new_home
, setup
->root_fd
, embedded_home
);
3568 r
= home_reconcile_blob_dirs(new_home
, setup
->root_fd
, reconciled
);
3574 if (!FLAGS_SET(flags
, HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES
)) {
3575 r
= home_store_header_identity_luks(new_home
, setup
, header_home
);
3579 r
= home_extend_embedded_identity(new_home
, h
, setup
);
3584 if (user_record_luks_discard(h
))
3585 (void) run_fitrim(setup
->root_fd
);
3587 r
= home_sync_and_statfs(setup
->root_fd
, &sfs
);
3591 if (!FLAGS_SET(flags
, HOME_SETUP_RESIZE_DONT_UNDO
)) {
3592 r
= home_setup_done(setup
);
3597 log_info("Resizing completed.");
3599 print_size_summary(new_image_size
, new_fs_size
, &sfs
);
3602 *ret_home
= TAKE_PTR(new_home
);
3607 int home_passwd_luks(
3609 HomeSetupFlags flags
,
3611 const PasswordCache
*cache
, /* the passwords acquired via PKCS#11/FIDO2 security tokens */
3612 char **effective_passwords
/* new passwords */) {
3614 size_t volume_key_size
, max_key_slots
, n_effective
;
3615 _cleanup_(erase_and_freep
) void *volume_key
= NULL
;
3616 struct crypt_pbkdf_type good_pbkdf
, minimal_pbkdf
;
3621 assert(user_record_storage(h
) == USER_LUKS
);
3624 r
= dlopen_cryptsetup();
3628 type
= sym_crypt_get_type(setup
->crypt_device
);
3630 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to determine crypto device type.");
3632 r
= sym_crypt_keyslot_max(type
);
3634 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to determine number of key slots.");
3637 r
= sym_crypt_get_volume_key_size(setup
->crypt_device
);
3639 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to determine volume key size.");
3640 volume_key_size
= (size_t) r
;
3642 volume_key
= malloc(volume_key_size
);
3646 r
= luks_get_volume_key(h
, setup
->crypt_device
, cache
, volume_key
, &volume_key_size
, NULL
);
3648 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Failed to unlock LUKS superblock with supplied passwords.");
3650 return log_error_errno(r
, "Failed to unlock LUKS superblock: %m");
3652 n_effective
= strv_length(effective_passwords
);
3654 build_good_pbkdf(&good_pbkdf
, h
);
3655 build_minimal_pbkdf(&minimal_pbkdf
, h
);
3657 for (size_t i
= 0; i
< max_key_slots
; i
++) {
3658 r
= sym_crypt_keyslot_destroy(setup
->crypt_device
, i
);
3659 if (r
< 0 && !IN_SET(r
, -ENOENT
, -EINVAL
)) /* Returns EINVAL or ENOENT if there's no key in this slot already */
3660 return log_error_errno(r
, "Failed to destroy LUKS password: %m");
3662 if (i
>= n_effective
) {
3664 log_info("Destroyed LUKS key slot %zu.", i
);
3668 if (password_cache_contains(cache
, effective_passwords
[i
])) { /* Is this a FIDO2 or PKCS#11 password? */
3669 log_debug("Using minimal PBKDF for slot %zu", i
);
3670 r
= sym_crypt_set_pbkdf_type(setup
->crypt_device
, &minimal_pbkdf
);
3672 log_debug("Using good PBKDF for slot %zu", i
);
3673 r
= sym_crypt_set_pbkdf_type(setup
->crypt_device
, &good_pbkdf
);
3676 return log_error_errno(r
, "Failed to tweak PBKDF for slot %zu: %m", i
);
3678 r
= sym_crypt_keyslot_add_by_volume_key(
3679 setup
->crypt_device
,
3683 effective_passwords
[i
],
3684 strlen(effective_passwords
[i
]));
3686 return log_error_errno(r
, "Failed to set up LUKS password: %m");
3688 log_info("Updated LUKS key slot %zu.", i
);
3694 int home_lock_luks(UserRecord
*h
, HomeSetup
*setup
) {
3700 assert(setup
->root_fd
< 0);
3701 assert(!setup
->crypt_device
);
3703 r
= acquire_open_luks_device(h
, setup
, /* graceful= */ false);
3707 log_info("Discovered used LUKS device %s.", setup
->dm_node
);
3709 assert_se(p
= user_record_home_directory(h
));
3710 r
= syncfs_path(AT_FDCWD
, p
);
3711 if (r
< 0) /* Snake oil, but let's better be safe than sorry */
3712 return log_error_errno(r
, "Failed to synchronize file system %s: %m", p
);
3714 log_info("File system synchronized.");
3716 /* Note that we don't invoke FIFREEZE here, it appears libcryptsetup/device-mapper already does that on its own for us */
3718 r
= sym_crypt_suspend(setup
->crypt_device
, setup
->dm_name
);
3720 return log_error_errno(r
, "Failed to suspend cryptsetup device: %s: %m", setup
->dm_node
);
3722 log_info("LUKS device suspended.");
3726 int home_unlock_luks(UserRecord
*h
, HomeSetup
*setup
, const PasswordCache
*cache
) {
3727 _cleanup_(keyring_unlinkp
) key_serial_t key_serial
= -1;
3728 _cleanup_(erase_and_freep
) void *vk
= NULL
;
3734 assert(!setup
->crypt_device
);
3736 r
= acquire_open_luks_device(h
, setup
, /* graceful= */ false);
3740 log_info("Discovered used LUKS device %s.", setup
->dm_node
);
3742 r
= sym_crypt_get_volume_key_size(setup
->crypt_device
);
3744 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to determine LUKS volume key size.");
3751 r
= luks_get_volume_key(h
, setup
->crypt_device
, cache
, vk
, &vks
, &key_serial
);
3753 return log_error_errno(r
, "No valid password for LUKS superblock.");
3755 return log_error_errno(r
, "Failed to unlock LUKS superblock: %m");
3757 r
= sym_crypt_resume_by_volume_key(setup
->crypt_device
, setup
->dm_name
, vk
, vks
);
3759 return log_error_errno(r
, "Failed to resume LUKS superblock: %m");
3761 TAKE_KEY_SERIAL(key_serial
); /* Leave key in kernel keyring */
3763 log_info("LUKS device resumed.");
3767 static int device_is_gone(HomeSetup
*setup
) {
3768 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
3774 if (!setup
->dm_node
)
3777 if (stat(setup
->dm_node
, &st
) < 0) {
3778 if (errno
!= ENOENT
)
3779 return log_error_errno(errno
, "Failed to stat block device node %s: %m", setup
->dm_node
);
3784 r
= sd_device_new_from_stat_rdev(&d
, &st
);
3787 return log_error_errno(errno
, "Failed to allocate device object from block device node %s: %m", setup
->dm_node
);
3795 static int device_monitor_handler(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
3796 HomeSetup
*setup
= ASSERT_PTR(userdata
);
3799 if (!device_for_action(device
, SD_DEVICE_REMOVE
))
3802 /* We don't really care for the device object passed to us, we just check if the device node still
3805 r
= device_is_gone(setup
);
3808 if (r
> 0) /* Yay! we are done! */
3809 (void) sd_event_exit(sd_device_monitor_get_event(monitor
), 0);
3814 int wait_for_block_device_gone(HomeSetup
*setup
, usec_t timeout_usec
) {
3815 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*m
= NULL
;
3816 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
3821 /* So here's the thing: we enable "deferred deactivation" on our dm-crypt volumes. This means they
3822 * are automatically torn down once not used anymore (i.e. once unmounted). Which is great. It also
3823 * means that when we deactivate a home directory and try to tear down the volume that backs it, it
3824 * possibly is already torn down or in the process of being torn down, since we race against the
3825 * automatic tearing down. Which is fine, we handle errors from that. However, we lose the ability to
3826 * naturally wait for the tear down operation to complete: if we are not the ones who tear down the
3827 * device we are also not the ones who naturally block on that operation. Hence let's add some code
3828 * to actively wait for the device to go away, via sd-device. We'll call this whenever tearing down a
3829 * LUKS device, to ensure the device is really really gone before we proceed. Net effect: "homectl
3830 * deactivate foo && homectl activate foo" will work reliably, i.e. deactivation immediately followed
3831 * by activation will work. Also, by the time deactivation completes we can guarantee that all data
3832 * is sync'ed down to the lowest block layer as all higher levels are fully and entirely
3835 if (!setup
->dm_name
)
3838 assert(setup
->dm_node
);
3839 log_debug("Waiting until %s disappears.", setup
->dm_node
);
3841 r
= sd_event_new(&event
);
3843 return log_error_errno(r
, "Failed to allocate event loop: %m");
3845 r
= sd_device_monitor_new(&m
);
3847 return log_error_errno(r
, "Failed to allocate device monitor: %m");
3849 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
, "block", "disk");
3851 return log_error_errno(r
, "Failed to configure device monitor match: %m");
3853 r
= sd_device_monitor_attach_event(m
, event
);
3855 return log_error_errno(r
, "Failed to attach device monitor to event loop: %m");
3857 r
= sd_device_monitor_start(m
, device_monitor_handler
, setup
);
3859 return log_error_errno(r
, "Failed to start device monitor: %m");
3861 r
= device_is_gone(setup
);
3865 log_debug("%s has already disappeared before entering wait loop.", setup
->dm_node
);
3866 return 0; /* gone already */
3869 if (timeout_usec
!= USEC_INFINITY
) {
3870 r
= sd_event_add_time_relative(event
, NULL
, CLOCK_MONOTONIC
, timeout_usec
, 0, NULL
, NULL
);
3872 return log_error_errno(r
, "Failed to add timer event: %m");
3875 r
= sd_event_loop(event
);
3877 return log_error_errno(r
, "Failed to run event loop: %m");
3879 r
= device_is_gone(setup
);
3883 return log_error_errno(r
, "Device %s still around.", setup
->dm_node
);
3885 log_debug("Successfully waited until device %s disappeared.", setup
->dm_node
);
3889 int home_auto_shrink_luks(UserRecord
*h
, HomeSetup
*setup
, PasswordCache
*cache
) {
3894 assert(user_record_storage(h
) == USER_LUKS
);
3896 assert(setup
->root_fd
>= 0);
3898 if (user_record_auto_resize_mode(h
) != AUTO_RESIZE_SHRINK_AND_GROW
)
3901 if (fstatfs(setup
->root_fd
, &sfs
) < 0)
3902 return log_error_errno(errno
, "Failed to statfs home directory: %m");
3904 if (!fs_can_online_shrink_and_grow(sfs
.f_type
)) {
3905 log_debug("Not auto-shrinking file system, since selected file system cannot do both online shrink and grow.");
3909 r
= home_resize_luks(
3911 HOME_SETUP_ALREADY_ACTIVATED
|
3912 HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES
|
3913 HOME_SETUP_RESIZE_MINIMIZE
|
3914 HOME_SETUP_RESIZE_DONT_GROW
|
3915 HOME_SETUP_RESIZE_DONT_UNDO
,
3925 uint64_t luks_volume_key_size_convert(struct crypt_device
*cd
) {
3930 /* Convert the "int" to uint64_t, which we usually use for byte sizes stored on disk. */
3932 k
= sym_crypt_get_volume_key_size(cd
);
3936 return (uint64_t) k
;