Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
2f88b24
refactor: direct translation from python to bash
aghiles-ait Nov 3, 2025
36e6f21
refactor: replace informative or fatal messages generated by echo cmd…
aghiles-ait Nov 3, 2025
765d75b
chore: add starting and ending logs
aghiles-ait Nov 3, 2025
f1a6292
refactor: add more context to existing log messages
aghiles-ait Nov 3, 2025
d37f137
refactor: factorize repeated partitioning, formatting, encryption of …
aghiles-ait Nov 3, 2025
5af596b
refactor: add additional logs for context
aghiles-ait Nov 3, 2025
e888ef5
chore: delete unused init.py file
aghiles-ait Nov 3, 2025
08c446d
refactor: enrich code with logs
aghiles-ait Nov 4, 2025
0781351
refactor: factorize displaying of mount directory
aghiles-ait Nov 4, 2025
9895ff3
refactor replace luksOpen with the more modern open cryptsetup command
aghiles-ait Nov 4, 2025
ee36b1a
refactor: factorize mount device operation in a single function
aghiles-ait Nov 4, 2025
e5030eb
refactot: factorize mount_device in a single place
aghiles-ait Nov 4, 2025
58cde85
refactor: add logs and comment
aghiles-ait Nov 4, 2025
c013d15
docs: add beginning commentary section
aghiles-ait Nov 4, 2025
bfa335d
chore: add additional context to comment
aghiles-ait Nov 4, 2025
8ea126a
refactor: delete encryptedDisk.py
aghiles-ait Nov 4, 2025
67479ca
refactor: change create_partition function to detect_or_create_partit…
aghiles-ait Nov 5, 2025
5fd7cda
chore: make grep case insensitive
aghiles-ait Nov 5, 2025
8eb7d12
refactor: use one line idiomatic logging when possible
aghiles-ait Nov 5, 2025
ba597dd
chore: rename "path" env variable to "mount_path" for more context
aghiles-ait Nov 6, 2025
e0338d1
refactor: factorize detect_or_create_partition() code
aghiles-ait Nov 7, 2025
03c2a63
refactor: delete unsupported keyType case
aghiles-ait Nov 7, 2025
5d26c93
refactor: reorder the code
aghiles-ait Nov 7, 2025
fe4fbf4
chore: add control about the pre-existence of mapper
aghiles-ait Nov 7, 2025
fab3652
refactor: remove irrelevant condition tests
aghiles-ait Nov 7, 2025
e20c084
chore: umount mount_path prior to execution
aghiles-ait Nov 7, 2025
4cfed20
refactor: reorder the code
aghiles-ait Nov 7, 2025
7d2d52c
fix: correct mistake in the header comment
aghiles-ait Nov 7, 2025
962ff3a
docs: add a warning readme.md
aghiles-ait Nov 7, 2025
24c28ed
refactor: make mappername value controlled by create_or_detect_partit…
aghiles-ait Nov 10, 2025
98a22d6
Revert "refactor: make mappername value controlled by create_or_detec…
aghiles-ait Nov 10, 2025
2ab6c84
refactor: make mappername value controlled by create_or_detect_partit…
aghiles-ait Nov 10, 2025
ae70276
refactor: merge code
aghiles-ait Nov 10, 2025
8f22fad
fix: add missing mappername modification
aghiles-ait Nov 10, 2025
6b5231e
fix: test existence of device_to_mount file after its assignment
aghiles-ait Nov 10, 2025
3c5f83f
chore: uniformize syntax between two for loops
aghiles-ait Nov 10, 2025
629c7f8
fix: add --key_file=- to cryptsetup
aghiles-ait Nov 10, 2025
ac8b4f0
chore: delete unnecessary ls command
aghiles-ait Nov 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed cvmassistants/disktool/__init__.py
Empty file.
89 changes: 0 additions & 89 deletions cvmassistants/disktool/encryptedDisk.py

This file was deleted.

150 changes: 150 additions & 0 deletions cvmassistants/disktool/encryptedDisk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#!/usr/bin/env bash
###############################################################################
# Script: encryptedDisk.sh
# Description: Configure encrypted or unencrypted disk partitions on Ubuntu systems (e.g., TDX environment)
#
# This script partitions, formats, and mounts disk devices. Supports both
# encrypted (LUKS) and unencrypted disks. Environment variables control behavior:
# `mount_path` (mount point), `disk` (device name), `keyType` (only wrapkey supported),
# and `wrapkey` (encryption key).
#
# Requirements:
# - Must be run as root
# - cryptsetup must be installed (for encrypted disks)
# - fdisk must be installed
# - mkfs.ext4 must be available
#
###############################################################################

log_info() {
echo -e "[INFO] $*"
}

log_fatal() {
echo -e "[ERROR] $*" >&2
exit 1
}

# Create a new partition on a disk
# Arguments: disk_device
detect_or_create_partition() {
local disk_dev="$1"
local suffix

# Try both possible partition naming schemes (e.g., /dev/sda1 or /dev/nvme0n1p1)
part_disk=""
for suffix in "1" "p1"; do
if [[ -e "${disk_dev}${suffix}" ]]; then
part_disk="${disk_dev}${suffix}"
mappername="${mappername}${suffix}"
log_info "Partition $part_disk already exists for device $disk_dev"
return 0
fi
done

log_info "Creating partition on $disk_dev with the following passed fdisk parameters:
n = new partition
p = primary partition
1 = partition number 1
<Enter><Enter> = default start and end sectors
w = write changes"
# Create the partition using fdisk
# fdisk may return non-zero due to partition table re-read warning, but partition is created
echo -e "n\np\n1\n\n\nw\n" | fdisk "$disk_dev" >/dev/null 2>&1 || true

# Force kernel to re-read the partition table
if command -v partprobe >/dev/null 2>&1; then
partprobe "$disk_dev" >/dev/null 2>&1 || log_fatal "partprobe failed on $disk_dev"
elif command -v partx >/dev/null 2>&1; then
partx -u "$disk_dev" >/dev/null 2>&1 || log_fatal "partx failed on $disk_dev"
fi

# Wait a moment for partition to appear
sleep 1

# Try both possible partition naming schemes
for suffix in "1" "p1"; do
part_disk="${disk_dev}${suffix}"
if [[ -e "$part_disk" ]]; then
mappername="${mappername}${suffix}"
log_info "Partition $part_disk successfully created on $disk_dev"
return 0
fi
done

log_fatal "Failed to create partition on $disk_dev — no partition device detected after fdisk"
}

# Format and encrypt a partition
# Arguments: key partition_device mapper_name
format_and_encrypt_partition() {
local key="$1"
local part_dev="$2"
local mapper="$3"

echo "$key" | cryptsetup luksFormat --key-file=- "$part_dev"
[[ $? -ne 0 ]] && log_fatal "Failed to format partition $part_dev in luks format"
log_info "Partition $part_dev formatted successfully in luks format"

echo "$key" | cryptsetup open --key-file=- "$part_dev" "$mapper"
[[ $? -ne 0 ]] && log_fatal "Failed to open partition $part_dev in luks format"
log_info "Partition $part_dev opened successfully in luks format"

mkfs.ext4 "/dev/mapper/$mapper"
[[ $? -ne 0 ]] && log_fatal "Failed to format partition /dev/mapper/$mapper in ext4 format"
log_info "Partition /dev/mapper/$mapper successfully formatted in ext4 format"

cryptsetup close "$mapper"
[[ $? -ne 0 ]] && log_fatal "Failed to close partition /dev/mapper/$mapper"
log_info "Partition /dev/mapper/$mapper closed successfully"
}

# Mount a device to a mount point
# Arguments: device_path mount_point
mount_device() {
local device="$1"
local mount_point="$2"

mount "$device" "$mount_point"
[[ $? -ne 0 ]] && log_fatal "Failed to mount $device to $mount_point"
log_info "Mounted $device to $mount_point"
}

log_info "Starting encrypted disk configuration..."

# Check required environment variables
[[ -z "$mount_path" ]] && log_fatal "Mount directory is null"
[[ -z "$disk" ]] && log_fatal "Disk dev name is null"
# Handle only encrypted disk case
[ "$keyType" != "wrapkey" ] && log_fatal "keyType $keyType is not supported"

log_info "Handling encrypted disk case"
[[ -z "$wrapkey" ]] && log_fatal "wrapkey is null"

if [ ! -d "$mount_path" ]; then
log_info "Mount directory $mount_path does not exist"
mkdir -p "$mount_path" && log_info "Created mount directory $mount_path"
else
umount "$mount_path" 2>/dev/null && log_info "Unmounted $mount_path"
fi

diskpath="/dev/$disk" # /dev/vda
part_disk=""

mappername="${disk}"
detect_or_create_partition "$diskpath" # assign part_disk and mappername
device_to_mount="/dev/mapper/$mappername"
[ -e "$device_to_mount" ] && log_fatal "Mapper $device_to_mount already exists"

# Format and encrypt the partition (and check if it opens correctly)
format_and_encrypt_partition "$wrapkey" "$part_disk" "$mappername"

# Open the encrypted device in its mapper
echo "$wrapkey" | cryptsetup open --key-file=- "$part_disk" "$mappername"
[[ $? -ne 0 ]] && log_fatal "cryptsetup open --key-file=- "$part_disk" "$mappername": failed"
log_info "cryptsetup open --key-file=- "$part_disk" "$mappername": success"

# Mount the device
mount_device "$device_to_mount" "$mount_path" && log_info "Mounted $device_to_mount to $mount_path"

log_info "Encrypted disk configuration completed."
8 changes: 8 additions & 0 deletions cvmassistants/disktool/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
**Warning:** ⚠️
This script will **forcefully format the specified partition**.
All existing data on the partition will be **permanently lost**.

Specifically:

- The disk is defined via the `disk` environment variable (e.g., `vda`).
- The partition affected is the **first partition** of that disk