particle-os-tools/src/bootc/scriptlets/05-ostree.sh
robojerk 74c7bede5f Initial commit: Particle-OS tools repository
- Complete Particle-OS rebranding from uBlue-OS
- Professional installation system with standardized paths
- Self-initialization system with --init and --reset commands
- Enhanced error messages and dependency checking
- Comprehensive testing infrastructure
- All source scriptlets updated with runtime improvements
- Clean codebase with redundant files moved to archive
- Complete documentation suite
2025-07-11 21:14:33 -07:00

379 lines
No EOL
10 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# OSTree extension operations
# ComposeFS/OSTree interoperability and advanced OSTree operations
# OSTree container operations
ostree_container_operations() {
local action="$1"
shift
case "$action" in
"commit")
ostree_container_commit "$@"
;;
"pull")
ostree_container_pull "$@"
;;
"list")
ostree_container_list "$@"
;;
"diff")
ostree_container_diff "$@"
;;
"mount")
ostree_container_mount "$@"
;;
"unmount")
ostree_container_unmount "$@"
;;
*)
error_exit "Unknown ostree container action: $action"
;;
esac
}
# Create OSTree commit from container image
ostree_container_commit() {
local image_name="$1"
local ref_name="${2:-latest}"
if [[ -z "$image_name" ]]; then
error_exit "Container image name required"
fi
info "Creating OSTree commit from container: $image_name"
# Validate container first
if ! container_lint "$image_name"; then
error_exit "Container validation failed"
fi
# Create OSTree commit using ostree container commit
if ostree container commit "$image_name" "$ref_name"; then
success "OSTree commit created successfully: $ref_name"
info "Commit hash: $(ostree rev-parse "$ref_name")"
else
error_exit "Failed to create OSTree commit"
fi
}
# Pull container image to OSTree repository
ostree_container_pull() {
local image_name="$1"
local ref_name="${2:-latest}"
if [[ -z "$image_name" ]]; then
error_exit "Container image name required"
fi
info "Pulling container to OSTree repository: $image_name"
# Pull container using ostree container pull
if ostree container pull "$image_name" "$ref_name"; then
success "Container pulled successfully to OSTree repository"
info "Available as ref: $ref_name"
else
error_exit "Failed to pull container to OSTree repository"
fi
}
# List OSTree container references
ostree_container_list() {
info "Listing OSTree container references"
echo "=== OSTree Container Refs ==="
ostree refs --repo="$OSTREE_REPO" | grep "^container/" || info "No container references found"
echo -e "\n=== OSTree Commits ==="
ostree log --repo="$OSTREE_REPO" --oneline | head -10
}
# Show diff between container and current deployment
ostree_container_diff() {
local image_name="$1"
if [[ -z "$image_name" ]]; then
error_exit "Container image name required"
fi
info "Showing diff between container and current deployment: $image_name"
# Get current deployment
local current_deployment=$(ostree admin status | grep '^*' | awk '{print $2}')
if [[ -z "$current_deployment" ]]; then
error_exit "No current deployment found"
fi
info "Current deployment: $current_deployment"
# Create temporary commit for comparison
local temp_ref="temp-$(date +%s)"
if ostree container commit "$image_name" "$temp_ref"; then
echo "=== Diff: $current_deployment -> $image_name ==="
ostree diff "$current_deployment" "$temp_ref" || info "No differences found"
# Clean up temporary ref
ostree refs --repo="$OSTREE_REPO" --delete "$temp_ref"
else
error_exit "Failed to create temporary commit for diff"
fi
}
# Mount OSTree deployment for inspection
ostree_container_mount() {
local ref_name="$1"
local mount_point="${2:-/tmp/ostree-mount}"
if [[ -z "$ref_name" ]]; then
error_exit "OSTree reference name required"
fi
info "Mounting OSTree deployment: $ref_name at $mount_point"
# Create mount point
mkdir -p "$mount_point"
# Mount OSTree deployment
if ostree admin mount "$ref_name" "$mount_point"; then
success "OSTree deployment mounted at: $mount_point"
info "Use 'ostree admin unmount $mount_point' to unmount"
else
error_exit "Failed to mount OSTree deployment"
fi
}
# Unmount OSTree deployment
ostree_container_unmount() {
local mount_point="$1"
if [[ -z "$mount_point" ]]; then
mount_point="/tmp/ostree-mount"
fi
info "Unmounting OSTree deployment from: $mount_point"
if ostree admin unmount "$mount_point"; then
success "OSTree deployment unmounted from: $mount_point"
rmdir "$mount_point" 2>/dev/null || true
else
error_exit "Failed to unmount OSTree deployment"
fi
}
# ComposeFS backend operations
composefs_operations() {
local action="$1"
shift
case "$action" in
"enable")
enable_composefs_backend
;;
"disable")
disable_composefs_backend
;;
"status")
check_composefs_status
;;
"convert")
convert_to_composefs "$@"
;;
*)
error_exit "Unknown composefs action: $action"
;;
esac
}
# Enable ComposeFS backend for OSTree
enable_composefs_backend() {
info "Enabling ComposeFS backend for OSTree"
# Check if composefs is available
if ! command -v composefs &>/dev/null; then
error_exit "composefs not available - install composefs package"
fi
# Check current OSTree configuration
local ostree_conf="/etc/ostree/ostree.conf"
if [[ -f "$ostree_conf" ]]; then
if grep -q "composefs" "$ostree_conf"; then
info "ComposeFS backend already configured"
return 0
fi
fi
# Create OSTree configuration directory
mkdir -p /etc/ostree
# Add ComposeFS configuration
cat >> "$ostree_conf" << EOF
[core]
composefs=true
EOF
success "ComposeFS backend enabled in OSTree configuration"
info "New deployments will use ComposeFS backend"
}
# Disable ComposeFS backend for OSTree
disable_composefs_backend() {
info "Disabling ComposeFS backend for OSTree"
local ostree_conf="/etc/ostree/ostree.conf"
if [[ -f "$ostree_conf" ]]; then
# Remove composefs configuration
sed -i '/composefs=true/d' "$ostree_conf"
success "ComposeFS backend disabled in OSTree configuration"
else
info "No OSTree configuration found"
fi
}
# Check ComposeFS backend status
check_composefs_status() {
info "Checking ComposeFS backend status"
echo "=== ComposeFS Backend Status ==="
# Check if composefs binary is available
if command -v composefs &>/dev/null; then
success "✓ composefs binary available"
composefs --version
else
warning "✗ composefs binary not found"
fi
# Check OSTree configuration
local ostree_conf="/etc/ostree/ostree.conf"
if [[ -f "$ostree_conf" ]]; then
if grep -q "composefs=true" "$ostree_conf"; then
success "✓ ComposeFS backend enabled in OSTree configuration"
else
info " ComposeFS backend not enabled in OSTree configuration"
fi
else
info " No OSTree configuration file found"
fi
# Check current deployment
local current_deployment=$(ostree admin status | grep '^*' | awk '{print $2}')
if [[ -n "$current_deployment" ]]; then
echo -e "\n=== Current Deployment ==="
ostree admin status
fi
}
# Convert existing deployment to use ComposeFS
convert_to_composefs() {
local ref_name="$1"
if [[ -z "$ref_name" ]]; then
error_exit "OSTree reference name required"
fi
info "Converting deployment to use ComposeFS: $ref_name"
# Enable ComposeFS backend first
enable_composefs_backend
# Create new deployment with ComposeFS
local new_ref="${ref_name}-composefs"
if ostree commit --repo="$OSTREE_REPO" --branch="$new_ref" --tree=ref:"$ref_name"; then
success "Deployment converted to ComposeFS: $new_ref"
info "Use 'ostree admin deploy $new_ref' to activate"
else
error_exit "Failed to convert deployment to ComposeFS"
fi
}
# OSTree repository management
ostree_repo_operations() {
local action="$1"
shift
case "$action" in
"init")
init_ostree_repo
;;
"check")
check_ostree_repo
;;
"clean")
clean_ostree_repo "$@"
;;
"gc")
garbage_collect_ostree_repo
;;
*)
error_exit "Unknown ostree repo action: $action"
;;
esac
}
# Initialize OSTree repository
init_ostree_repo() {
info "Initializing OSTree repository"
if [[ -d "$OSTREE_REPO" ]]; then
info "OSTree repository already exists"
return 0
fi
if ostree admin init-fs "$OSTREE_REPO"; then
success "OSTree repository initialized"
else
error_exit "Failed to initialize OSTree repository"
fi
}
# Check OSTree repository health
check_ostree_repo() {
info "Checking OSTree repository health"
if [[ ! -d "$OSTREE_REPO" ]]; then
error_exit "OSTree repository not found: $OSTREE_REPO"
fi
echo "=== OSTree Repository Health ==="
# Check repository integrity
if ostree fsck --repo="$OSTREE_REPO"; then
success "✓ Repository integrity check passed"
else
error_exit "Repository integrity check failed"
fi
# Show repository statistics
echo -e "\n=== Repository Statistics ==="
ostree summary --repo="$OSTREE_REPO" --view
}
# Clean OSTree repository
clean_ostree_repo() {
local keep_refs="${1:-10}"
info "Cleaning OSTree repository (keeping $keep_refs references)"
# Remove old deployments
local deployments=($(ostree admin status | grep -v '^*' | awk '{print $2}' | tail -n +$((keep_refs + 1))))
for deployment in "${deployments[@]}"; do
if [[ -n "$deployment" ]]; then
info "Removing old deployment: $deployment"
ostree admin undeploy "$deployment" || warning "Failed to remove deployment: $deployment"
fi
done
success "OSTree repository cleanup completed"
}
# Garbage collect OSTree repository
garbage_collect_ostree_repo() {
info "Running garbage collection on OSTree repository"
if ostree admin cleanup --repo="$OSTREE_REPO"; then
success "Garbage collection completed"
else
error_exit "Garbage collection failed"
fi
}