mklive.sh.in 15 KB

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