#!/usr/bin/env bash # Convert directories to btrfs subvolumes usage() { echo "Usage: $0 [directory2 ...]" echo "Converts each directory to a btrfs subvolume." } btrfs_convert() { local d dir tmp_dir for d in "$@"; do dir="$d" tmp_dir="$dir.tmp" if [[ ! -d "$dir" ]]; then echo "[ERROR] Directory '$dir' does not exist. Skipping." continue fi if [[ -d "$tmp_dir" ]]; then echo "[ERROR] Temporary directory '$tmp_dir' already exists. Skipping '$dir'." continue fi echo "[INFO] Creating btrfs subvolume: '$tmp_dir'..." if ! btrfs subvolume create "$tmp_dir" &>/dev/null; then echo "[ERROR] Failed to create btrfs subvolume '$tmp_dir'. Skipping '$dir'." continue fi echo "[INFO] Moving contents from '$dir' to '$tmp_dir'..." if ! mv "$dir"/* "$tmp_dir"/ 2>/dev/null; then echo "[ERROR] Failed to move contents from '$dir' to '$tmp_dir'. Cleaning up." rm -rf "$tmp_dir" continue fi echo "[INFO] Removing original directory '$dir'..." if ! rm -rf "$dir"; then echo "[ERROR] Failed to remove '$dir'. Manual cleanup may be required." continue fi echo "[INFO] Renaming '$tmp_dir' to '$dir'..." if ! mv "$tmp_dir" "$dir"; then echo "[ERROR] Failed to rename '$tmp_dir' to '$dir'. Manual cleanup may be required." continue fi echo "[SUCCESS] Converted '$dir' to a btrfs subvolume." done } if [[ $# -lt 1 ]]; then usage exit 1 fi btrfs_convert "$@"