init-install-efi.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #!/bin/sh -e
  2. #
  3. # Copyright (c) 2012, Intel Corporation.
  4. # All rights reserved.
  5. #
  6. # install.sh [device_name] [rootfs_name]
  7. #
  8. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  9. # We need 20 Mb for the boot partition
  10. boot_size=20
  11. # 5% for swap
  12. swap_ratio=5
  13. # Get a list of hard drives
  14. hdnamelist=""
  15. live_dev_name=`cat /proc/mounts | grep ${1%/} | awk '{print $1}'`
  16. live_dev_name=${live_dev_name#\/dev/}
  17. # Only strip the digit identifier if the device is not an mmc
  18. case $live_dev_name in
  19. mmcblk*)
  20. ;;
  21. nvme*)
  22. ;;
  23. *)
  24. live_dev_name=${live_dev_name%%[0-9]*}
  25. ;;
  26. esac
  27. echo "Searching for hard drives ..."
  28. # Some eMMC devices have special sub devices such as mmcblk0boot0 etc
  29. # we're currently only interested in the root device so pick them wisely
  30. devices=`ls /sys/block/ | grep -v mmcblk` || true
  31. mmc_devices=`ls /sys/block/ | grep "mmcblk[0-9]\{1,\}$"` || true
  32. devices="$devices $mmc_devices"
  33. for device in $devices; do
  34. case $device in
  35. loop*)
  36. # skip loop device
  37. ;;
  38. sr*)
  39. # skip CDROM device
  40. ;;
  41. ram*)
  42. # skip ram device
  43. ;;
  44. *)
  45. # skip the device LiveOS is on
  46. # Add valid hard drive name to the list
  47. case $device in
  48. $live_dev_name*)
  49. # skip the device we are running from
  50. ;;
  51. *)
  52. hdnamelist="$hdnamelist $device"
  53. ;;
  54. esac
  55. ;;
  56. esac
  57. done
  58. if [ -z "${hdnamelist}" ]; then
  59. echo "You need another device (besides the live device /dev/${live_dev_name}) to install the image. Installation aborted."
  60. exit 1
  61. fi
  62. TARGET_DEVICE_NAME=""
  63. for hdname in $hdnamelist; do
  64. # Display found hard drives and their basic info
  65. echo "-------------------------------"
  66. echo /dev/$hdname
  67. if [ -r /sys/block/$hdname/device/vendor ]; then
  68. echo -n "VENDOR="
  69. cat /sys/block/$hdname/device/vendor
  70. fi
  71. if [ -r /sys/block/$hdname/device/model ]; then
  72. echo -n "MODEL="
  73. cat /sys/block/$hdname/device/model
  74. fi
  75. if [ -r /sys/block/$hdname/device/uevent ]; then
  76. echo -n "UEVENT="
  77. cat /sys/block/$hdname/device/uevent
  78. fi
  79. echo
  80. done
  81. # Get user choice
  82. while true; do
  83. echo "Please select an install target or press n to exit ($hdnamelist ): "
  84. read answer
  85. if [ "$answer" = "n" ]; then
  86. echo "Installation manually aborted."
  87. exit 1
  88. fi
  89. for hdname in $hdnamelist; do
  90. if [ "$answer" = "$hdname" ]; then
  91. TARGET_DEVICE_NAME=$answer
  92. break
  93. fi
  94. done
  95. if [ -n "$TARGET_DEVICE_NAME" ]; then
  96. break
  97. fi
  98. done
  99. if [ -n "$TARGET_DEVICE_NAME" ]; then
  100. echo "Installing image on /dev/$TARGET_DEVICE_NAME ..."
  101. else
  102. echo "No hard drive selected. Installation aborted."
  103. exit 1
  104. fi
  105. device=/dev/$TARGET_DEVICE_NAME
  106. #
  107. # The udev automounter can cause pain here, kill it
  108. #
  109. rm -f /etc/udev/rules.d/automount.rules
  110. rm -f /etc/udev/scripts/mount*
  111. #
  112. # Unmount anything the automounter had mounted
  113. #
  114. umount ${device}* 2> /dev/null || /bin/true
  115. mkdir -p /tmp
  116. # Create /etc/mtab if not present
  117. if [ ! -e /etc/mtab ] && [ -e /proc/mounts ]; then
  118. ln -sf /proc/mounts /etc/mtab
  119. fi
  120. disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//")
  121. swap_size=$((disk_size*swap_ratio/100))
  122. rootfs_size=$((disk_size-boot_size-swap_size))
  123. rootfs_start=$((boot_size))
  124. rootfs_end=$((rootfs_start+rootfs_size))
  125. swap_start=$((rootfs_end))
  126. # MMC devices are special in a couple of ways
  127. # 1) they use a partition prefix character 'p'
  128. # 2) they are detected asynchronously (need rootwait)
  129. rootwait=""
  130. part_prefix=""
  131. if [ ! "${device#/dev/mmcblk}" = "${device}" ] || \
  132. [ ! "${device#/dev/nvme}" = "${device}" ]; then
  133. part_prefix="p"
  134. rootwait="rootwait"
  135. fi
  136. # USB devices also require rootwait
  137. if [ -n `readlink /dev/disk/by-id/usb* | grep $TARGET_DEVICE_NAME` ]; then
  138. rootwait="rootwait"
  139. fi
  140. bootfs=${device}${part_prefix}1
  141. rootfs=${device}${part_prefix}2
  142. swap=${device}${part_prefix}3
  143. echo "*****************"
  144. echo "Boot partition size: $boot_size MB ($bootfs)"
  145. echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
  146. echo "Swap partition size: $swap_size MB ($swap)"
  147. echo "*****************"
  148. echo "Deleting partition table on ${device} ..."
  149. dd if=/dev/zero of=${device} bs=512 count=35
  150. echo "Creating new partition table on ${device} ..."
  151. parted ${device} mklabel gpt
  152. echo "Creating boot partition on $bootfs"
  153. parted ${device} mkpart boot fat32 0% $boot_size
  154. parted ${device} set 1 boot on
  155. echo "Creating rootfs partition on $rootfs"
  156. parted ${device} mkpart root ext3 $rootfs_start $rootfs_end
  157. echo "Creating swap partition on $swap"
  158. parted ${device} mkpart swap linux-swap $swap_start 100%
  159. parted ${device} print
  160. echo "Formatting $bootfs to vfat..."
  161. mkfs.vfat $bootfs
  162. echo "Formatting $rootfs to ext3..."
  163. mkfs.ext3 $rootfs
  164. echo "Formatting swap partition...($swap)"
  165. mkswap $swap
  166. mkdir /tgt_root
  167. mkdir /src_root
  168. mkdir -p /boot
  169. # Handling of the target root partition
  170. mount $rootfs /tgt_root
  171. mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
  172. echo "Copying rootfs files..."
  173. cp -a /src_root/* /tgt_root
  174. if [ -d /tgt_root/etc/ ] ; then
  175. boot_uuid=$(blkid -o value -s UUID ${bootfs})
  176. swap_part_uuid=$(blkid -o value -s PARTUUID ${swap})
  177. echo "/dev/disk/by-partuuid/$swap_part_uuid swap swap defaults 0 0" >> /tgt_root/etc/fstab
  178. echo "UUID=$boot_uuid /boot vfat defaults 1 2" >> /tgt_root/etc/fstab
  179. # We dont want udev to mount our root device while we're booting...
  180. if [ -d /tgt_root/etc/udev/ ] ; then
  181. echo "${device}" >> /tgt_root/etc/udev/mount.blacklist
  182. fi
  183. fi
  184. umount /src_root
  185. # Handling of the target boot partition
  186. mount $bootfs /boot
  187. echo "Preparing boot partition..."
  188. EFIDIR="/boot/EFI/BOOT"
  189. mkdir -p $EFIDIR
  190. # Copy the efi loader
  191. cp /run/media/$1/EFI/BOOT/*.efi $EFIDIR
  192. if [ -f /run/media/$1/EFI/BOOT/grub.cfg ]; then
  193. root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs})
  194. GRUBCFG="$EFIDIR/grub.cfg"
  195. cp /run/media/$1/EFI/BOOT/grub.cfg $GRUBCFG
  196. # Update grub config for the installed image
  197. # Delete the install entry
  198. sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG
  199. # Delete the initrd lines
  200. sed -i "/initrd /d" $GRUBCFG
  201. # Delete any LABEL= strings
  202. sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG
  203. # Delete any root= strings
  204. sed -i "s/ root=[^ ]*/ /g" $GRUBCFG
  205. # Add the root= and other standard boot options
  206. sed -i "s@linux /vmlinuz *@linux /vmlinuz root=PARTUUID=$root_part_uuid rw $rootwait quiet @" $GRUBCFG
  207. fi
  208. if [ -d /run/media/$1/loader ]; then
  209. rootuuid=$(blkid -o value -s PARTUUID ${rootfs})
  210. SYSTEMDBOOT_CFGS="/boot/loader/entries/*.conf"
  211. # copy config files for systemd-boot
  212. cp -dr /run/media/$1/loader /boot
  213. # delete the install entry
  214. rm -f /boot/loader/entries/install.conf
  215. # delete the initrd lines
  216. sed -i "/initrd /d" $SYSTEMDBOOT_CFGS
  217. # delete any LABEL= strings
  218. sed -i "s/ LABEL=[^ ]*/ /" $SYSTEMDBOOT_CFGS
  219. # delete any root= strings
  220. sed -i "s/ root=[^ ]*/ /" $SYSTEMDBOOT_CFGS
  221. # add the root= and other standard boot options
  222. sed -i "s@options *@options root=PARTUUID=$rootuuid rw $rootwait quiet @" $SYSTEMDBOOT_CFGS
  223. fi
  224. umount /tgt_root
  225. cp /run/media/$1/vmlinuz /boot
  226. umount /boot
  227. sync
  228. echo "Remove your installation media, and press ENTER"
  229. read enter
  230. echo "Rebooting..."
  231. reboot -f