#!/bin/bash # Output format conversion module for bootc image creation # This module handles conversion to different output formats (QCOW2, ISO, IMG) # Common functions are sourced by the main script # Convert to QCOW2 convert_to_qcow2() { log_info "Converting to QCOW2..." # Unmount before conversion sudo umount "$WORK_DIR/mnt" # Convert raw image to QCOW2 qemu-img convert -f raw -O qcow2 "$OUTPUT_DIR/debian-bootc.raw" "$OUTPUT_DIR/debian-bootc.qcow2" # Keep the raw image for VM use log_info "Raw image kept: $OUTPUT_DIR/debian-bootc.raw" log_info "QCOW2 image created: $OUTPUT_DIR/debian-bootc-simple.qcow2" # Show final image info ls -lh "$OUTPUT_DIR/debian-bootc.qcow2" } # Convert to ISO convert_to_iso() { log_info "Converting to ISO..." # Unmount before conversion sudo umount "$WORK_DIR/mnt" # Create temporary directory for ISO contents local iso_temp="$WORK_DIR/iso-temp" mkdir -p "$iso_temp" # Copy filesystem to temporary directory log_info "Preparing ISO contents..." sudo cp -r "$WORK_DIR/mnt"/* "$iso_temp/" # Create bootable ISO log_info "Creating bootable ISO..." genisoimage -o "$OUTPUT_DIR/debian-bootc.iso" \ -b isolinux/isolinux.bin \ -c isolinux/boot.cat \ -no-emul-boot \ -boot-load-size 4 \ -boot-info-table \ -r \ -J \ -v \ "$iso_temp" # Cleanup temporary directory sudo rm -rf "$iso_temp" log_info "ISO image created: $OUTPUT_DIR/debian-bootc.iso" ls -lh "$OUTPUT_DIR/debian-bootc.iso" } # Convert to IMG (raw format) convert_to_img() { log_info "Converting to IMG format..." # Unmount before conversion sudo umount "$WORK_DIR/mnt" # Copy raw image to IMG cp "$OUTPUT_DIR/debian-bootc.raw" "$OUTPUT_DIR/debian-bootc.img" log_info "IMG image created: $OUTPUT_DIR/debian-bootc.img" ls -lh "$OUTPUT_DIR/debian-bootc.img" } # Convert to VMDK (VMware format) convert_to_vmdk() { log_info "Converting to VMDK..." # Unmount before conversion sudo umount "$WORK_DIR/mnt" # Convert raw image to VMDK qemu-img convert -f raw -O vmdk "$OUTPUT_DIR/debian-bootc.raw" "$OUTPUT_DIR/debian-bootc.vmdk" log_info "VMDK image created: $OUTPUT_DIR/debian-bootc.vmdk" ls -lh "$OUTPUT_DIR/debian-bootc.vmdk" } # Convert to VDI (VirtualBox format) convert_to_vdi() { log_info "Converting to VDI..." # Unmount before conversion sudo umount "$WORK_DIR/mnt" # Convert raw image to VDI qemu-img convert -f raw -O vdi "$OUTPUT_DIR/debian-bootc.raw" "$OUTPUT_DIR/debian-bootc.vdi" log_info "VDI image created: $OUTPUT_DIR/debian-bootc.vdi" ls -lh "$OUTPUT_DIR/debian-bootc.vdi" } # Convert to multiple formats convert_to_all_formats() { log_info "Converting to all supported formats..." # Unmount before conversion sudo umount "$WORK_DIR/mnt" # Convert to all formats convert_to_qcow2 convert_to_iso convert_to_img convert_to_vmdk convert_to_vdi log_info "All format conversions completed!" # Show all created images echo "" log_info "Created images:" ls -lh "$OUTPUT_DIR"/debian-bootc.* } # Show format information show_format_info() { log_info "Format information:" if [ -f "$OUTPUT_DIR/debian-bootc.raw" ]; then echo "Raw image: $(ls -lh "$OUTPUT_DIR/debian-bootc.raw")" fi if [ -f "$OUTPUT_DIR/debian-bootc.qcow2" ]; then echo "QCOW2 image: $(ls -lh "$OUTPUT_DIR/debian-bootc.qcow2")" fi if [ -f "$OUTPUT_DIR/debian-bootc.iso" ]; then echo "ISO image: $(ls -lh "$OUTPUT_DIR/debian-bootc.iso")" fi if [ -f "$OUTPUT_DIR/debian-bootc.img" ]; then echo "IMG image: $(ls -lh "$OUTPUT_DIR/debian-bootc.img")" fi if [ -f "$OUTPUT_DIR/debian-bootc.vmdk" ]; then echo "VMDK image: $(ls -lh "$OUTPUT_DIR/debian-bootc.vmdk")" fi if [ -f "$OUTPUT_DIR/debian-bootc.vdi" ]; then echo "VDI image: $(ls -lh "$OUTPUT_DIR/debian-bootc.vdi")" fi } # Print module usage print_module_usage() { echo "Usage: source modules/formats.sh" echo "This module handles output format conversion." echo "" echo "Available functions:" echo " convert_to_qcow2 - Convert to QCOW2 format" echo " convert_to_iso - Convert to ISO format" echo " convert_to_img - Convert to IMG format" echo " convert_to_vmdk - Convert to VMDK format" echo " convert_to_vdi - Convert to VDI format" echo " convert_to_all_formats - Convert to all formats" echo " show_format_info - Display format information" echo "" echo "Required variables:" echo " OUTPUT_DIR - Output directory for images" echo " WORK_DIR - Working directory for operations" }