Public repo to distribute scripts and config's
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

211 строки
5.9 KiB

  1. #!/bin/bash
  2. # This was I input to o1-preview with this prompt:
  3. #
  4. # BLOCKDEV=nvme0n1
  5. # wipefs -a -f /dev/${BLOCKDEV}
  6. # parted -s /dev/${BLOCKDEV} "mklabel gpt mkpart zosboot fat32 1 10M set 1 esp on mkpart zoscache btrfs 10M 100%"
  7. # mkfs.vfat -F32 /dev/${BLOCKDEV}p1
  8. # mkfs.btrfs -f /dev/${BLOCKDEV}p2
  9. # mkdir zospxe && mount /dev/${BLOCKDEV}p1 -t vfat zospxe && mkdir -p zospxe/efi/boot
  10. # wget -q https://bootstrap.grid.tf/uefi/prod/1234 -O zospxe/efi/boot/BOOTX64.EFI
  11. #
  12. # find a BLOCKDEV that is at least an nvme, ssd (in that order)
  13. # and make this script a bit more log-expressive, and failing gracefully.
  14. # Also, can you add the if -d $device is specified, it will use that and
  15. # not search for a disk, and also make farmerid a parameter
  16. # for https://bootstrap.grid.tf/uefi/prod/$farmerid}
  17. # Exit immediately if a command exits with a non-zero status.
  18. set -e
  19. # Function to log messages with timestamps
  20. log() {
  21. echo "$(date '+%F %T') - $@"
  22. }
  23. # Function to display usage information
  24. usage() {
  25. echo "Usage: $0 [-d DEVICE] [-f FARMERID]"
  26. echo " -d, --device DEVICE Specify the block device to use (e.g., sda, nvme0n1)."
  27. echo " -f, --farmerid FARMERID Specify the farmer ID for the download URL."
  28. echo " -h, --help Display this help message."
  29. exit 1
  30. }
  31. # Function to find a suitable block device
  32. find_blockdev() {
  33. # Minimum size in bytes (e.g., 10 GiB)
  34. MIN_SIZE=$((10 * 1024 * 1024 * 1024))
  35. # First try NVMe devices
  36. log "Searching for NVMe devices..."
  37. for DEV in /dev/nvme*n1; do
  38. if [ -b "$DEV" ]; then
  39. SIZE_BYTES=$(blockdev --getsize64 "$DEV")
  40. if [ "$SIZE_BYTES" -ge "$MIN_SIZE" ]; then
  41. log "Found NVMe device: $DEV ($(numfmt --to=iec $SIZE_BYTES))"
  42. BLOCKDEV="${DEV##*/}"
  43. return 0
  44. else
  45. log "$DEV is smaller than $(numfmt --to=iec $MIN_SIZE). Skipping."
  46. fi
  47. fi
  48. done
  49. log "No suitable NVMe devices found. Searching for SSD devices..."
  50. for DEV_PATH in /sys/block/*; do
  51. DEV_NAME=$(basename "$DEV_PATH")
  52. # Exclude unwanted devices
  53. case "$DEV_NAME" in
  54. loop* | ram* | sr* | fd* | md* | dm-*)
  55. continue
  56. ;;
  57. esac
  58. DEVICE="/dev/$DEV_NAME"
  59. if [ -b "$DEVICE" ] && [ "$(cat /sys/block/${DEV_NAME}/queue/rotational)" == "0" ]; then
  60. SIZE_BYTES=$(blockdev --getsize64 "$DEVICE")
  61. if [ "$SIZE_BYTES" -ge "$MIN_SIZE" ]; then
  62. log "Found SSD device: $DEVICE ($(numfmt --to=iec $SIZE_BYTES))"
  63. BLOCKDEV="$DEV_NAME"
  64. return 0
  65. else
  66. log "$DEVICE is smaller than $(numfmt --to=iec $MIN_SIZE). Skipping."
  67. fi
  68. fi
  69. done
  70. log "No suitable block devices found."
  71. return 1
  72. }
  73. # Main script execution
  74. main() {
  75. # Default values
  76. BLOCKDEV_SPECIFIED=""
  77. FARMERID="1234" # Default farmer ID
  78. # Parse command-line arguments
  79. while [[ $# -gt 0 ]]; do
  80. case "$1" in
  81. -d | --device)
  82. shift
  83. if [ -z "$1" ]; then
  84. log "Error: '--device' requires a non-empty option argument."
  85. usage
  86. fi
  87. BLOCKDEV_SPECIFIED="$1"
  88. shift
  89. ;;
  90. -f | --farmerid)
  91. shift
  92. if [ -z "$1" ]; then
  93. log "Error: '--farmerid' requires a non-empty option argument."
  94. usage
  95. fi
  96. FARMERID="$1"
  97. shift
  98. ;;
  99. -h | --help)
  100. usage
  101. ;;
  102. *)
  103. log "Error: Unknown option: $1"
  104. usage
  105. ;;
  106. esac
  107. done
  108. if [ -n "$BLOCKDEV_SPECIFIED" ]; then
  109. # Use the specified block device
  110. if [[ "$BLOCKDEV_SPECIFIED" != /dev/* ]]; then
  111. BLOCKDEV_SPECIFIED="/dev/$BLOCKDEV_SPECIFIED"
  112. fi
  113. if [ ! -b "$BLOCKDEV_SPECIFIED" ]; then
  114. log "Error: Specified device $BLOCKDEV_SPECIFIED does not exist or is not a block device."
  115. exit 1
  116. fi
  117. BLOCKDEV="${BLOCKDEV_SPECIFIED##*/}"
  118. log "Using specified block device: $BLOCKDEV_SPECIFIED"
  119. else
  120. # Find a suitable block device
  121. if ! find_blockdev; then
  122. log "Error: No suitable block device found."
  123. exit 1
  124. fi
  125. log "Selected block device: /dev/${BLOCKDEV}"
  126. fi
  127. # Confirm with the user before proceeding
  128. read -p "This will erase all data on /dev/${BLOCKDEV}. Are you sure you want to proceed? (yes/[no]): " CONFIRM
  129. if [ "$CONFIRM" != "yes" ]; then
  130. log "Operation cancelled by user."
  131. exit 0
  132. fi
  133. # Determine partition naming convention
  134. if [[ $BLOCKDEV =~ [0-9]$ ]]; then
  135. PARTPREFIX="${BLOCKDEV}p"
  136. else
  137. PARTPREFIX="${BLOCKDEV}"
  138. fi
  139. # Wipe filesystem signatures
  140. log "Wiping filesystem signatures on /dev/${BLOCKDEV}..."
  141. wipefs -a -f /dev/${BLOCKDEV}
  142. # Create GPT partition table
  143. log "Creating GPT partition table on /dev/${BLOCKDEV}..."
  144. parted -s /dev/${BLOCKDEV} mklabel gpt
  145. # Create partitions
  146. log "Creating partition 1 (zosboot) on /dev/${BLOCKDEV}..."
  147. parted -s /dev/${BLOCKDEV} mkpart zosboot fat32 1MiB 10MiB
  148. log "Setting partition 1 as ESP..."
  149. parted -s /dev/${BLOCKDEV} set 1 esp on
  150. log "Creating partition 2 (zos-cache) on /dev/${BLOCKDEV}..."
  151. parted -s /dev/${BLOCKDEV} mkpart zos-cache btrfs 10MiB 100%
  152. # Format partitions
  153. log "Formatting /dev/${PARTPREFIX}1 as FAT32..."
  154. mkfs.vfat -F32 /dev/${PARTPREFIX}1
  155. log "Formatting /dev/${PARTPREFIX}2 as BTRFS..."
  156. mkfs.btrfs -f /dev/${PARTPREFIX}2
  157. # Mount and prepare directories
  158. MOUNT_POINT="zospxe"
  159. log "Creating mount point at ./${MOUNT_POINT}..."
  160. mkdir -p "${MOUNT_POINT}"
  161. log "Mounting /dev/${PARTPREFIX}1 to ./${MOUNT_POINT}..."
  162. mount /dev/${PARTPREFIX}1 "${MOUNT_POINT}"
  163. log "Creating directory structure in ${MOUNT_POINT}..."
  164. mkdir -p "${MOUNT_POINT}/efi/boot"
  165. # Download the required BOOTX64.EFI file
  166. EFI_FILE_URL="https://bootstrap.grid.tf/uefi/prod/${FARMERID}"
  167. log "Downloading BOOTX64.EFI from ${EFI_FILE_URL}..."
  168. wget -q "${EFI_FILE_URL}" -O "${MOUNT_POINT}/efi/boot/BOOTX64.EFI"
  169. # Create zos-cache subvolume
  170. log "Creating mount point at ./${MOUNT_POINT}root..."
  171. mkdir -p "${MOUNT_POINT}root"
  172. log "Mounting /dev/${PARTPREFIX}2 to ./${MOUNT_POINT}root..."
  173. mount /dev/${PARTPREFIX}2 "${MOUNT_POINT}root"
  174. log "Creating subvolume \"zos-cache\" on ${MOUNT_POINT}root"
  175. btrfs subvolume create ${MOUNT_POINT}root/zos-cache
  176. # Umount mountpoints
  177. log "Umounting ${MOUNT_POINT}/*"
  178. umount -l ${MOUNT_POINT}
  179. umount -l ${MOUNT_POINT}root
  180. log "Operation completed successfully."
  181. }
  182. # Execute the main function
  183. main "$@"