summaryrefslogtreecommitdiff
path: root/user.mk
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2019-06-26 15:47:04 -0400
committerAndrew Cady <d@jerkface.net>2019-06-26 16:29:08 -0400
commit07accc3f94e74361f884f2c29556c9104f9da332 (patch)
tree86c7f073bb185b3b13e8fb6c642b24dbe1de7839 /user.mk
parentc7ab0efabdee29445c9496a8618379163cc7830b (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.mk32
1 files changed, 32 insertions, 0 deletions
diff --git a/user.mk b/user.mk
new file mode 100644
index 0000000..4292fe8
--- /dev/null
+++ b/user.mk
@@ -0,0 +1,32 @@
1user_username = u
2user_home_path := $(shell getent passwd "$(user_username)" | (IFS=: read _ _ _ _ _ homedir _; echo $$homedir))
3user_home_btrfs = $(user_home_path).btrfs
4user_home_bytes = 10GiB
5user_home_force_replace =
6
7user_exists = getent passwd "$(user_username)" >/dev/null
8
9is_btrfs = df ~$(user_username) -t btrfs >/dev/null
10
11user-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
16remove_user_home_btrfs = sudo mv -b "$(user_home_btrfs)" "$(user_home_btrfs)"~"$$(date -Is)"
17
18user-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
31mount-user-btrfs-home:
32 mountpoint -q "$(user_home_path)" || sudo mount -t btrfs "$(user_home_btrfs)" "$(user_home_path)"