188 lines
6.5 KiB
Bash
188 lines
6.5 KiB
Bash
#!/bin/bash
|
|
|
|
# Filesystem management module for bootc image creation
|
|
# This module handles container filesystem extraction and setup
|
|
|
|
# Common functions are sourced by the main script
|
|
|
|
# Extract container filesystem
|
|
extract_container_filesystem() {
|
|
log_info "Extracting container filesystem..."
|
|
|
|
# Change ownership to allow container to write
|
|
log_info "Setting directory permissions..."
|
|
sudo chown -R 1000:1000 "$WORK_DIR/mnt"
|
|
|
|
# Extract container filesystem directly
|
|
log_info "Extracting container filesystem..."
|
|
podman run --rm -v "$WORK_DIR/mnt:/extract" "$CONTAINER_IMAGE" \
|
|
/bin/bash -c "
|
|
cd /extract
|
|
echo 'Starting filesystem extraction...'
|
|
|
|
# Copy the main system directories with verbose output
|
|
echo 'Copying /usr...'
|
|
cp -rv /usr . 2>&1 || echo 'Copy of /usr failed'
|
|
|
|
echo 'Copying /etc...'
|
|
cp -rv /etc . 2>&1 || echo 'Copy of /etc failed'
|
|
|
|
echo 'Copying /var...'
|
|
cp -rv /var . 2>&1 || echo 'Copy of /var failed'
|
|
|
|
echo 'Copying /home...'
|
|
cp -rv /home . 2>&1 || echo 'Copy of /home failed'
|
|
|
|
echo 'Copying /root...'
|
|
cp -rv /root . 2>&1 || echo 'Copy of /root failed'
|
|
|
|
echo 'Copying /boot...'
|
|
cp -rv /boot . 2>&1 || echo 'Copy of /boot failed'
|
|
|
|
# Create symlinks for traditional compatibility (usr-merge structure)
|
|
echo 'Creating symlinks for usr-merge compatibility...'
|
|
if [ -d usr/bin ]; then
|
|
ln -sf usr/bin bin
|
|
echo 'Created bin -> usr/bin symlink'
|
|
fi
|
|
if [ -d usr/lib ]; then
|
|
ln -sf usr/lib lib
|
|
echo 'Created lib -> usr/lib symlink'
|
|
fi
|
|
if [ -d usr/lib64 ]; then
|
|
ln -sf usr/lib64 lib64
|
|
echo 'Created lib64 -> usr/lib64 symlink'
|
|
fi
|
|
if [ -d usr/sbin ]; then
|
|
ln -sf usr/sbin sbin
|
|
echo 'Created sbin -> usr/sbin symlink'
|
|
fi
|
|
|
|
echo 'Filesystem extraction completed'
|
|
echo 'Final directory listing:'
|
|
ls -la
|
|
echo 'Checking usr directory:'
|
|
ls -la usr/
|
|
echo 'Checking if critical directories exist:'
|
|
[ -d usr/lib ] && echo 'usr/lib exists' || echo 'usr/lib MISSING'
|
|
[ -d usr/sbin ] && echo 'usr/sbin exists' || echo 'usr/sbin MISSING'
|
|
[ -d usr/bin ] && echo 'usr/bin exists' || echo 'usr/bin MISSING'
|
|
echo 'Checking boot directory:'
|
|
ls -la boot/
|
|
"
|
|
|
|
# Restore proper ownership for the final image
|
|
log_info "Restoring proper ownership..."
|
|
sudo chown -R root:root "$WORK_DIR/mnt"
|
|
|
|
log_info "Filesystem extraction completed"
|
|
}
|
|
|
|
# Fix file permissions
|
|
fix_file_permissions() {
|
|
log_info "Fixing critical file permissions..."
|
|
|
|
# Fix sudo permissions
|
|
sudo chmod 4755 "$WORK_DIR/mnt/usr/bin/sudo" # Set setuid bit for sudo
|
|
sudo chown root:root "$WORK_DIR/mnt/usr/bin/sudo" # Ensure sudo is owned by root
|
|
|
|
# Fix home directory permissions (use UID 1000 which is typically the first user)
|
|
sudo chown -R 1000:1000 "$WORK_DIR/mnt/home/debian"
|
|
sudo chmod 755 "$WORK_DIR/mnt/home/debian"
|
|
|
|
# Ensure critical system tools have proper permissions
|
|
sudo chmod 755 "$WORK_DIR/mnt/usr/bin/ip" 2>/dev/null || echo "ip command not found"
|
|
sudo chmod 755 "$WORK_DIR/mnt/usr/bin/ping" 2>/dev/null || echo "ping command not found"
|
|
sudo chmod 755 "$WORK_DIR/mnt/bin/shutdown" 2>/dev/null || echo "shutdown command not found"
|
|
sudo chmod 755 "$WORK_DIR/mnt/bin/halt" 2>/dev/null || echo "halt command not found"
|
|
sudo chmod 755 "$WORK_DIR/mnt/bin/poweroff" 2>/dev/null || echo "poweroff command not found"
|
|
|
|
# Set proper permissions for system directories
|
|
sudo chmod 1777 "$WORK_DIR/mnt/tmp"
|
|
sudo chmod 755 "$WORK_DIR/mnt/proc" "$WORK_DIR/mnt/sys" "$WORK_DIR/mnt/dev" \
|
|
"$WORK_DIR/mnt/run" "$WORK_DIR/mnt/media" "$WORK_DIR/mnt/mnt"
|
|
|
|
log_info "File permissions fixed"
|
|
}
|
|
|
|
# Verify filesystem integrity
|
|
verify_filesystem_integrity() {
|
|
log_info "Verifying filesystem integrity..."
|
|
|
|
local critical_dirs=("usr" "etc" "var" "boot" "home")
|
|
local missing_dirs=()
|
|
|
|
for dir in "${critical_dirs[@]}"; do
|
|
if [ ! -d "$WORK_DIR/mnt/$dir" ]; then
|
|
missing_dirs+=("$dir")
|
|
fi
|
|
done
|
|
|
|
if [ ${#missing_dirs[@]} -gt 0 ]; then
|
|
log_error "Missing critical directories: ${missing_dirs[*]}"
|
|
return 1
|
|
fi
|
|
|
|
# Check for critical symlinks
|
|
local critical_symlinks=("bin" "lib" "sbin")
|
|
local missing_symlinks=()
|
|
|
|
for symlink in "${critical_symlinks[@]}"; do
|
|
if [ ! -L "$WORK_DIR/mnt/$symlink" ]; then
|
|
missing_symlinks+=("$symlink")
|
|
fi
|
|
done
|
|
|
|
if [ ${#missing_symlinks[@]} -gt 0 ]; then
|
|
log_error "Missing critical symlinks: ${missing_symlinks[*]}"
|
|
return 1
|
|
fi
|
|
|
|
# Check for kernel and initrd
|
|
if [ ! -f "$WORK_DIR/mnt/boot/vmlinuz-6.12.41+deb13-amd64" ]; then
|
|
log_error "Kernel not found in expected location"
|
|
return 1
|
|
fi
|
|
|
|
if [ ! -f "$WORK_DIR/mnt/boot/initrd.img-6.12.41+deb13-amd64" ]; then
|
|
log_error "Initrd not found in expected location"
|
|
return 1
|
|
fi
|
|
|
|
log_info "Filesystem integrity verified successfully"
|
|
return 0
|
|
}
|
|
|
|
# Show filesystem statistics
|
|
show_filesystem_stats() {
|
|
log_info "Filesystem statistics:"
|
|
|
|
echo "Directory sizes:"
|
|
du -sh "$WORK_DIR/mnt"/* 2>/dev/null | sort -hr
|
|
|
|
echo ""
|
|
echo "File counts:"
|
|
find "$WORK_DIR/mnt" -type f | wc -l | xargs echo "Total files:"
|
|
find "$WORK_DIR/mnt" -type d | wc -l | xargs echo "Total directories:"
|
|
find "$WORK_DIR/mnt" -type l | wc -l | xargs echo "Total symlinks:"
|
|
|
|
echo ""
|
|
echo "Largest files:"
|
|
find "$WORK_DIR/mnt" -type f -exec ls -lh {} + 2>/dev/null | sort -k5 -hr | head -10
|
|
}
|
|
|
|
# Print module usage
|
|
print_module_usage() {
|
|
echo "Usage: source modules/filesystem.sh"
|
|
echo "This module handles filesystem extraction and management."
|
|
echo ""
|
|
echo "Available functions:"
|
|
echo " extract_container_filesystem - Extract container filesystem"
|
|
echo " fix_file_permissions - Fix critical file permissions"
|
|
echo " verify_filesystem_integrity - Verify filesystem integrity"
|
|
echo " show_filesystem_stats - Display filesystem statistics"
|
|
echo ""
|
|
echo "Required variables:"
|
|
echo " CONTAINER_IMAGE - Container image to extract from"
|
|
echo " WORK_DIR - Working directory for operations"
|
|
}
|