mkimage.sh.in 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #!/bin/sh
  2. #-
  3. # Copyright (c) 2013-2016 Juan Romero Pardines.
  4. # Copyright (c) 2017 Google
  5. # All rights reserved.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in the
  14. # documentation and/or other materials provided with the distribution.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. #-
  27. readonly PROGNAME=$(basename "$0")
  28. readonly ARCH=$(uname -m)
  29. trap 'printf "\nInterrupted! exiting...\n"; cleanup; exit 0' INT TERM HUP
  30. # This source pulls in all the functions from lib.sh. This set of
  31. # functions makes it much easier to work with chroots and abstracts
  32. # away all the problems with running binaries with QEMU.
  33. # shellcheck source=./lib.sh
  34. . ./lib.sh
  35. # This script has a special cleanup() function since it needs to
  36. # unmount the rootfs as mounted on a loop device. This function is
  37. # defined after sourcing the library functions to ensure it is the
  38. # last one defined.
  39. cleanup() {
  40. umount_pseudofs
  41. umount -f "${ROOTFS}/boot" 2>/dev/null
  42. umount -f "${ROOTFS}" 2>/dev/null
  43. if [ -e "$LOOPDEV" ]; then
  44. partx -d "$LOOPDEV" 2>/dev/null
  45. losetup -d "$LOOPDEV" 2>/dev/null
  46. fi
  47. [ -d "$ROOTFS" ] && rmdir "$ROOTFS"
  48. }
  49. # This script is designed to take in a complete platformfs and spit
  50. # out an image that is suitable for writing with dd. The image is
  51. # configurable in terms of the filesystem layout, but not in terms of
  52. # the installed system itself. Customization to the installed system
  53. # should be made during the mkplatformfs step.
  54. usage() {
  55. cat <<_EOF
  56. Usage: $PROGNAME [options] <rootfs-tarball>
  57. The <rootfs-tarball> argument expects a tarball generated by void-mkrootfs.
  58. The platform is guessed automatically by its name.
  59. Accepted sizes suffixes: KiB, MiB, GiB, TiB, EiB.
  60. OPTIONS
  61. -b <fstype> Set /boot filesystem type (defaults to FAT)
  62. -B <bsize> Set /boot filesystem size (defaults to 64MiB)
  63. -r <fstype> Set / filesystem type (defaults to EXT4)
  64. -s <totalsize> Set total image size (defaults to 2GB)
  65. -o <output> Set image filename (guessed automatically)
  66. -h Show this help
  67. -V Show version
  68. Resulting image will have 2 partitions, /boot and /.
  69. _EOF
  70. exit 0
  71. }
  72. # ########################################
  73. # SCRIPT EXECUTION STARTS HERE
  74. # ########################################
  75. while getopts "b:B:o:r:s:hV" opt; do
  76. case $opt in
  77. b) BOOT_FSTYPE="$OPTARG";;
  78. B) BOOT_FSSIZE="$OPTARG";;
  79. o) FILENAME="$OPTARG";;
  80. r) ROOT_FSTYPE="$OPTARG";;
  81. s) IMGSIZE="$OPTARG";;
  82. V) echo "$PROGNAME @@MKLIVE_VERSION@@"; exit 0;;
  83. h) usage;;
  84. esac
  85. done
  86. shift $((OPTIND - 1))
  87. ROOTFS_TARBALL="$1"
  88. if [ -z "$ROOTFS_TARBALL" ]; then
  89. usage
  90. elif [ ! -r "$ROOTFS_TARBALL" ]; then
  91. # In rare cases the tarball can wind up owned by the wrong user.
  92. # This leads to confusing failures if execution is allowed to
  93. # proceed.
  94. die "Cannot read rootfs tarball: $ROOTFS_TARBALL"
  95. fi
  96. # By default we build all platform images with a 64MiB boot partition
  97. # formated FAT16, and an approximately 1.9GiB root partition formated
  98. # ext4. More exotic combinations are of course possible, but this
  99. # combination works on all known platforms.
  100. : "${IMGSIZE:=2G}"
  101. : "${BOOT_FSTYPE:=vfat}"
  102. : "${BOOT_FSSIZE:=64MiB}"
  103. : "${ROOT_FSTYPE:=ext4}"
  104. # Verify that the required tooling is available
  105. readonly REQTOOLS="sfdisk partx losetup mount truncate mkfs.${BOOT_FSTYPE} mkfs.${ROOT_FSTYPE}"
  106. check_tools
  107. # Setup the platform variable. Here we want just the name and
  108. # optionally -musl if this is the musl variant.
  109. PLATFORM="${ROOTFS_TARBALL#void-}"
  110. PLATFORM="${PLATFORM%-ROOTFS*}"
  111. # This is an awful hack since the script isn't using privesc
  112. # mechanisms selectively. This is a TODO item.
  113. if [ "$(id -u)" -ne 0 ]; then
  114. die "need root perms to continue, exiting."
  115. fi
  116. # Set the default filename if none was provided above. The default
  117. # will include the platform the image is being built for and the date
  118. # on which it was built.
  119. if [ -z "$FILENAME" ]; then
  120. FILENAME="void-${PLATFORM}-$(date +%Y%m%d).img"
  121. fi
  122. # Be absolutely certain the platform is supported before continuing
  123. case "$PLATFORM" in
  124. bananapi|beaglebone|cubieboard2|cubietruck|odroid-c2|odroid-u2|rpi|rpi2|rpi3|usbarmory|GCP|*-musl);;
  125. *) die "The $PLATFORM is not supported, exiting..."
  126. esac
  127. # Create the base image. This was previously accomplished with dd,
  128. # but truncate is markedly faster.
  129. info_msg "Creating disk image ($IMGSIZE) ..."
  130. truncate -s "${IMGSIZE}" "$FILENAME" >/dev/null 2>&1
  131. # Grab a tmpdir for the rootfs. If this fails we need to halt now
  132. # because otherwise things will go very badly for the host system.
  133. ROOTFS=$(mktemp -d) || die "Could not create tmpdir for ROOTFS"
  134. info_msg "Creating disk image partitions/filesystems ..."
  135. if [ "$BOOT_FSTYPE" = "vfat" ]; then
  136. # The mkfs.vfat program tries to make some "intelligent" choices
  137. # about the type of filesystem it creates. Instead we set options
  138. # if the type is vfat to ensure that the same options will be used
  139. # every time.
  140. _args="-I -F16"
  141. fi
  142. case "$PLATFORM" in
  143. cubieboard2|cubietruck|ci20*|odroid-c2*)
  144. # These platforms use a single partition for the entire filesystem.
  145. sfdisk "${FILENAME}" <<_EOF
  146. label: dos
  147. 2048,,L
  148. _EOF
  149. LOOPDEV=$(losetup --show --find --partscan "$FILENAME")
  150. mkfs.${ROOT_FSTYPE} -O '^64bit,^extra_isize,^has_journal' "${LOOPDEV}p1" >/dev/null 2>&1
  151. mount "${LOOPDEV}p1" "$ROOTFS"
  152. ROOT_UUID=$(blkid -o value -s UUID "${LOOPDEV}p1")
  153. ;;
  154. *)
  155. # These platforms use a partition layout with a small boot
  156. # partition (64M by default) and the rest of the space as the
  157. # root filesystem. This is the generally preferred disk
  158. # layout for new platforms.
  159. sfdisk "${FILENAME}" <<_EOF
  160. label: dos
  161. 2048,${BOOT_FSSIZE},b,*
  162. ,+,L
  163. _EOF
  164. LOOPDEV=$(losetup --show --find --partscan "$FILENAME")
  165. # Normally we need to quote to prevent argument splitting, but
  166. # we explicitly want argument splitting here.
  167. # shellcheck disable=SC2086
  168. mkfs.${BOOT_FSTYPE} $_args "${LOOPDEV}p1" >/dev/null
  169. case "$ROOT_FSTYPE" in
  170. # Because the images produced by this script are generally
  171. # either on single board computers using flash memory or
  172. # in cloud environments that already provide disk
  173. # durability, we shut off the journal for ext filesystems.
  174. # For flash memory this greatly extends the life of the
  175. # memory and for cloud images this lowers the overhead by
  176. # a small amount.
  177. ext[34]) disable_journal="-O ^has_journal";;
  178. esac
  179. mkfs.${ROOT_FSTYPE} "$disable_journal" "${LOOPDEV}p2" >/dev/null 2>&1
  180. mount "${LOOPDEV}p2" "$ROOTFS"
  181. mkdir -p "${ROOTFS}/boot"
  182. mount "${LOOPDEV}p1" "${ROOTFS}/boot"
  183. BOOT_UUID=$(blkid -o value -s UUID "${LOOPDEV}p1")
  184. ROOT_UUID=$(blkid -o value -s UUID "${LOOPDEV}p2")
  185. ;;
  186. esac
  187. # This step unpacks the platformfs tarball made by mkplatformfs.sh.
  188. info_msg "Unpacking rootfs tarball ..."
  189. if [ "$PLATFORM" = "beaglebone" ]; then
  190. # The beaglebone requires some special extra handling. The MLO
  191. # program is a special first stage boot loader that brings up
  192. # enough of the processor to then load u-boot which loads the rest
  193. # of the system. The noauto option also prevents /boot from being
  194. # mounted during system startup.
  195. fstab_args=",noauto"
  196. tar xfp "$ROOTFS_TARBALL" -C "$ROOTFS" ./boot/MLO
  197. tar xfp "$ROOTFS_TARBALL" -C "$ROOTFS" ./boot/u-boot.img
  198. touch "$ROOTFS/boot/uEnv.txt"
  199. umount "$ROOTFS/boot"
  200. fi
  201. # In the general case, its enough to just unpack the ROOTFS_TARBALL
  202. # onto the ROOTFS. This will get a system that is ready to boot, save
  203. # for the bootloader which is handled later.
  204. tar xfp "$ROOTFS_TARBALL" --xattrs --xattrs-include='*' -C "$ROOTFS"
  205. # For f2fs the system should not attempt an fsck at boot. This
  206. # filesystem is in theory self healing and does not use the standard
  207. # mechanisms. All other filesystems should use fsck at boot.
  208. fspassno="1"
  209. if [ "$ROOT_FSTYPE" = "f2fs" ]; then
  210. fspassno="0"
  211. fi
  212. # Void images prefer uuids to nodes in /dev since these are not
  213. # dependent on the hardware layout. On a single board computer this
  214. # may not matter much but it makes the cloud images easier to manage.
  215. echo "UUID=$ROOT_UUID / $ROOT_FSTYPE defaults 0 ${fspassno}" >> "${ROOTFS}/etc/fstab"
  216. if [ -n "$BOOT_UUID" ]; then
  217. echo "UUID=$BOOT_UUID /boot $BOOT_FSTYPE defaults${fstab_args} 0 2" >> "${ROOTFS}/etc/fstab"
  218. fi
  219. # This section does final configuration on the images. In the case of
  220. # SBCs this writes the bootloader to the image or sets up other
  221. # required binaries to boot. In the case of images destined for a
  222. # Cloud, this sets up the services that the cloud will expect to be
  223. # running and a suitable bootloader. When adding a new platform,
  224. # please add a comment explaining what the steps you are adding do,
  225. # and where information about your specific platform's boot process
  226. # can be found.
  227. info_msg "Configuring image for platform $PLATFORM"
  228. case "$PLATFORM" in
  229. bananapi*|cubieboard2*|cubietruck*)
  230. dd if="${ROOTFS}/boot/u-boot-sunxi-with-spl.bin" of="${LOOPDEV}" bs=1024 seek=8 >/dev/null 2>&1
  231. ;;
  232. odroid-c2*)
  233. dd if="${ROOTFS}/boot/bl1.bin.hardkernel" of="${LOOPDEV}" bs=1 count=442 >/dev/null 2>&1
  234. dd if="${ROOTFS}/boot/bl1.bin.hardkernel" of="${LOOPDEV}" bs=512 skip=1 seek=1 >/dev/null 2>&1
  235. dd if="${ROOTFS}/boot/u-boot.bin" of="${LOOPDEV}" bs=512 seek=97 >/dev/null 2>&1
  236. ;;
  237. odroid-u2*)
  238. dd if="${ROOTFS}/boot/E4412_S.bl1.HardKernel.bin" of="${LOOPDEV}" seek=1 >/dev/null 2>&1
  239. dd if="${ROOTFS}/boot/bl2.signed.bin" of="${LOOPDEV}" seek=31 >/dev/null 2>&1
  240. dd if="${ROOTFS}/boot/u-boot.bin" of="${LOOPDEV}" seek=63 >/dev/null 2>&1
  241. dd if="${ROOTFS}/boot/E4412_S.tzsw.signed.bin" of="${LOOPDEV}" seek=2111 >/dev/null 2>&1
  242. ;;
  243. usbarmory*)
  244. dd if="${ROOTFS}/boot/u-boot.imx" of="${LOOPDEV}" bs=512 seek=2 conv=fsync >/dev/null 2>&1
  245. ;;
  246. ci20*)
  247. dd if="${ROOTFS}/boot/u-boot-spl.bin" of="${LOOPDEV}" obs=512 seek=1 >/dev/null 2>&1
  248. dd if="${ROOTFS}/boot/u-boot.img" of="${LOOPDEV}" obs=1K seek=14 >/dev/null 2>&1
  249. ;;
  250. GCP*)
  251. # Google Cloud Platform image configuration for Google Cloud
  252. # Engine. The steps below are built in reference to the
  253. # documentation on building custom images available here:
  254. # https://cloud.google.com/compute/docs/images/import-existing-image
  255. # The images produced by this script are ready to upload and boot.
  256. # Setup GRUB
  257. mount_pseudofs
  258. run_cmd_chroot "${ROOTFS}" "grub-install ${LOOPDEV}"
  259. sed -i "s:page_poison=1:page_poison=1 console=ttyS0,38400n8d:" "${ROOTFS}/etc/default/grub"
  260. run_cmd_chroot "${ROOTFS}" update-grub
  261. # Setup the GCP Guest services
  262. for _service in dhcpcd sshd agetty-console nanoklogd socklog-unix GCP-Guest-Initialization GCP-accounts GCP-clock-skew GCP-ip-forwarding ; do
  263. run_cmd_chroot "${ROOTFS}" "ln -sv /etc/sv/$_service /etc/runit/runsvdir/default/$_service"
  264. done
  265. # Turn off the agetty's since we can't use them anyway
  266. rm -v "${ROOTFS}/etc/runit/runsvdir/default/agetty-tty"*
  267. # Disable root login over ssh and lock account
  268. sed -i "s:PermitRootLogin yes:PermitRootLogin no:" "${ROOTFS}/etc/ssh/sshd_config"
  269. run_cmd_chroot "${ROOTFS}" "passwd -l root"
  270. # Set the Timezone
  271. run_cmd_chroot "${ROOTFS}" "ln -svf /usr/share/zoneinfo/UTC /etc/localtime"
  272. # Generate glibc-locales if necessary (this is a noop on musl)
  273. if [ "$PLATFORM" = GCP ] ; then
  274. run_cmd_chroot "${ROOTFS}" "xbps-reconfigure -f glibc-locales"
  275. fi
  276. # Remove SSH host keys (these will get rebuilt on first boot)
  277. rm -f "${ROOTFS}/etc/ssh/*key*"
  278. rm -f "${ROOTFS}/etc/ssh/moduli"
  279. # Force the hostname since this isn't read from DHCP
  280. echo void-GCE > "${ROOTFS}/etc/hostname"
  281. # Cleanup the chroot from anything that was setup for the
  282. # run_cmd_chroot commands
  283. cleanup_chroot
  284. ;;
  285. esac
  286. # Release all the mounts, deconfigure the loop device, and remove the
  287. # rootfs mountpoint. Since this was just a mountpoint it should be
  288. # empty. If it contains stuff we bail out here since something went
  289. # very wrong.
  290. umount -R "$ROOTFS"
  291. losetup -d "$LOOPDEV"
  292. rmdir "$ROOTFS" || die "$ROOTFS not empty!"
  293. # We've been working with this as root for a while now, so this makes
  294. # sure the permissions are sane.
  295. chmod 644 "$FILENAME"
  296. # The standard images are ready to go, but the cloud images require
  297. # some minimal additional post processing.
  298. case "$PLATFORM" in
  299. GCP*)
  300. # This filename is mandated by the Google Cloud Engine import
  301. # process, the archive name is not.
  302. mv void-GCP*.img disk.raw
  303. info_msg "Compressing disk.raw"
  304. tar Sczf "${FILENAME%.img}.tar.gz" disk.raw
  305. # Since this process just produces something that can be
  306. # uploaded, we remove the original disk image.
  307. rm disk.raw
  308. info_msg "Sucessfully created ${FILENAME%.img}.tar.gz image."
  309. ;;
  310. *)
  311. info_msg "Successfully created $FILENAME image."
  312. ;;
  313. esac