mklive.sh.in 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #!/bin/bash
  2. #
  3. # vim: set ts=4 sw=4 et:
  4. #
  5. #-
  6. # Copyright (c) 2009-2013 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 PROGNAME=$(basename $0)
  32. info_msg() {
  33. printf "\033[1m$@\n\033[m"
  34. }
  35. error_out() {
  36. info_msg "There was an error in line $1 ... cleaning up $BUILDDIR, exiting."
  37. [ -d "$BUILDDIR" ] && rm -rf "$BUILDDIR"
  38. exit 1
  39. }
  40. write_etc_motd() {
  41. cat >> "$ROOTFS/etc/motd" <<_EOF
  42. ###############################################################################
  43. Autogenerated by void-mklive "@@MKLIVE_VERSION@@"
  44. ###############################################################################
  45. Welcome to the Void Linux Live system, you have been autologged in.
  46. This user has full sudo(8) permissions without any password, be careful
  47. executing commands through sudo(8).
  48. To start the installation please type:
  49. $ sudo void-installer
  50. and follow the on-screen instructions. Thanks for using Void Linux.
  51. http://www.voidlinux.eu
  52. ###############################################################################
  53. _EOF
  54. }
  55. write_conf_file() {
  56. cat > "$1" <<_EOF
  57. # *-*- sh -*-*
  58. # Default configuration file for vmklive-@VERSION@.
  59. #
  60. # List of packages to be installed into the live image.
  61. # NOTE: base-system and dialog packages are always added by default.
  62. #
  63. #PACKAGE_LIST=""
  64. # Default Void Linux splash image for grub/syslinux.
  65. SPLASH_IMAGE=/usr/share/void-artwork/splash.png
  66. # Default keymap to use.
  67. KEYMAP=us
  68. # Default locale to use.
  69. LOCALE=en_US.UTF-8
  70. # Path to XBPS utilities.
  71. #XBPS_INSTALL_CMD=xbps-install
  72. #XBPS_QUERY_CMD=xbps-query
  73. #XBPS_RECONFIGURE_CMD=xbps-reconfigure
  74. #XBPS_REMOVE_CMD=xbps-remove
  75. #XBPS_RINDEX_CMD=xbps-rindex
  76. #XBPS_UHELPER_CMD=xbps-uhelper
  77. # XBPS cache directory to install packages from.
  78. #REPOSITORY_CACHE=/blah/foo
  79. _EOF
  80. chmod 644 "$1"
  81. }
  82. usage() {
  83. cat <<_EOF
  84. Usage: $(basename $0) [options]
  85. Options:
  86. -C file Path to configuration file (defaults to ~/.mklive.conf)
  87. -c (gzip|bzip2|xz) Compression type for the initramfs/squashfs image.
  88. -r rootdir Use this directory to generate the image (if unset, defaults to cwd).
  89. -o outfile Output file name for the ISO image.
  90. -s splash Splash image file for isolinux.
  91. -V Show version.
  92. _EOF
  93. exit 1
  94. }
  95. copy_void_keys() {
  96. if [ ! -d "$1"/var/db/xbps/keys ]; then
  97. mkdir -p "$1"/var/db/xbps/keys
  98. cp /var/db/xbps/keys/*.plist "$1"/var/db/xbps/keys
  99. fi
  100. }
  101. install_packages() {
  102. for f in ${PACKAGE_LIST}; do
  103. info_msg " $f"
  104. done
  105. # Check that all pkgs are reachable.
  106. ${XBPS_INSTALL_CMD} ${XBPS_ARGS} -n ${PACKAGE_LIST} >>$LOGFILE 2>&1
  107. if [ $? -ne 0 ]; then
  108. info_msg "Missing required binary packages, exiting..."
  109. error_out
  110. fi
  111. ${XBPS_INSTALL_CMD} ${XBPS_ARGS} ${PACKAGE_LIST} >>$LOGFILE 2>&1
  112. ${XBPS_INSTALL_CMD} ${XBPS_ARGS} -u >>$LOGFILE 2>&1
  113. ${XBPS_REMOVE_CMD} ${XBPS_ARGS} -o >>$LOGFILE 2>&1
  114. ${XBPS_QUERY_CMD} -r "$ROOTFS" -l | sort > "${OUTPUT_FILE%.iso}"-package-list.txt
  115. # Enable choosen UTF-8 locale and generate it into the target rootfs.
  116. sed -e "s/\#\(${LOCALE}.*\)/\1/g" -i $ROOTFS/etc/default/libc-locales
  117. systemd-nspawn -D $ROOTFS xbps-reconfigure -fa >>$LOGFILE 2>&1
  118. # Enable some services if found.
  119. if [ -f $ROOTFS/usr/lib/systemd/system/NetworkManager.service ]; then
  120. systemd-nspawn -D $ROOTFS systemctl enable NetworkManager.service >>$LOGFILE 2>&1
  121. fi
  122. if [ -x installer.sh ]; then
  123. install -Dm755 installer.sh $ROOTFS/usr/sbin/void-installer
  124. else
  125. install -Dm755 /usr/sbin/void-installer $ROOTFS/usr/sbin/void-installer
  126. fi
  127. }
  128. generate_initramfs() {
  129. copy_void_keys $ROOTFS/kernel_temp
  130. # Install required pkgs in a temporary rootdir to create
  131. # the initramfs and to copy required files.
  132. $XBPS_INSTALL_CMD -r $ROOTFS/kernel_temp -Sy \
  133. base-system void-mklive -c $REPOSITORY_CACHE >>$LOGFILE 2>&1
  134. systemd-nspawn -D $ROOTFS/kernel_temp /usr/bin/dracut --${COMPRESSTYPE} \
  135. --force-add "vmklive" "/boot/initrd.lz" $KERNELVERSION >>$LOGFILE 2>&1
  136. mv $ROOTFS/kernel_temp/boot/initrd.lz $BOOT_DIR
  137. cp $ROOTFS/kernel_temp/boot/vmlinuz-$KERNELVERSION $BOOT_DIR/vmlinuz
  138. rm -rf $ROOTFS/kernel_temp
  139. }
  140. generate_isolinux_boot() {
  141. cp -f $SYSLINUX_DATADIR/isolinux.bin "$ISOLINUX_DIR"
  142. cp -f $SYSLINUX_DATADIR/ldlinux.c32 "$ISOLINUX_DIR"
  143. cp -f $SYSLINUX_DATADIR/libcom32.c32 "$ISOLINUX_DIR"
  144. cp -f $SYSLINUX_DATADIR/vesamenu.c32 "$ISOLINUX_DIR"
  145. cp -f $SYSLINUX_DATADIR/libutil.c32 "$ISOLINUX_DIR"
  146. cp -f $SYSLINUX_DATADIR/chain.c32 "$ISOLINUX_DIR"
  147. cp -f $MKLIVE_DATADIR/isolinux.cfg.in \
  148. "$ISOLINUX_DIR"/isolinux.cfg
  149. if [ -f "$SPLASH_IMAGE" ]; then
  150. cp -f $SPLASH_IMAGE "$ISOLINUX_DIR"
  151. fi
  152. sed -i -e "s|@@SPLASHIMAGE@@|$(basename $SPLASH_IMAGE)|" \
  153. -e "s|@@KERNVER@@|${KERNELVERSION}|" \
  154. -e "s|@@KEYMAP@@|${KEYMAP}|" \
  155. -e "s|@@ARCH@@|$(uname -m)|" \
  156. -e "s|@@LOCALE@@|${LOCALE}|" $ISOLINUX_DIR/isolinux.cfg
  157. }
  158. generate_grub_efi_boot() {
  159. cp -f $MKLIVE_DATADIR/grub.cfg $GRUB_DIR
  160. cp -f $MKLIVE_DATADIR/grub_void.cfg.in $GRUB_DIR/grub_void.cfg
  161. sed -i -e "s|@@SPLASHIMAGE@@|$(basename $SPLASH_IMAGE)|" \
  162. -e "s|@@KERNVER@@|${KERNELVERSION}|" \
  163. -e "s|@@KEYMAP@@|${KEYMAP}|" \
  164. -e "s|@@ARCH@@|$(uname -m)|" \
  165. -e "s|@@LOCALE@@|${LOCALE}|" $GRUB_DIR/grub_void.cfg
  166. mkdir -p $GRUB_DIR/fonts
  167. cp -f $GRUB_DATADIR/unicode.pf2 $GRUB_DIR/fonts
  168. # Create EFI vfat image.
  169. dd if=/dev/zero of=$GRUB_DIR/efiboot.img bs=1024 count=4096 >>$LOGFILE 2>&1
  170. mkfs.vfat -F12 -S 512 -n "grub_uefi" "$GRUB_DIR/efiboot.img" >>$LOGFILE 2>&1
  171. GRUB_EFI_TMPDIR="$(mktemp --tmpdir=$HOME -d)"
  172. LOOP_DEVICE="$(losetup --show --find ${GRUB_DIR}/efiboot.img)"
  173. mount -o rw,flush -t vfat "${LOOP_DEVICE}" "${GRUB_EFI_TMPDIR}" >>$LOGFILE 2>&1
  174. mkdir -p "${GRUB_EFI_TMPDIR}/EFI/boot/"
  175. cd "$BUILDDIR"
  176. grub-mkstandalone --directory="/usr/lib/grub/x86_64-efi" \
  177. --format="x86_64-efi" \
  178. --compression="xz" --output="${GRUB_EFI_TMPDIR}/EFI/boot/bootx64.efi" \
  179. "boot/grub/grub.cfg" >>$LOGFILE 2>&1
  180. umount "$GRUB_EFI_TMPDIR"
  181. losetup --detach "${LOOP_DEVICE}"
  182. rm -rf $GRUB_EFI_TMPDIR
  183. }
  184. generate_squashfs() {
  185. # Find out required size for the rootfs and create an ext3fs image off it.
  186. ROOTFS_SIZE=$(du -sk "$ROOTFS"|awk '{print $1}')
  187. mkdir -p "$BUILDDIR/tmp/LiveOS"
  188. dd if=/dev/zero of="$BUILDDIR/tmp/LiveOS/ext3fs.img" \
  189. bs="$((${ROOTFS_SIZE}+($ROOTFS_SIZE/6)))K" count=1 >>$LOGFILE 2>&1
  190. mkdir -p "$BUILDDIR/tmp-rootfs"
  191. mkfs.ext3 -F -m1 "$BUILDDIR/tmp/LiveOS/ext3fs.img" >>$LOGFILE 2>&1
  192. mount -o loop "$BUILDDIR/tmp/LiveOS/ext3fs.img" "$BUILDDIR/tmp-rootfs"
  193. cd $BUILDDIR
  194. cp -a rootfs/* tmp-rootfs/
  195. umount -f "$BUILDDIR/tmp-rootfs"
  196. mkdir -p "$BUILDDIR/LiveOS"
  197. mksquashfs "$BUILDDIR/tmp" "$BUILDDIR/LiveOS/squashfs.img" \
  198. -comp ${COMPRESSTYPE} >>$LOGFILE 2>&1
  199. chmod 444 "$BUILDDIR/LiveOS/squashfs.img"
  200. # Remove rootfs and temporary dirs, we don't need them anymore.
  201. rm -rf "$ROOTFS" "$BUILDDIR/tmp-rootfs" "$BUILDDIR/tmp"
  202. }
  203. generate_iso_image() {
  204. xorriso -as mkisofs \
  205. -iso-level 3 -rock -joliet \
  206. -max-iso9660-filenames -omit-period \
  207. -omit-version-number -relaxed-filenames -allow-lowercase \
  208. -volid "VOID_LIVE" \
  209. -eltorito-boot boot/isolinux/isolinux.bin \
  210. -eltorito-catalog boot/isolinux/boot.cat \
  211. -no-emul-boot -boot-load-size 4 -boot-info-table \
  212. -eltorito-alt-boot -e boot/grub/efiboot.img -isohybrid-gpt-basdat -no-emul-boot \
  213. -isohybrid-mbr $SYSLINUX_DATADIR/isohdpfx.bin \
  214. -output "$OUTPUT_FILE" "$BUILDDIR" >>$LOGFILE 2>&1
  215. }
  216. #
  217. # main()
  218. #
  219. while getopts "C:c:o:r:s:hV" opt; do
  220. case $opt in
  221. C) CONFIG_FILE="$OPTARG";;
  222. c) COMPRESSTYPE="$OPTARG";;
  223. o) OUTPUT_FILE="$OPTARG";;
  224. r) ROOTDIR="$OPTARG";;
  225. s) SPLASH_IMAGE="$OPTARG";;
  226. h) usage;;
  227. V) echo "$PROGNAME @@MKLIVE_VERSION@@"; exit 0;;
  228. esac
  229. done
  230. shift $(($OPTIND - 1))
  231. # Set defaults
  232. if [ -z "$CONFIG_FILE" ]; then
  233. CONFIG_FILE="$HOME/.mklive.conf"
  234. fi
  235. LOGFILE="$(mktemp -t vmklive-XXXXXXXXXX.log)"
  236. if [ -z "$SYSLINUX_DATADIR" ]; then
  237. SYSLINUX_DATADIR=/usr/share/syslinux
  238. fi
  239. if [ -z "$GRUB_DATADIR" ]; then
  240. GRUB_DATADIR=/usr/share/grub
  241. fi
  242. if [ -z "$MKLIVE_DATADIR" ]; then
  243. MKLIVE_DATADIR=/usr/share/void-mklive
  244. fi
  245. if [ -z "$SPLASH_IMAGE" ]; then
  246. SPLASH_IMAGE=/usr/share/void-artwork/splash.png
  247. fi
  248. if [ -z "$XBPS_INSTALL_CMD" ]; then
  249. XBPS_INSTALL_CMD=xbps-install
  250. fi
  251. if [ -z "$XBPS_REMOVE_CMD" ]; then
  252. XBPS_REMOVE_CMD=xbps-remove
  253. fi
  254. if [ -z "$XBPS_QUERY_CMD" ]; then
  255. XBPS_QUERY_CMD=xbps-query
  256. fi
  257. if [ -z "$XBPS_RINDEX_CMD" ]; then
  258. XBPS_RINDEX_CMD=xbps-rindex
  259. fi
  260. if [ -z "$XBPS_UHELPER_CMD" ]; then
  261. XBPS_UHELPER_CMD=xbps-uhelper
  262. fi
  263. if [ -z "$XBPS_RECONFIGURE_CMD" ]; then
  264. XBPS_RECONFIGURE_CMD=xbps-reconfigure
  265. fi
  266. if [ -z "$COMPRESSTYPE" ]; then
  267. COMPRESSTYPE=xz
  268. fi
  269. if [ -z "$REPOSITORYCACHE" ]; then
  270. REPOSITORY_CACHE="/var/cache/xbps"
  271. fi
  272. # Create or read configuration file.
  273. if [ ! -r $CONFIG_FILE ]; then
  274. info_msg "Creating config file at $CONFIG_FILE."
  275. write_conf_file $CONFIG_FILE
  276. fi
  277. . $CONFIG_FILE
  278. if [ -z "$PACKAGE_LIST" ]; then
  279. PACKAGE_LIST="base-system dialog"
  280. else
  281. PACKAGE_LIST="base-system dialog $PACKAGE_LIST"
  282. fi
  283. if [ ! -f $SYSLINUX_DATADIR/isolinux.bin ]; then
  284. echo "Missing required isolinux files in $SYSLINUX_DATADIR, exiting..."
  285. exit 1
  286. fi
  287. # Check for root permissions.
  288. if [ "$(id -u)" -ne 0 ]; then
  289. echo "Must be run as root, exiting..."
  290. exit 1
  291. fi
  292. ISO_VOLUME="VOID_LIVE"
  293. if [ -n "$ROOTDIR" ]; then
  294. BUILDDIR=$(mktemp --tmpdir="$ROOTDIR" -d)
  295. else
  296. BUILDDIR=$(mktemp --tmpdir="$(pwd -P)" -d)
  297. fi
  298. BUILDDIR=$(readlink -f $BUILDDIR)
  299. ROOTFS="$BUILDDIR/rootfs"
  300. BOOT_DIR="$BUILDDIR/boot"
  301. ISOLINUX_DIR="$BOOT_DIR/isolinux"
  302. GRUB_DIR="$BOOT_DIR/grub"
  303. ISOLINUX_CFG="$ISOLINUX_DIR/isolinux.cfg"
  304. mkdir -p $ISOLINUX_DIR $GRUB_DIR
  305. #
  306. # Check there are repos registered before anything.
  307. #
  308. ${XBPS_QUERY_CMD} -L >/dev/null 2>&1
  309. if [ $? -ne 0 ]; then
  310. echo "No repositories available, exiting..."
  311. error_out
  312. fi
  313. XBPS_ARGS="-r $ROOTFS -y"
  314. XBPS_ARGS="$XBPS_ARGS -c $REPOSITORY_CACHE"
  315. XBPS_VERSION=$($XBPS_QUERY_CMD -V|awk '{print $2}')
  316. case $XBPS_VERSION in
  317. *) ;;
  318. esac
  319. # Sync index for remote repos first.
  320. copy_void_keys $ROOTFS
  321. $XBPS_INSTALL_CMD -r $ROOTFS -S >/dev/null 2>&1
  322. _linux_series=$($XBPS_QUERY_CMD -r $ROOTFS -Rx linux)
  323. KERNELVERSION=$($XBPS_QUERY_CMD -r $ROOTFS -R --property pkgver ${_linux_series})
  324. KERNELVERSION=$($XBPS_UHELPER_CMD getpkgversion $KERNELVERSION)
  325. if [ -z "$OUTPUT_FILE" ]; then
  326. OUTPUT_FILE="$HOME/void-live-$(uname -m)-${KERNELVERSION}-$(date +%Y%m%d).iso"
  327. fi
  328. info_msg "Redirecting stdout/stderr to $LOGFILE ..."
  329. #
  330. # Install live system and specified packages.
  331. #
  332. info_msg "[1/7] Installing packages into the rootfs..."
  333. install_packages
  334. #
  335. # Prepare /etc/motd.
  336. #
  337. mkdir -p "$ROOTFS"/etc
  338. write_etc_motd
  339. #
  340. # Generate the initramfs.
  341. #
  342. info_msg "[2/7] Generating initramfs image ($COMPRESSTYPE)..."
  343. generate_initramfs
  344. #
  345. # Generate the isolinux boot.
  346. #
  347. info_msg "[3/7] Generating isolinux support for PC-BIOS systems..."
  348. generate_isolinux_boot
  349. #
  350. # Generate the GRUB EFI boot.
  351. #
  352. info_msg "[4/7] Generating GRUB support for EFI systems..."
  353. generate_grub_efi_boot
  354. #
  355. # Generate the squashfs image from rootfs.
  356. #
  357. info_msg "[5/7] Generating squashfs image ($COMPRESSTYPE) from rootfs..."
  358. generate_squashfs
  359. #
  360. # Generate the ISO image.
  361. #
  362. info_msg "[6/7] Generating ISO image..."
  363. generate_iso_image
  364. info_msg "[7/7] Removing build directory..."
  365. rm -rf "$BUILDDIR"
  366. hsize=$(du -sh "$OUTPUT_FILE"|awk '{print $1}')
  367. info_msg "Created $(readlink -f $OUTPUT_FILE) ($hsize) successfully."
  368. exit 0