Advertisement
Guest User

Untitled

a guest
Nov 30th, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.53 KB | Source Code | 0 0
  1. #!/bin/bash
  2. set -e
  3.  
  4.  
  5. # Check if script is run as root
  6. if [ "$EUID" -ne 0 ]; then
  7.     echo "Please run as root"
  8.     exit 1
  9. fi
  10.  
  11.  
  12. IMAGE_NAME="ArchLinuxARM-aarch64-odroid-m1s.img"
  13. MOUNT_POINT="/mnt/arch"
  14.  
  15.  
  16. # Create image file
  17. echo "Creating image file..."
  18. fallocate -l 4G "$IMAGE_NAME"
  19.  
  20.  
  21. # Set up loop device
  22. LOOP_DEV=$(losetup --find --show "$IMAGE_NAME")
  23. echo "Using loop device: $LOOP_DEV"
  24.  
  25.  
  26. # Create partition table and partitions
  27. echo "Creating partitions..."
  28. parted --script "$LOOP_DEV" mklabel msdos
  29. parted --script "$LOOP_DEV" mkpart primary ext2 0% 512M
  30. parted --script "$LOOP_DEV" mkpart primary ext4 512M 100%
  31.  
  32.  
  33. # Update partition table
  34. partx -u "$LOOP_DEV"
  35.  
  36.  
  37. # Format partitions
  38. echo "Formatting partitions..."
  39. mkfs.ext2 "${LOOP_DEV}p1"
  40. mkfs.ext4 -F "${LOOP_DEV}p2"
  41.  
  42.  
  43. # Create mount points and mount partitions
  44. echo "Mounting partitions..."
  45. mkdir -p "$MOUNT_POINT"
  46. mount "${LOOP_DEV}p2" "$MOUNT_POINT"
  47. mkdir -p "$MOUNT_POINT/boot"
  48. mount "${LOOP_DEV}p1" "$MOUNT_POINT/boot"
  49.  
  50.  
  51. # Download and extract root filesystem
  52. echo "Downloading and extracting root filesystem..."
  53. wget -c http://os.archlinuxarm.org/os/ArchLinuxARM-aarch64-latest.tar.gz
  54. tar -xpf ArchLinuxARM-aarch64-latest.tar.gz -C "$MOUNT_POINT"
  55.  
  56.  
  57. # Mount necessary filesystems for chroot
  58. echo "Mounting virtual filesystems..."
  59. mount -t proc none "$MOUNT_POINT/proc"
  60. mount -t sysfs none "$MOUNT_POINT/sys"
  61. mount -o bind /dev "$MOUNT_POINT/dev"
  62.  
  63.  
  64. # Backup and replace resolv.conf
  65. cp "$MOUNT_POINT/etc/resolv.conf" "$MOUNT_POINT/etc/resolv.conf.bak"
  66. cp /etc/resolv.conf "$MOUNT_POINT/etc/resolv.conf"
  67.  
  68.  
  69. # Generate initial ramdisk
  70. echo "Generating initial ramdisk..."
  71. mkinitcpio -k "$MOUNT_POINT/boot/Image" -g "$MOUNT_POINT/boot/initramfs-linux.img"
  72.  
  73.  
  74. # Chroot operations
  75. echo "Performing chroot operations..."
  76. cat << 'EOF' > "$MOUNT_POINT/setup.sh"
  77. #!/bin/bash
  78. set -e
  79. pacman-key --init
  80. pacman-key --populate
  81. pacman -Suy --noconfirm
  82. pacman -Scc --noconfirm
  83. EOF
  84. chmod +x "$MOUNT_POINT/setup.sh"
  85. chroot "$MOUNT_POINT" /setup.sh
  86.  
  87.  
  88. # Update boot files
  89. echo "Updating boot files..."
  90. cd "$MOUNT_POINT/boot"
  91. cp --sparse=never Image Image.new
  92. mv Image.new Image
  93. cp --sparse=never initramfs-linux.img initramfs-linux.img.new
  94. mv initramfs-linux.img.new initramfs-linux.img
  95.  
  96.  
  97. # Create boot.cmd for SD card
  98. echo "Creating boot.cmd..."
  99. cat << 'EOF' > "$MOUNT_POINT/boot/boot.cmd"
  100. setenv bootargs "root=/dev/mmcblk1p2 rw rootwait console=tty1 console=ttyS2,1500000"
  101. # Load the DTB
  102. load ${devtype} ${devnum}:${partition} ${fdt_addr_r} /dtbs/rockchip/rk3566-odroid-m1s.dtb
  103. fdt addr ${fdt_addr_r}
  104. # Load kernel and initramfs
  105. load ${devtype} ${devnum}:${partition} ${kernel_addr_r} /Image
  106. load ${devtype} ${devnum}:${partition} ${ramdisk_addr_r} /initramfs-linux.img
  107. setenv initrd_size ${filesize}
  108. # Boot with kernel and initramfs
  109. booti ${kernel_addr_r} ${ramdisk_addr_r}:${initrd_size} ${fdt_addr_r}
  110. EOF
  111.  
  112.  
  113. # Generate boot.scr from boot.cmd
  114. echo "Generating boot.scr..."
  115. mkimage -C none -A arm64 -T script -d "$MOUNT_POINT/boot/boot.cmd" "$MOUNT_POINT/boot/boot.scr"
  116.  
  117.  
  118. # Cleanup
  119. echo "Cleaning up..."
  120. rm "$MOUNT_POINT/setup.sh"
  121. rm "$MOUNT_POINT/etc/resolv.conf"
  122. mv "$MOUNT_POINT/etc/resolv.conf.bak" "$MOUNT_POINT/etc/resolv.conf"
  123.  
  124.  
  125. # Unmount everything
  126. echo "Unmounting filesystems..."
  127. umount "$MOUNT_POINT/dev"
  128. umount "$MOUNT_POINT/proc"
  129. umount "$MOUNT_POINT/sys"
  130. umount "$MOUNT_POINT/boot"
  131. umount "$MOUNT_POINT"
  132.  
  133.  
  134. # Detach loop device
  135. echo "Detaching loop device..."
  136. losetup --detach "$LOOP_DEV"
  137.  
  138.  
  139. echo "Image preparation complete!"
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement