Advertisement
Guest User

Untitled

a guest
Dec 20th, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Exit on error
  4. set -e
  5.  
  6. # Function to clean up mounts on exit
  7. cleanup() {
  8. echo "Cleaning up mounts..."
  9.  
  10. # Define mount points in reverse order of mounting
  11. MOUNT_POINTS=(
  12. "/mnt/chroot/dev/pts"
  13. "/mnt/chroot/dev"
  14. "/mnt/chroot/sys"
  15. "/mnt/chroot/proc"
  16. "/mnt/chroot/var/log/audit"
  17. "/mnt/chroot/var/log"
  18. "/mnt/chroot/var/tmp"
  19. "/mnt/chroot/var"
  20. "/mnt/chroot/home"
  21. "/mnt/chroot/boot"
  22. "/mnt/chroot/boot/efi"
  23. "/mnt/chroot"
  24. )
  25.  
  26. # Unmount each point if it's mounted
  27. for mount_point in "${MOUNT_POINTS[@]}"; do
  28. if mountpoint -q "$mount_point"; then
  29. echo "Unmounting $mount_point"
  30. umount -f "$mount_point" || echo "Warning: Failed to unmount $mount_point"
  31. fi
  32. done
  33. }
  34.  
  35. # Set cleanup to run on script exit
  36. trap cleanup EXIT
  37.  
  38. # Check if running as root
  39. if [ "$(id -u)" -ne 0 ]; then
  40. echo "Error: This script must be run as root"
  41. exit 1
  42. fi
  43.  
  44. # Check for required tools and install if missing
  45. echo "Checking and installing required tools..."
  46. if ! rpm -q nvme-cli &>/dev/null; then
  47. dnf install -y nvme-cli
  48. fi
  49.  
  50. for tool in lsblk parted mkfs.xfs mkfs.fat lvcreate pvcreate vgcreate; do
  51. if ! command -v $tool >/dev/null 2>&1; then
  52. echo "Error: Required tool $tool is not installed"
  53. exit 1
  54. fi
  55. done
  56.  
  57. # Function to detect NVMe device
  58. detect_nvme_device() {
  59. echo "DEBUG: Starting device detection..."
  60.  
  61. # Get the root device with more detailed debug
  62. root_mount=$(mount | grep ' / ')
  63. echo "DEBUG: Root mount line: $root_mount"
  64. root_device=$(echo "$root_mount" | cut -d' ' -f1)
  65. echo "DEBUG: Root device is: $root_device"
  66.  
  67. # For AWS NVMe, we need to get the base device without any partition number
  68. root_disk=$(echo "$root_device" | sed -E 's/p?[0-9]+$//')
  69. echo "DEBUG: Root disk is: $root_disk"
  70.  
  71. # List all block devices
  72. echo "DEBUG: All block devices:"
  73. lsblk -p
  74.  
  75. # Find all NVMe devices
  76. mapfile -t nvme_devices < <(lsblk -d -n -o NAME,TYPE | grep " disk" | grep -E '^nvme[0-9]+n1' | cut -d' ' -f1)
  77. echo "DEBUG: Found NVMe devices: ${nvme_devices[*]}"
  78.  
  79. # Look for the non-root device
  80. for device in "${nvme_devices[@]}"; do
  81. device_path="/dev/$device"
  82. echo "DEBUG: Checking device: $device_path against root: $root_disk"
  83.  
  84. if [ "$device_path" = "$root_disk" ]; then
  85. echo "DEBUG: Skipping root disk: $device_path"
  86. continue
  87. fi
  88.  
  89. # Additional check to ensure device exists and is a block device
  90. if [ -b "$device_path" ]; then
  91. # Verify this device isn't mounted
  92. if ! lsblk "$device_path" | grep -q "part /" ; then
  93. TARGET_DEVICE="$device_path"
  94. echo "Selected new root device: ${TARGET_DEVICE}"
  95. return 0
  96. else
  97. echo "DEBUG: Device $device_path has mounted partitions, skipping"
  98. fi
  99. else
  100. echo "DEBUG: Device $device_path is not a block device or doesn't exist"
  101. fi
  102. done
  103.  
  104. echo "Error: No suitable device found for new root! Need an unpartitioned NVMe device."
  105. echo "DEBUG: Current block devices:"
  106. lsblk -p
  107. exit 1
  108. }
  109.  
  110. # Detect the device to use
  111. detect_nvme_device
  112.  
  113. # Create partitions on the new device
  114. echo "Creating partitions on ${TARGET_DEVICE}..."
  115. parted -s "${TARGET_DEVICE}" mklabel gpt
  116.  
  117. # Create aligned partitions
  118. parted -s "${TARGET_DEVICE}" unit s \
  119. mkpart primary 2048 4095 \
  120. mkpart ESP fat32 4096 198655 \
  121. mkpart primary xfs 198656 2246655 \
  122. mkpart primary 2246656 100%
  123.  
  124. # Set partition flags
  125. parted -s "${TARGET_DEVICE}" set 1 bios_grub on
  126. parted -s "${TARGET_DEVICE}" set 2 esp on
  127.  
  128. # Wait for device nodes
  129. sleep 5
  130.  
  131. # Format partitions
  132. mkfs.fat -F32 "${TARGET_DEVICE}p2"
  133. mkfs.xfs -f "${TARGET_DEVICE}p3"
  134.  
  135. # Setup LVM
  136. pvcreate -ff -y "${TARGET_DEVICE}p4"
  137. vgcreate RootVG "${TARGET_DEVICE}p4"
  138.  
  139. # Create logical volumes
  140. lvcreate -L 6G -n rootVol RootVG
  141. lvcreate -L 2G -n swapVol RootVG
  142. lvcreate -L 1G -n homeVol RootVG
  143. lvcreate -L 2G -n varVol RootVG
  144. lvcreate -L 2G -n varTmpVol RootVG
  145. lvcreate -L 2G -n logVol RootVG
  146. lvcreate -L 4G -n auditVol RootVG
  147.  
  148. # Format logical volumes
  149. mkfs.xfs -f /dev/RootVG/rootVol
  150. mkswap -f /dev/RootVG/swapVol
  151. mkfs.xfs -f /dev/RootVG/homeVol
  152. mkfs.xfs -f /dev/RootVG/varVol
  153. mkfs.xfs -f /dev/RootVG/varTmpVol
  154. mkfs.xfs -f /dev/RootVG/logVol
  155. mkfs.xfs -f /dev/RootVG/auditVol
  156.  
  157. # Create mount points and mount root filesystem
  158. echo "Creating mount points..."
  159. mkdir -p /mnt/chroot
  160.  
  161. echo "Mounting root filesystem..."
  162. mount /dev/RootVG/rootVol /mnt/chroot
  163.  
  164. # Create initial directories
  165. echo "Creating initial mount points..."
  166. mkdir -p /mnt/chroot/boot/efi
  167. mkdir -p /mnt/chroot/home
  168.  
  169. # Mount boot related filesystems
  170. echo "Mounting boot filesystems..."
  171. mount "${TARGET_DEVICE}p2" /mnt/chroot/boot/efi
  172. mount "${TARGET_DEVICE}p3" /mnt/chroot/boot
  173.  
  174. # Sync root filesystem content
  175. echo "Syncing root filesystem..."
  176. rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /mnt/chroot/
  177.  
  178. # Mount and sync home
  179. mount /dev/RootVG/homeVol /mnt/chroot/home
  180. rsync -aAXv /home/ /mnt/chroot/home/
  181.  
  182. # Create and mount var hierarchy
  183. echo "Setting up /var hierarchy..."
  184. mkdir -p /mnt/chroot/var
  185. mount /dev/RootVG/varVol /mnt/chroot/var
  186. rsync -aAXv /var/ /mnt/chroot/var/
  187.  
  188. # Create and mount /var/tmp
  189. mkdir -p /mnt/chroot/var/tmp
  190. mount /dev/RootVG/varTmpVol /mnt/chroot/var/tmp
  191. rsync -aAXv /var/tmp/ /mnt/chroot/var/tmp/
  192.  
  193. # Create and mount /var/log
  194. mkdir -p /mnt/chroot/var/log
  195. mount /dev/RootVG/logVol /mnt/chroot/var/log
  196. rsync -aAXv /var/log/ /mnt/chroot/var/log/
  197.  
  198. # Create and mount /var/log/audit
  199. mkdir -p /mnt/chroot/var/log/audit
  200. mount /dev/RootVG/auditVol /mnt/chroot/var/log/audit
  201. rsync -aAXv /var/log/audit/ /mnt/chroot/var/log/audit/
  202.  
  203. # Setup virtual filesystems
  204. echo "Setting up virtual filesystems..."
  205. mkdir -p /mnt/chroot/{proc,sys,dev/pts}
  206. mount -t proc proc /mnt/chroot/proc
  207. mount -t sysfs sys /mnt/chroot/sys
  208. mount -o bind /dev /mnt/chroot/dev
  209. mount -o bind /dev/pts /mnt/chroot/dev/pts
  210.  
  211. # Ensure etc directory exists and copy resolv.conf
  212. mkdir -p /mnt/chroot/etc
  213. cp /etc/resolv.conf /mnt/chroot/etc/
  214.  
  215. # Create new fstab
  216. cat > /mnt/chroot/etc/fstab << EOF
  217. # Device Mountpoint FStype Options Dump Pass
  218. ${TARGET_DEVICE}p2 /boot/efi vfat umask=0077,shortname=winnt 0 2
  219. ${TARGET_DEVICE}p3 /boot xfs defaults 0 0
  220. /dev/mapper/RootVG-rootVol / xfs defaults 0 0
  221. /dev/mapper/RootVG-swapVol swap swap defaults 0 0
  222. /dev/mapper/RootVG-homeVol /home xfs defaults,nosuid,noexec,nodev 0 0
  223. /dev/mapper/RootVG-varVol /var xfs defaults,nodev 0 0
  224. /dev/mapper/RootVG-varTmpVol /var/tmp xfs defaults,nodev,nosuid,noexec 0 0
  225. /dev/mapper/RootVG-logVol /var/log xfs defaults,nodev,nosuid,noexec 0 0
  226. /dev/mapper/RootVG-auditVol /var/log/audit xfs defaults,nodev,nosuid,noexec 0 0
  227. tmpfs /dev/shm tmpfs defaults,nodev,nosuid,noexec 0 0
  228. EOF
  229.  
  230. echo "Chroot environment setup complete at /mnt/chroot"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement