mklive.sh.in 12 KB

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