#!/bin/bash # disk_partition_and_image_writer.sh # A script to automate partitioning, formatting, and image writing for a specified disk. # Usage: ./disk_partition_and_image_writer.sh /dev/sdX /path/to/images # Note: Run this script as root. # Ensure the script is run as root if [[ $EUID -ne 0 ]]; then echo "Please run this script as root." exit 1 fi # Check if the required arguments are provided if [[ "$#" -ne 2 ]]; then echo "Usage: $0 " echo "Example: $0 /dev/sda /path/to/images" exit 1 fi DISK="$1" IMG_DIR="$2" # Check if the disk exists if [[ ! -b $DISK ]]; then echo "Device $DISK does not exist. Please check the disk path." exit 1 fi # Check if the image directory exists if [[ ! -d $IMG_DIR ]]; then echo "Image directory $IMG_DIR does not exist. Please check the path." exit 1 fi # Wipe existing partition table and file system signatures echo "Wiping existing data on $DISK..." wipefs -a "$DISK" || { echo "Failed to wipe $DISK. Exiting."; exit 1; } dd if=/dev/zero of="$DISK" bs=1M count=10 || { echo "Failed to zero out $DISK. Exiting."; exit 1; } sync # Use fdisk to create partitions echo "Starting automated partitioning on $DISK..." ( echo g # Create a new GPT partition table echo n # Create partition 1 echo # Default partition number (1) echo 2048 # Start sector echo $((2048 + 6291456 - 1)) # End sector for 3G partition echo n # Create partition 2 echo # Default partition number (2) echo 6293504 # Start sector echo $((6293504 + 2097152 - 1)) # End sector for 1G partition echo n # Create partition 3 echo # Default partition number (3) echo 8390656 # Start sector echo $((8390656 + 1024000 - 1)) # End sector for 500M partition echo n # Create partition 4 echo # Default partition number (4) echo 9414656 # Start sector echo $((9414656 + 1024000 - 1)) # End sector for 500M partition echo n # Create partition 5 echo # Default partition number (5) echo 10438656 # Start sector echo $((10438656 + 1024000 - 1)) # End sector for 500M partition echo n # Create partition 6 echo # Default partition number (6) echo 11462656 # Start sector echo $((11462656 + 6291456 - 1)) # End sector for 3G partition echo n # Create partition 7 echo # Default partition number (7) echo 17754112 # Start sector echo $((17754112 + 1024000 - 1)) # End sector for 500M partition echo n # Create partition 8 (EFI System) echo # Default partition number (8) echo 18778112 # Start sector echo $((18778112 + 1024000 - 1)) # End sector for 500M partition echo t # Change partition type echo 8 # Partition number 8 echo 1 # Set type to EFI System echo w # Write changes and exit ) | fdisk "$DISK" ## Format each partition to ext4 or vfat with specified label and block size mkfs.ext4 -F -L "system" -b 4096 "${DISK}1" && echo "Formatted ${DISK}1 with label 'system' and block size 4096." mkfs.ext4 -F -L "vendor" -b 4096 "${DISK}2" && echo "Formatted ${DISK}2 with label 'vendor' and block size 4096." mkfs.ext4 -F -L "sys_prod" -b 4096 "${DISK}4" && echo "Formatted ${DISK}4 with label 'sys_prod' and block size 4096." mkfs.ext4 -F -L "chip_prod" -b 4096 "${DISK}5" && echo "Formatted ${DISK}5 with label 'chip_prod' and block size 4096." mkfs.ext4 -F -L "datafs" -b 4096 "${DISK}6" && echo "Formatted ${DISK}6 with label 'datafs' and block size 4096." mkfs.ext4 -F -L "rootfs" -b 4096 "${DISK}7" && echo "Formatted ${DISK}7 with label 'rootfs' and block size 4096." mkfs.ext4 -F -b 4096 "${DISK}3" && echo "Formatted ${DISK}3 with block size 4096." mkfs.ext4 -F -b 4096 "${DISK}6" && echo "Formatted ${DISK}6 with block size 4096." mkfs.vfat -F 32 -n "EFI" "${DISK}8" && echo "Formatted ${DISK}8 with label 'EFI' as vfat." # Display the partition table (optional) # fdisk -l "$DISK" echo "Automated partitioning and formatting complete." # Begin system image writing process echo "Begin system image writing process" declare -A img_map=( ["${DISK}1"]="$IMG_DIR/system.img" ["${DISK}2"]="$IMG_DIR/vendor.img" ["${DISK}3"]="$IMG_DIR/chip_ckm.img" ["${DISK}4"]="$IMG_DIR/sys_prod.img" ["${DISK}5"]="$IMG_DIR/chip_prod.img" ["${DISK}7"]="$IMG_DIR/root.img" ["${DISK}8"]="$IMG_DIR/boot.img" ) for partition in "${!img_map[@]}"; do img_file="${img_map[$partition]}" if [[ -f "$img_file" ]]; then echo "Writing $img_file to $partition" sudo dd if="$img_file" of="$partition" bs=4M status=progress && sync else echo "Image file $img_file does not exist!" fi done echo "All operations are complete."