Skip to content

Instantly share code, notes, and snippets.

@satmandu
Last active October 22, 2019 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satmandu/5d8e5886881b2e92c203a122f5297fa4 to your computer and use it in GitHub Desktop.
Save satmandu/5d8e5886881b2e92c203a122f5297fa4 to your computer and use it in GitHub Desktop.
#!/bin/bash -ex
# Hack to add user USERDATA zpool to an Ubuntu 19.10 system.
# I have this at /usr/local/bin/zfsuseradd.sh
user="${1}"
[[ -n "$user" ]] || (echo "User not specified." && exit 1)
zfs_user_suffix=$(mount | grep rpool/USERDATA/root_ | awk '{print $1}' | sed 's/rpool\/USERDATA\/root_//')
[[ -n "$zfs_user_suffix" ]] || (echo "Can't get Ubuntu ZFS user suffix." && exit 1)
zfs_system_suffix=$(mount | grep rpool/ROOT/ubuntu_ | awk '{print $1}' | head -1 | sed 's/rpool\/ROOT\/ubuntu_//')
[[ -n "$zfs_system_suffix" ]] || (echo "Can't get Ubuntu ZFS system suffix." && exit 1)
echo "creating rpool/USERDATA/""${user}""_""${zfs_user_suffix}"""
[[ -e /"${user}"_"${zfs_user_suffix}" ]] && (echo "mount point already exists" && exit 1)
zfs create rpool/USERDATA/"${user}"_"${zfs_user_suffix}"
zfs set mountpoint=/"${user}"_"${zfs_user_suffix}" \
rpool/USERDATA/"${user}"_"${zfs_user_suffix}"
# Set local properties created by ubuntu installer for userdata zpools
# set canmount=on since otherwise these don't appear to get mounted at boot.
# canmount=on is default. In theory this can be set to on by:
# zfs inherit -S canmount rpool/USERDATA/$user_$zfs_user_suffix
# zfs set canmount=on \
# rpool/USERDATA/"${user}"_"${zfs_user_suffix}"
zfs set com.ubuntu.zsys:bootfs-datasets=rpool/ROOT/ubuntu_"${zfs_system_suffix}" \
rpool/USERDATA/"${user}"_"${zfs_user_suffix}"
rsync -haHAX /home/"${user}"/ /"${user}"_"${zfs_user_suffix}"
rm -r /home/"${user:?}"
chown "${user}"."${user}" /"${user}"_"${zfs_user_suffix}"
zfs set mountpoint=/home/"${user}" rpool/USERDATA/"${user}"_"${zfs_user_suffix}"
@satmandu
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment