mklive.sh.in 14 KB

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