init-install-efi-testfs.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 200 Mb for the boot partition
  10. boot_size=200
  11. # 50% for the second rootfs
  12. testfs_ratio=50
  13. found="no"
  14. echo "Searching for a hard drive..."
  15. for device in 'hda' 'hdb' 'sda' 'sdb' 'mmcblk0' 'mmcblk1'
  16. do
  17. if [ -e /sys/block/${device}/removable ]; then
  18. if [ "$(cat /sys/block/${device}/removable)" = "0" ]; then
  19. found="yes"
  20. while true; do
  21. # Try sleeping here to avoid getting kernel messages
  22. # obscuring/confusing user
  23. sleep 5
  24. echo "Found drive at /dev/${device}. Do you want to install this image there ? [y/n]"
  25. read answer
  26. if [ "$answer" = "y" ] ; then
  27. break
  28. fi
  29. if [ "$answer" = "n" ] ; then
  30. found=no
  31. break
  32. fi
  33. echo "Please answer y or n"
  34. done
  35. fi
  36. fi
  37. if [ "$found" = "yes" ]; then
  38. break;
  39. fi
  40. done
  41. if [ "$found" = "no" ]; then
  42. exit 1
  43. fi
  44. echo "Installing image on /dev/${device}"
  45. #
  46. # The udev automounter can cause pain here, kill it
  47. #
  48. rm -f /etc/udev/rules.d/automount.rules
  49. rm -f /etc/udev/scripts/mount*
  50. #
  51. # Unmount anything the automounter had mounted
  52. #
  53. umount /dev/${device}* 2> /dev/null || /bin/true
  54. mkdir -p /tmp
  55. cat /proc/mounts > /etc/mtab
  56. disk_size=$(parted /dev/${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//")
  57. testfs_size=$((disk_size*testfs_ratio/100))
  58. rootfs_size=$((disk_size-boot_size-testfs_size))
  59. rootfs_start=$((boot_size))
  60. rootfs_end=$((rootfs_start+rootfs_size))
  61. testfs_start=$((rootfs_end))
  62. # MMC devices are special in a couple of ways
  63. # 1) they use a partition prefix character 'p'
  64. # 2) they are detected asynchronously (need rootwait)
  65. rootwait=""
  66. part_prefix=""
  67. if [ ! "${device#mmcblk}" = "${device}" ]; then
  68. part_prefix="p"
  69. rootwait="rootwait"
  70. fi
  71. bootfs=/dev/${device}${part_prefix}1
  72. rootfs=/dev/${device}${part_prefix}2
  73. testfs=/dev/${device}${part_prefix}3
  74. echo "*****************"
  75. echo "Boot partition size: $boot_size MB ($bootfs)"
  76. echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
  77. echo "Testfs partition size: $testfs_size MB ($testfs)"
  78. echo "*****************"
  79. echo "Deleting partition table on /dev/${device} ..."
  80. dd if=/dev/zero of=/dev/${device} bs=512 count=2
  81. echo "Creating new partition table on /dev/${device} ..."
  82. parted /dev/${device} mklabel gpt
  83. echo "Creating boot partition on $bootfs"
  84. parted /dev/${device} mkpart primary 0% $boot_size
  85. parted /dev/${device} set 1 boot on
  86. echo "Creating rootfs partition on $rootfs"
  87. parted /dev/${device} mkpart primary $rootfs_start $rootfs_end
  88. echo "Creating testfs partition on $testfs"
  89. parted /dev/${device} mkpart primary $testfs_start 100%
  90. parted /dev/${device} print
  91. echo "Formatting $bootfs to vfat..."
  92. mkfs.vfat -n "boot" $bootfs
  93. echo "Formatting $rootfs to ext3..."
  94. mkfs.ext3 -L "platform" $rootfs
  95. echo "Formatting $testfs to ext3..."
  96. mkfs.ext3 -L "testrootfs" $testfs
  97. mkdir /ssd
  98. mkdir /rootmnt
  99. mkdir /bootmnt
  100. mount $rootfs /ssd
  101. mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /rootmnt
  102. echo "Copying rootfs files..."
  103. cp -a /rootmnt/* /ssd
  104. touch /ssd/etc/masterimage
  105. if [ -d /ssd/etc/ ] ; then
  106. # We dont want udev to mount our root device while we're booting...
  107. if [ -d /ssd/etc/udev/ ] ; then
  108. echo "/dev/${device}" >> /ssd/etc/udev/mount.blacklist
  109. fi
  110. fi
  111. umount /ssd
  112. umount /rootmnt
  113. echo "Preparing boot partition..."
  114. mount $bootfs /ssd
  115. EFIDIR="/ssd/EFI/BOOT"
  116. mkdir -p $EFIDIR
  117. cp /run/media/$1/vmlinuz /ssd
  118. # Copy the efi loader
  119. cp /run/media/$1/EFI/BOOT/*.efi $EFIDIR
  120. if [ -f /run/media/$1/EFI/BOOT/grub.cfg ]; then
  121. GRUBCFG="$EFIDIR/grub.cfg"
  122. cp /run/media/$1/EFI/BOOT/grub.cfg $GRUBCFG
  123. # Update grub config for the installed image
  124. # Delete the install entry
  125. sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG
  126. # Delete the initrd lines
  127. sed -i "/initrd /d" $GRUBCFG
  128. # Delete any LABEL= strings
  129. sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG
  130. # Delete any root= strings
  131. sed -i "s/ root=[^ ]*/ /" $GRUBCFG
  132. # Add the root= and other standard boot options
  133. sed -i "s@linux /vmlinuz *@linux /vmlinuz root=$rootfs rw $rootwait quiet @" $GRUBCFG
  134. fi
  135. if [ -d /run/media/$1/loader ]; then
  136. SYSTEMDBOOT_CFGS="/ssd/loader/entries/*.conf"
  137. # copy config files for systemd-boot
  138. cp -dr /run/media/$1/loader /ssd
  139. # delete the install entry
  140. rm -f /ssd/loader/entries/install.conf
  141. # delete the initrd lines
  142. sed -i "/initrd /d" $SYSTEMDBOOT_CFGS
  143. # delete any LABEL= strings
  144. sed -i "s/ LABEL=[^ ]*/ /" $SYSTEMDBOOT_CFGS
  145. # delete any root= strings
  146. sed -i "s/ root=[^ ]*/ /" $SYSTEMDBOOT_CFGS
  147. # add the root= and other standard boot options
  148. sed -i "s@options *@options root=$rootfs rw $rootwait quiet @" $SYSTEMDBOOT_CFGS
  149. # Add the test label
  150. echo -ne "title test\nlinux /test-kernel\noptions root=$testfs rw $rootwait quiet\n" > /ssd/loader/entries/test.conf
  151. fi
  152. umount /ssd
  153. sync
  154. echo "Remove your installation media, and press ENTER"
  155. read enter
  156. echo "Rebooting..."
  157. reboot -f