mklive.sh.in 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #!/bin/bash
  2. #
  3. # vim: set ts=4 sw=4 et:
  4. #
  5. #-
  6. # Copyright (c) 2009-2014 Juan Romero Pardines.
  7. # All rights reserved.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. # 1. Redistributions of source code must retain the above copyright
  13. # notice, this list of conditions and the following disclaimer.
  14. # 2. Redistributions in binary form must reproduce the above copyright
  15. # notice, this list of conditions and the following disclaimer in the
  16. # documentation and/or other materials provided with the distribution.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. #-
  29. set -E
  30. trap "echo; error_out $LINENO $?" INT TERM HUP ERR
  31. readonly REQUIRED_PKGS="base-files syslinux grub-x86_64-efi squashfs-tools xorriso"
  32. readonly PROGNAME=$(basename $0)
  33. info_msg() {
  34. printf "\033[1m$@\n\033[m"
  35. }
  36. error_out() {
  37. info_msg "There was an error in line $1 ... cleaning up $BUILDDIR, exiting."
  38. [ -d "$BUILDDIR" ] && rm -rf "$BUILDDIR"
  39. exit 1
  40. }
  41. usage() {
  42. cat <<_EOF
  43. Usage: $(basename $0) [options]
  44. Options:
  45. -a <xbps-arch> Set XBPS_ARCH (do not use it unless you know what it is)
  46. -b <system-pkg> Set an alternative base-system package (defaults to base-system).
  47. -r <repo-url> Use this XBPS repository (may be specified multiple times).
  48. -c <cachedir> Use this XBPS cache directory (/var/cache/xbps if unset).
  49. -k <keymap> Default keymap to use (us if unset)
  50. -l <locale> Default locale to use (en_US.UTF-8 if unset).
  51. -i <lz4|gzip|bzip2|xz> Compression type for the initramfs image (lz4 if unset).
  52. -s <gzip|bzip2|xz> Compression type for the squashfs image (xz if unset)
  53. -S <freesize> Allocate this free size (MB) for the rootfs.
  54. -o <file> Output file name for the ISO image (auto if unset).
  55. -p "pkg pkgN ..." Install additional packages into the ISO image.
  56. -C "cmdline args" Add additional kernel command line arguments.
  57. -T "title" Modify the bootloader title.
  58. The $(basename $0) script generates a live image of the Void Linux distribution.
  59. This ISO image can be written to a CD/DVD-ROM or any USB stick.
  60. _EOF
  61. exit 1
  62. }
  63. copy_void_keys() {
  64. mkdir -p "$1"/var/db/xbps/keys
  65. cp keys/*.plist "$1"/var/db/xbps/keys
  66. }
  67. copy_void_conf() {
  68. install -Dm644 data/void-vpkgs.conf "$1"/usr/share/xbps/virtualpkg.d/void.conf
  69. }
  70. install_prereqs() {
  71. copy_void_conf $VOIDHOSTDIR
  72. $XBPS_INSTALL_CMD -r $VOIDHOSTDIR $XBPS_REPOSITORY $XBPS_CACHEDIR -y ${REQUIRED_PKGS} >> $LOGFILE 2>&1
  73. if [ $? -ne 0 ]; then
  74. info_msg "Failed to install required software, exiting..."
  75. error_out
  76. fi
  77. }
  78. install_packages() {
  79. if [ -n "$BASE_ARCH" ]; then
  80. export XBPS_ARCH="$BASE_ARCH"
  81. fi
  82. copy_void_conf $ROOTFS
  83. # Check that all pkgs are reachable.
  84. ${XBPS_INSTALL_CMD} -r $ROOTFS $XBPS_REPOSITORY $XBPS_CACHEDIR -yn ${PACKAGE_LIST} >>$LOGFILE 2>&1
  85. if [ $? -ne 0 ]; then
  86. info_msg "Missing required binary packages, exiting..."
  87. error_out
  88. fi
  89. ${XBPS_INSTALL_CMD} -r $ROOTFS $XBPS_REPOSITORY $XBPS_CACHEDIR -y ${PACKAGE_LIST} >>$LOGFILE 2>&1
  90. ${XBPS_INSTALL_CMD} -r $ROOTFS $XBPS_REPOSITORY $XBPS_CACHEDIR -yu >>$LOGFILE 2>&1
  91. ${XBPS_REMOVE_CMD} -r $ROOTFS $XBPS_CACHEDIR -o >>$LOGFILE 2>&1
  92. # Enable choosen UTF-8 locale and generate it into the target rootfs.
  93. if [ -f $ROOTFS/etc/default/libc-locales ]; then
  94. sed -e "s/\#\(${LOCALE}.*\)/\1/g" -i $ROOTFS/etc/default/libc-locales
  95. xbps-uchroot $ROOTFS xbps-reconfigure -f glibc-locales >>$LOGFILE 2>&1
  96. fi
  97. # reconfigure util-linux just to be safe
  98. xbps-uchroot $ROOTFS xbps-reconfigure -f util-linux >>$LOGFILE 2>&1
  99. if [ -x installer.sh ]; then
  100. install -Dm755 installer.sh $ROOTFS/usr/sbin/void-installer
  101. else
  102. install -Dm755 /usr/sbin/void-installer $ROOTFS/usr/sbin/void-installer
  103. fi
  104. # Cleanup and remove useless stuff.
  105. rm -rf $ROOTFS/var/cache/* $ROOTFS/run/* $ROOTFS/var/run/*
  106. unset XBPS_ARCH
  107. }
  108. copy_dracut_files() {
  109. mkdir -p $1/usr/lib/dracut/modules.d/01vmklive
  110. cp dracut/*.sh $1/usr/lib/dracut/modules.d/01vmklive/
  111. }
  112. generate_initramfs() {
  113. # Install required pkgs in a temporary rootdir to create
  114. # the initramfs and to copy required files.
  115. copy_dracut_files $VOIDHOSTDIR
  116. copy_void_conf $VOIDHOSTDIR
  117. $XBPS_INSTALL_CMD -r $VOIDHOSTDIR $XBPS_REPOSITORY $XBPS_CACHEDIR -y base-system xz lz4 >>$LOGFILE 2>&1
  118. if [ "$BASE_SYSTEM_PKG" = "base-system-systemd" ]; then
  119. _args="--add systemd"
  120. else
  121. _args="--omit systemd"
  122. fi
  123. xbps-uchroot $VOIDHOSTDIR /usr/bin/dracut --${INITRAMFS_COMPRESSION} \
  124. --force-add "vmklive" ${_args} "/boot/initrd" $KERNELVERSION >>$LOGFILE 2>&1
  125. mv $VOIDHOSTDIR/boot/initrd $BOOT_DIR
  126. cp $VOIDHOSTDIR/boot/vmlinuz-$KERNELVERSION $BOOT_DIR/vmlinuz
  127. }
  128. generate_isolinux_boot() {
  129. cp -f $SYSLINUX_DATADIR/isolinux.bin "$ISOLINUX_DIR"
  130. cp -f $SYSLINUX_DATADIR/ldlinux.c32 "$ISOLINUX_DIR"
  131. cp -f $SYSLINUX_DATADIR/libcom32.c32 "$ISOLINUX_DIR"
  132. cp -f $SYSLINUX_DATADIR/vesamenu.c32 "$ISOLINUX_DIR"
  133. cp -f $SYSLINUX_DATADIR/libutil.c32 "$ISOLINUX_DIR"
  134. cp -f $SYSLINUX_DATADIR/chain.c32 "$ISOLINUX_DIR"
  135. cp -f isolinux/isolinux.cfg.in "$ISOLINUX_DIR"/isolinux.cfg
  136. cp -f ${SPLASH_IMAGE} "$ISOLINUX_DIR"
  137. sed -i -e "s|@@SPLASHIMAGE@@|$(basename ${SPLASH_IMAGE})|" \
  138. -e "s|@@KERNVER@@|${KERNELVERSION}|" \
  139. -e "s|@@KEYMAP@@|${KEYMAP}|" \
  140. -e "s|@@ARCH@@|$(uname -m)|" \
  141. -e "s|@@LOCALE@@|${LOCALE}|" \
  142. -e "s|@@BOOT_TITLE@@|${BOOT_TITLE}|" \
  143. -e "s|@@BOOT_CMDLINE@@|${BOOT_CMDLINE}|" \
  144. $ISOLINUX_DIR/isolinux.cfg
  145. }
  146. generate_grub_efi_boot() {
  147. cp -f grub/grub.cfg $GRUB_DIR
  148. cp -f grub/grub_void.cfg.in $GRUB_DIR/grub_void.cfg
  149. sed -i -e "s|@@SPLASHIMAGE@@|$(basename ${SPLASH_IMAGE})|" \
  150. -e "s|@@KERNVER@@|${KERNELVERSION}|" \
  151. -e "s|@@KEYMAP@@|${KEYMAP}|" \
  152. -e "s|@@ARCH@@|$(uname -m)|" \
  153. -e "s|@@BOOT_TITLE@@|${BOOT_TITLE}|" \
  154. -e "s|@@BOOT_CMDLINE@@|${BOOT_CMDLINE}|" \
  155. -e "s|@@LOCALE@@|${LOCALE}|" $GRUB_DIR/grub_void.cfg
  156. modprobe -q loop
  157. # Create EFI vfat image.
  158. dd if=/dev/zero of=$GRUB_DIR/efiboot.img bs=1024 count=4096 >>$LOGFILE 2>&1
  159. mkfs.vfat -F12 -S 512 -n "grub_uefi" "$GRUB_DIR/efiboot.img" >>$LOGFILE 2>&1
  160. GRUB_EFI_TMPDIR="$(mktemp --tmpdir=$HOME -d)"
  161. LOOP_DEVICE="$(losetup --show --find ${GRUB_DIR}/efiboot.img)"
  162. mount -o rw,flush -t vfat "${LOOP_DEVICE}" "${GRUB_EFI_TMPDIR}" >>$LOGFILE 2>&1
  163. cp -a $IMAGEDIR/boot $VOIDHOSTDIR
  164. xbps-uchroot $VOIDHOSTDIR grub-mkstandalone --directory="/usr/lib/grub/x86_64-efi" \
  165. --format="x86_64-efi" \
  166. --compression="xz" --output="/tmp/bootx64.efi" \
  167. "boot/grub/grub.cfg" >>$LOGFILE 2>&1
  168. mkdir -p ${GRUB_EFI_TMPDIR}/EFI/boot
  169. cp -f $VOIDHOSTDIR/tmp/bootx64.efi ${GRUB_EFI_TMPDIR}/EFI/boot/
  170. umount "$GRUB_EFI_TMPDIR"
  171. losetup --detach "${LOOP_DEVICE}"
  172. rm -rf $GRUB_EFI_TMPDIR
  173. }
  174. generate_squashfs() {
  175. # Find out required size for the rootfs and create an ext3fs image off it.
  176. ROOTFS_SIZE=$(du -sm "$ROOTFS"|awk '{print $1}')
  177. if [ -z "$ROOTFS_FREESIZE" ]; then
  178. ROOTFS_FREESIZE="$((ROOTFS_SIZE/6))"
  179. fi
  180. mkdir -p "$BUILDDIR/tmp/LiveOS"
  181. dd if=/dev/zero of="$BUILDDIR/tmp/LiveOS/ext3fs.img" \
  182. bs="$((ROOTFS_SIZE+ROOTFS_FREESIZE))M" count=1 >>$LOGFILE 2>&1
  183. mkdir -p "$BUILDDIR/tmp-rootfs"
  184. mkfs.ext3 -F -m1 "$BUILDDIR/tmp/LiveOS/ext3fs.img" >>$LOGFILE 2>&1
  185. mount -o loop "$BUILDDIR/tmp/LiveOS/ext3fs.img" "$BUILDDIR/tmp-rootfs"
  186. cp -a $ROOTFS/* $BUILDDIR/tmp-rootfs/
  187. umount -f "$BUILDDIR/tmp-rootfs"
  188. mkdir -p "$IMAGEDIR/LiveOS"
  189. mksquashfs "$BUILDDIR/tmp" "$IMAGEDIR/LiveOS/squashfs.img" \
  190. -comp ${SQUASHFS_COMPRESSION} >>$LOGFILE 2>&1
  191. chmod 444 "$IMAGEDIR/LiveOS/squashfs.img"
  192. # Remove rootfs and temporary dirs, we don't need them anymore.
  193. rm -rf "$ROOTFS" "$BUILDDIR/tmp-rootfs" "$BUILDDIR/tmp"
  194. }
  195. generate_iso_image() {
  196. xorriso -as mkisofs \
  197. -iso-level 3 -rock -joliet \
  198. -max-iso9660-filenames -omit-period \
  199. -omit-version-number -relaxed-filenames -allow-lowercase \
  200. -volid "VOID_LIVE" \
  201. -eltorito-boot boot/isolinux/isolinux.bin \
  202. -eltorito-catalog boot/isolinux/boot.cat \
  203. -no-emul-boot -boot-load-size 4 -boot-info-table \
  204. -eltorito-alt-boot -e boot/grub/efiboot.img -isohybrid-gpt-basdat -no-emul-boot \
  205. -isohybrid-mbr $SYSLINUX_DATADIR/isohdpfx.bin \
  206. -output "$CURDIR/$OUTPUT_FILE" "$IMAGEDIR" >>$LOGFILE 2>&1
  207. }
  208. #
  209. # main()
  210. #
  211. while getopts "a:b:r:c:C:T:k:l:i:s:S:o:p:h" opt; do
  212. case $opt in
  213. a) BASE_ARCH="$OPTARG";;
  214. b) BASE_SYSTEM_PKG="$OPTARG";;
  215. r) XBPS_REPOSITORY+="--repository=$OPTARG ";;
  216. c) XBPS_CACHEDIR="--cachedir=$OPTARG";;
  217. k) KEYMAP="$OPTARG";;
  218. l) LOCALE="$OPTARG";;
  219. i) INITRAMFS_COMPRESSION="$OPTARG";;
  220. s) SQUASHFS_COMPRESSION="$OPTARG";;
  221. S) ROOTFS_FREESIZE="$OPTARG";;
  222. o) OUTPUT_FILE="$OPTARG";;
  223. p) PACKAGE_LIST="$OPTARG";;
  224. C) BOOT_CMDLINE="$OPTARG";;
  225. T) BOOT_TITLE="$OPTARG";;
  226. h) usage;;
  227. esac
  228. done
  229. shift $(($OPTIND - 1))
  230. # Set defaults
  231. : ${XBPS_CACHEDIR:=--cachedir=/var/cache/xbps}
  232. : ${KEYMAP:=us}
  233. : ${LOCALE:=en_US.UTF-8}
  234. : ${INITRAMFS_COMPRESSION:=xz}
  235. : ${SQUASHFS_COMPRESSION:=xz}
  236. : ${BASE_SYSTEM_PKG:=base-system}
  237. : ${BOOT_TITLE:="Void Linux"}
  238. # Required packages in the image for a working system.
  239. PACKAGE_LIST="$BASE_SYSTEM_PKG $PACKAGE_LIST"
  240. LOGFILE="$(mktemp -t vmklive-XXXXXXXXXX.log)"
  241. # Check for root permissions.
  242. if [ "$(id -u)" -ne 0 ]; then
  243. echo "Must be run as root, exiting..."
  244. exit 1
  245. fi
  246. readonly CURDIR="$PWD"
  247. ISO_VOLUME="VOID_LIVE"
  248. if [ -n "$ROOTDIR" ]; then
  249. BUILDDIR=$(mktemp --tmpdir="$ROOTDIR" -d)
  250. else
  251. BUILDDIR=$(mktemp --tmpdir="$(pwd -P)" -d)
  252. fi
  253. BUILDDIR=$(readlink -f $BUILDDIR)
  254. IMAGEDIR="$BUILDDIR/image"
  255. ROOTFS="$IMAGEDIR/rootfs"
  256. VOIDHOSTDIR="$BUILDDIR/void-host"
  257. BOOT_DIR="$IMAGEDIR/boot"
  258. ISOLINUX_DIR="$BOOT_DIR/isolinux"
  259. GRUB_DIR="$BOOT_DIR/grub"
  260. ISOLINUX_CFG="$ISOLINUX_DIR/isolinux.cfg"
  261. : ${XBPS_REPOSITORY:=--repository=http://repo.voidlinux.eu/current}
  262. : ${SYSLINUX_DATADIR:=$VOIDHOSTDIR/usr/share/syslinux}
  263. : ${SPLASH_IMAGE:=data/splash.png}
  264. : ${XBPS_INSTALL_CMD:=xbps-install}
  265. : ${XBPS_REMOVE_CMD:=xbps-remove}
  266. : ${XBPS_QUERY_CMD:=xbps-query}
  267. : ${XBPS_RINDEX_CMD:=xbps-rindex}
  268. : ${XBPS_UHELPER_CMD:=xbps-uhelper}
  269. : ${XBPS_RECONFIGURE_CMD:=xbps-reconfigure}
  270. mkdir -p $ROOTFS $VOIDHOSTDIR $ISOLINUX_DIR $GRUB_DIR
  271. info_msg "Redirecting stdout/stderr to $LOGFILE ..."
  272. info_msg "[1/9] Synchronizing XBPS repository data..."
  273. # Sync index for remote repos first.
  274. copy_void_keys $ROOTFS
  275. $XBPS_INSTALL_CMD -r $ROOTFS ${XBPS_REPOSITORY} -S
  276. cp -a $ROOTFS/* $VOIDHOSTDIR
  277. _linux_series=$($XBPS_QUERY_CMD -r $ROOTFS ${XBPS_REPOSITORY:=-R} -x linux)
  278. KERNELVERSION=$($XBPS_QUERY_CMD -r $ROOTFS ${XBPS_REPOSITORY:=-R} -p pkgver ${_linux_series})
  279. KERNELVERSION=$($XBPS_UHELPER_CMD getpkgversion $KERNELVERSION)
  280. : ${OUTPUT_FILE="void-live-$(uname -m)-${KERNELVERSION}-$(date +%Y%m%d).iso"}
  281. #
  282. # Install required packages to generate the image.
  283. #
  284. info_msg "[2/9] Installing software to generate the image: ${REQUIRED_PKGS} ..."
  285. install_prereqs
  286. #
  287. # Install live system and specified packages.
  288. #
  289. mkdir -p "$ROOTFS"/etc
  290. [ -s data/motd ] && cp data/motd $ROOTFS/etc
  291. [ -s data/issue ] && cp data/issue $ROOTFS/etc
  292. info_msg "[3/9] Installing void pkgs into the rootfs: ${PACKAGE_LIST} ..."
  293. install_packages
  294. export PATH=$VOIDHOSTDIR/usr/bin:$VOIDHOSTDIR/usr/sbin:$PATH
  295. export LD_LIBRARY_PATH=$VOIDHOSTDIR/usr/lib
  296. #
  297. # Generate the initramfs.
  298. #
  299. info_msg "[4/9] Generating initramfs image ($INITRAMFS_COMPRESSION)..."
  300. generate_initramfs
  301. #
  302. # Generate the isolinux boot.
  303. #
  304. info_msg "[5/9] Generating isolinux support for PC-BIOS systems..."
  305. generate_isolinux_boot
  306. #
  307. # Generate the GRUB EFI boot.
  308. #
  309. info_msg "[6/9] Generating GRUB support for EFI systems..."
  310. generate_grub_efi_boot
  311. #
  312. # Generate the squashfs image from rootfs.
  313. #
  314. info_msg "[7/9] Generating squashfs image ($SQUASHFS_COMPRESSION) from rootfs..."
  315. generate_squashfs
  316. #
  317. # Generate the ISO image.
  318. #
  319. info_msg "[8/9] Generating ISO image..."
  320. generate_iso_image
  321. info_msg "[9/9] Removing build directory..."
  322. rm -rf "$BUILDDIR"
  323. hsize=$(du -sh "$CURDIR/$OUTPUT_FILE"|awk '{print $1}')
  324. info_msg "Created $(readlink -f $CURDIR/$OUTPUT_FILE) ($hsize) successfully."
  325. exit 0