mklive.sh.in 14 KB

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