mklive.sh.in 14 KB

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