Compare commits

...

15 Commits

Author SHA1 Message Date
Alex Tavarez
e968fc3ea3 Added more helpful information on usage of the zfs-media-drive script 2025-09-17 15:34:41 -04:00
Alex Tavarez
7f4e62ee4b Changed default ZFS pool name, created prompt-based conditional with a case allowing for custom datasets, added cases for custom encrypted dataset or symbolic link creation 2025-09-17 15:33:16 -04:00
Alex Tavarez
409b8e50f6 Abstracted dataset creation from within function to separate function 2025-09-17 12:55:16 -04:00
Alex Tavarez
9585df0209 Replaced function with default variable value 2025-09-17 12:34:56 -04:00
Alex Tavarez
92cfaebb66 Added an argument shift and simplified conditional case 2025-09-17 12:05:42 -04:00
Alex Tavarez
fb906b659c Added a position shift for script arguments, simplified conditional case 2025-09-17 12:03:40 -04:00
Alex Tavarez
61051d7a3e Finished a task 2025-09-15 14:42:38 -04:00
Alex Tavarez
1072a8a207 Renamed file 2025-09-15 14:41:25 -04:00
Alex Tavarez
00eb99291a Moved main conditional out of previously existing function, added hard-coded command line to own function, added script options loop 2025-09-15 14:40:15 -04:00
Alex Tavarez
39c92a7479 Properly added case for an absent subcommand 2025-09-15 14:29:13 -04:00
Alex Tavarez
3c0afdaf52 Added one of the flag loops to case wherein 'init' subcommand is absent 2025-09-15 14:27:17 -04:00
Alex Tavarez
8266185bbd Loop through option lists for changing defaults and adding arguments for newly created functions 2025-09-15 14:24:53 -04:00
Alex Tavarez
c6780025f6 Renamed script, and made what is being deleted conditional and based on user input for piped 'grep' command 2025-09-12 09:47:53 -04:00
Alex Tavarez
2f86d2c0a5 Renamed media drive script--rewrote to account for different cases in handling media drive 2025-09-12 09:46:23 -04:00
Alex Tavarez
784a67a883 Updated task as ongoing 2025-09-12 09:44:54 -04:00
6 changed files with 227 additions and 59 deletions

View File

@@ -3,8 +3,6 @@
#+language: en
* PLANNED
** TODO [#A] Modify shell scripts in ~${PROJECT_ROOT}/zfs~ such that assumptions regarding avaialble datasets are minimized
- This could be done by softcoding dataset and pool names or creating functions that specify through argument
** TODO [#A] Create a ~${PROJECT_ROOT}/skel~ directory
- Should contain a shell script setting new permanent but reversible XDG variables, copying the directory structure to existing user home directories, and setting it as default for newly created user home directories
- Should contain subdirectories that match the paths relative to user home directories for XDG variables
@@ -14,4 +12,6 @@
* IN PROGRESS
* FINISHED
* FINISHED
** DONE [#A] Modify shell scripts in ~${PROJECT_ROOT}/zfs~ such that assumptions regarding avaialble datasets are minimized
- This could be done by softcoding dataset and pool names or creating functions that specify through argument

View File

@@ -1,4 +0,0 @@
#!/bin/bash
set -euo pipefail
zfs list -t snapshot | grep 'apt-' | sed 's| .*$||g' | head -n -5 | xargs -d '\n' -I {} sudo zfs destroy {}

View File

@@ -1,52 +0,0 @@
#!/bin/bash
set -euo pipefail
# @TODO: Implement more soft-coding and interactivity to this script
sudo zpool create \
-o ashift=12 \
-o autotrim=on \
-o compatibility=openzfs-2.1-linux \
-O encryption=on \
-O keylocation=file:///etc/zfs/keys/zroot.key \
-O keyformat=passphrase \
-O acltype=posixacl \
-O xattr=sa \
-O dnodesize=auto \
-O compression=zstd-3 \
-O normalization=formD \
-O relatime=on \
-O canmount=off \
-O mountpoint=none \
"$2" \
/dev/disk/by-id/"${1}"-part1
DATASET=("${HOME}/Documents/.calibre" "${2}/LITERATURE")
mkdir -p "${DATASET[0]}"
sudo zfs create -o mountpoint="${DATASET[0]}" "${DATASET[1]}"
DATASET=("${HOME}/Videos" "${2}/FILM")
mkdir -p "${DATASET[0]}"
sudo zfs create -o mountpoint="${DATASET[0]}" "${DATASET[1]}"
DATASET=("${HOME}/.var/app/com.valvesoftware.Steam" "${2}/STEAMGAMES")
mkdir -p "${DATASET[0]}"
sudo zfs create -o mountpoint="${DATASET[0]}" "${DATASET[1]}"
DATASET=("${HOME}/.var/app/org.libretro.RetroArch" "${2}/RARCHGAMES")
mkdir -p "${DATASET[0]}"
sudo zfs create -o mountpoint="${DATASET[0]}" "${DATASET[1]}"
DATASET=("${HOME}/Games" "${2}/GAMES")
mkdir -p "${DATASET[0]}"
sudo zfs create -o mountpoint="${DATASET[0]}" "${DATASET[1]}"
ln -s "${HOME}/.var/app/com.valvesoftware.Steam" "${DATASET[0]}/Steam"
ln -s "${HOME}/.var/app/org.libretro.RetroArch" "${DATASET[0]}/libRetro"
DATASET=("${HOME}/.var/app/org.libretro.RetroArch" "${2}/RARCHGAMES")
mkdir -p "${DATASET[0]}"
sudo zfs create -o mountpoint="${DATASET[0]}" "${DATASET[1]}"
DATASET=("${HOME}/.XXX" "${2}/XXX")
mkdir -p "${DATASET[0]}"
sudo zfs create -o keylocation=prompt -o keyformat=passphrase -o mountpoint="${DATASET[0]}" "${DATASET[1]}"

21
zfs/zfs-clean.sh Normal file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
set -euo pipefail
REGEX='apt-'
if [ -z "$1" ]; then
exit 1
fi
if [ -n "$1" ] && [ "$1" == "upgrades" ]; then
shift
while getopts "r:" opt; do
case "$opt" in
r) REGEX=${OPTARG};;
*) exit 1;;
esac
done
zfs list -t snapshot | grep "${REGEX}" | sed 's| .*$||g' | head -n -5 | xargs -d '\n' -I {} sudo zfs destroy {}
fi

12
zfs/zfs-media-drive.help Normal file
View File

@@ -0,0 +1,12 @@
This script allows the creation of ZFS pool specifically for the purpose of media.
This pool is divided into datasets that allow useful storage management of that media.
Subcommands for this script are: init, enc, and link. The first creates the filesystem
structure for the ZFS-formatted drive. The second allows specifying encrypted datasets.
The last creates cross-dataset symbolic links at their corresponding paths.
-k Option to provide a custom path to the key for the encrypted filesystem. Equivalent to 'keylocation' option in ZFS pool creation.
-f Opton to provide a custom format for the aforementioned key. Equivalnt to 'keyformat' option in ZFS pool creation.
-c Option to specify custom ZFS compression algorithm for ZFS pool. Equivalent to 'compression' option in ZFS pool creation.
-v Option to specify custom ZFS version for compatibility purposes. Equivalent to 'compatibility' option in ZFS pool creation.
-p Option to provide a custom name for the created ZFS pool.
-d Specify the storage device, volume or partition to be given a pool.

191
zfs/zfs-media-drive.sh Normal file
View File

@@ -0,0 +1,191 @@
#!/bin/bash
set -euo pipefail
SCRIPT_ROOT=$(dirname "$0")
# @TODO: Implement more soft-coding and interactivity to this script
ZFS_POOL_NAME="medialib"
ZFS_COMPAT="openzfs-2.1-linux"
ZFS_KEY_LOC="file:///etc/zfs/keys/zroot.key"
ZFS_KEY_FORMAT="passphrase"
ZFS_COMPRESSION="zstd-3"
create_zpool () {
sudo zpool create \
-o ashift=12 \
-o autotrim=on \
-o compatibility="$3" \
-O encryption=on \
-O keylocation="$4" \
-O keyformat="$5" \
-O acltype=posixacl \
-O xattr=sa \
-O dnodesize=auto \
-O compression="$6" \
-O normalization=formD \
-O relatime=on \
-O canmount=off \
-O mountpoint=none \
"$2" \
/dev/disk/by-id/"${1}"
}
mkdataset () {
mkdir -p "$1"
sudo zfs create -v -o mountpoint="$1" "$2"
}
create_datasets () {
read -r -p "Provide custom datasets? (y/n): " CUSTOMIZE_DATASETS
if [[ "$CUSTOMIZE_DATASETS" =~ [Nn]* ]]; then
DATASET=("${HOME}/Documents/.calibre" "${1}/LITERATURE")
mkdataset "${DATASET[0]}" "${DATASET[1]}"
DATASET=("${HOME}/Videos" "${1}/FILM")
mkdataset "${DATASET[0]}" "${DATASET[1]}"
DATASET=("${HOME}/.var/app/com.valvesoftware.Steam" "${1}/STEAMGAMES")
mkdataset "${DATASET[0]}" "${DATASET[1]}"
DATASET=("${HOME}/.var/app/org.libretro.RetroArch" "${1}/RARCHGAMES")
mkdataset "${DATASET[0]}" "${DATASET[1]}"
DATASET=("${HOME}/Games" "${1}/GAMES")
mkdataset "${DATASET[0]}" "${DATASET[1]}"
ln -s "${HOME}/.var/app/com.valvesoftware.Steam" "${DATASET[0]}/Steam"
ln -s "${HOME}/.var/app/org.libretro.RetroArch" "${DATASET[0]}/libRetro"
DATASET=("${HOME}/.var/app/org.libretro.RetroArch" "${1}/RARCHGAMES")
mkdataset "${DATASET[0]}" "${DATASET[1]}"
DATASET=("${HOME}/.XXX" "${1}/XXX")
mkdir -p "${DATASET[0]}"
sudo zfs create -o keylocation=prompt -o keyformat=passphrase -o mountpoint="${DATASET[0]}" "${DATASET[1]}"
elif [[ "$CUSTOMIZE_DATASETS" =~ [Yy]* ]]; then
while true; do
read -r -a DATASET -p "Define a mount path and its corresponding dataset name or route, in that order: "
if [ ${#DATASET[@]} -eq 2 ]; then
mkdataset "${DATASET[0]}" "${1}"/"${DATASET[1]}"
read -r -p "Create another ZFS dataset? (y/n): " ANOTHER_DATASET
if [[ "$ANOTHER_DATASET" =~ [Yy]* ]]; then
continue
elif [[ "$ANOTHER_DATASET" =~ [Nn]* ]]; then
break
fi
else
printf "Error: must provide 2 arguments (i.e., mount path and dataset name or route)."
read -r -p "Try again? (y/n): " RETRY_PROMPT
if [[ "$RETRY_PROMPT" =~ [Yy]* ]]; then
continue
elif [[ "$RETRY_PROMPT" =~ [Nn]* ]]; then
break
fi
fi
done
else
exit 1
fi
}
print_menu () {
printf "%s" "$(<"${SCRIPT_ROOT}"/zfs-media-drive.help)"
}
if [ -z "$1" ]; then
printf "Subcommand must be provided"
exit 1
fi
if [ -n "$1" ] && [ "$1" == "init" ]; then
shift
while getopts "d:p:k:f:c:v:" opt; do
case $opt in
k) ZFS_KEY_LOC="${OPTARG}";;
f) ZFS_KEY_FORMAT="${OPTARG}";;
c) ZFS_COMPRESSION="${OPTARG}";;
v) ZFS_COMPAT="${OPTARG}";;
p) ZFS_POOL_NAME="${OPTARG}";;
d) ZFS_TARGET_DRIVE="${OPTARG}";;
*) exit 1;
esac
done
if [ -z "$ZFS_TARGET_DRIVE" ]; then
printf "Error: It is necessary to argue a -d option."
exit 1
fi
export ZFS_POOL_NAME
create_zpool "$ZFS_TARGET_DRIVE" "$ZFS_POOL_NAME" "$ZFS_COMPAT" "$ZFS_KEY_LOC" "$ZFS_KEY_FORMAT" "$ZFS_COMPRESSION"
printf "Following pool has been created: \n"
zpool list -v | grep "$ZFS_POOL_NAME"
create_datasets "$ZFS_POOL_NAME"
printf "Following datasets have been created: \n"
zfs list | grep "$ZFS_POOL_NAME"
elif [ -n "$1" ] && [ "$1" == "enc" ]; then
while true; do
read -r -a DATASET -p "Define a mount path and its corresponding dataset name or route, in that order: "
if [ ${#DATASET[@]} -eq 2 ]; then
mkdir -p "${DATASET[0]}"
sudo zfs create -v -o keylocation=prompt -o keyformat=passphrase -o mountpoint="${DATASET[0]}" "${ZFS_POOL_NAME}"/"${DATASET[1]}"
read -r -p "Create another ZFS dataset? (y/n): " ANOTHER_DATASET
if [[ "$ANOTHER_DATASET" =~ [Yy]* ]]; then
continue
elif [[ "$ANOTHER_DATASET" =~ [Nn]* ]]; then
break
fi
else
printf "Error: must provide 2 arguments (i.e., mount path and dataset name or route)."
read -r -p "Try again? (y/n): " RETRY_PROMPT
if [[ "$RETRY_PROMPT" =~ [Yy]* ]]; then
continue
elif [[ "$RETRY_PROMPT" =~ [Nn]* ]]; then
break
fi
fi
done
elif [ -n "$1" ] && [ "$1" == "link" ]; then
while true; do
read -r -a LINKAGE -p "Define a symbolic link source path and destination dataset: "
if [ ${#LINKAGE[@]} -eq 2 ]; then
if ! [ -d "${LINKAGE[0]}" ]; then
mkdir -p "${LINKAGE[0]}"
fi
ln -s "${LINKAGE[0]}" "${ZFS_POOL_NAME}"/"${LINKAGE[1]}"
read -r -p "Create another symbolic link? (y/n): " ANOTHER_LINKAGE
if [[ "$ANOTHER_LINKAGE" =~ [Yy]* ]]; then
continue
elif [[ "$ANOTHER_LINKAGE" =~ [Nn]* ]]; then
break
fi
else
printf "Error: must provide 2 arguments (i.e., source path and destination path)."
read -r -p "Try again? (y/n): " RETRY_PROMPT
if [[ "$RETRY_PROMPT" =~ [Yy]* ]]; then
continue
elif [[ "$RETRY_PROMPT" =~ [Nn]* ]]; then
break
fi
fi
done
else
while getopts "h" flag; do
case "$flag" in
h) print_menu;;
*) exit 1;;
esac
done
fi