diff options
author | Andrew Cady <d@jerkface.net> | 2019-06-26 15:47:04 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2019-06-26 16:29:08 -0400 |
commit | 07accc3f94e74361f884f2c29556c9104f9da332 (patch) | |
tree | 86c7f073bb185b3b13e8fb6c642b24dbe1de7839 /user.mk | |
parent | c7ab0efabdee29445c9496a8618379163cc7830b (diff) |
Add makefile target "user-account"
The target creates a user account and ensures its home directory is on a
btrfs filesystem (creating one if necessary).
Btrfs is necessary for fsmgr; it relies on "cp --reflink"
Diffstat (limited to 'user.mk')
-rw-r--r-- | user.mk | 32 |
1 files changed, 32 insertions, 0 deletions
@@ -0,0 +1,32 @@ | |||
1 | user_username = u | ||
2 | user_home_path := $(shell getent passwd "$(user_username)" | (IFS=: read _ _ _ _ _ homedir _; echo $$homedir)) | ||
3 | user_home_btrfs = $(user_home_path).btrfs | ||
4 | user_home_bytes = 10GiB | ||
5 | user_home_force_replace = | ||
6 | |||
7 | user_exists = getent passwd "$(user_username)" >/dev/null | ||
8 | |||
9 | is_btrfs = df ~$(user_username) -t btrfs >/dev/null | ||
10 | |||
11 | user-account: | ||
12 | [ "$(user_username)" ] | ||
13 | $(user_exists) || sudo useradd "$(user_username)" -G sudo -m -p '' -s /bin/bash | ||
14 | $(is_btrfs) || $(MAKE) mount-user-btrfs-home >/dev/null 2>&1 || $(MAKE) user-btrfs-home | ||
15 | |||
16 | remove_user_home_btrfs = sudo mv -b "$(user_home_btrfs)" "$(user_home_btrfs)"~"$$(date -Is)" | ||
17 | |||
18 | user-btrfs-home: | ||
19 | [ "$(user_home_path)" ] | ||
20 | [ ! -e "$(user_home_btrfs)" ] || $(if $(user_home_force_replace),$(remove_user_home_btrfs),false) | ||
21 | [ ! -e "$(user_home_btrfs)" ] | ||
22 | ! mountpoint -q "$(user_home_path)" || sudo umount "$(user_home_path)" | ||
23 | [ ! -e "$(user_home_path)" ] || sudo rmdir "$(user_home_path)" || sudo mv -b "$(user_home_path)" "$(user_home_path)"~"$$(date -Is)" | ||
24 | [ -e "$(user_home_btrfs)" ] || sudo fallocate -l "$(user_home_bytes)" "$(user_home_btrfs)" | ||
25 | sudo mkdir "$(user_home_path)" | ||
26 | sudo mkfs.btrfs "$(user_home_btrfs)" | ||
27 | sudo mount -t btrfs "$(user_home_btrfs)" "$(user_home_path)" | ||
28 | sudo rsync -a /etc/skel/ "$(user_home_path)"/ | ||
29 | sudo chown -R "$(user_username):" "$(user_home_path)" | ||
30 | |||
31 | mount-user-btrfs-home: | ||
32 | mountpoint -q "$(user_home_path)" || sudo mount -t btrfs "$(user_home_btrfs)" "$(user_home_path)" | ||