diff --git a/templates/xbps-casper/files/COPYING b/templates/xbps-casper/files/COPYING new file mode 100644 index 00000000000..d511905c164 --- /dev/null +++ b/templates/xbps-casper/files/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/templates/xbps-casper/files/bin/casper-getty b/templates/xbps-casper/files/bin/casper-getty new file mode 100755 index 00000000000..bc9606c9f07 --- /dev/null +++ b/templates/xbps-casper/files/bin/casper-getty @@ -0,0 +1,15 @@ +#! /bin/sh + +autologin=1 +for opt in "$@" ; do + if [ "$opt" = "-l" -o "$opt" = "-n" ] ; then + autologin=0 + fi +done + +if [ "$autologin" = "1" ] ; then + exec /sbin/agetty -n -l /sbin/casper-login $* +else + exec /sbin/agetty $* +fi + diff --git a/templates/xbps-casper/files/bin/casper-login b/templates/xbps-casper/files/bin/casper-login new file mode 100755 index 00000000000..e6a41732cb9 --- /dev/null +++ b/templates/xbps-casper/files/bin/casper-login @@ -0,0 +1,6 @@ +#! /bin/sh + +USERNAME=root +[ -f /etc/casper.conf ] && . /etc/casper.conf +exec /bin/login -f $USERNAME + diff --git a/templates/xbps-casper/files/bin/casper-new-uuid b/templates/xbps-casper/files/bin/casper-new-uuid new file mode 100755 index 00000000000..39dc20c294a --- /dev/null +++ b/templates/xbps-casper/files/bin/casper-new-uuid @@ -0,0 +1,85 @@ +#!/bin/sh +# -*- coding: utf-8 -*- +# +# «casper-new-uuid» - Creates and injects new UUIDs for casper disks +# +# Create new UUIDs for disks to prevent conflicts and booting the wrong casper +# directory. Particularly useful in creating recovery disks that need to be +# able to also work with recovery partitioning schemes. +# +# Copyright (C) 2008, Dell Inc. +# +# Author: +# - Mario Limonciello +# +# This script is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation; either version 2 of the License, or at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this application; if not, write to the Free Software Foundation, Inc., 51 +# Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +################################################################################## + +set -e + +help() { + echo + echo "USAGE: $0 initrd.{l,g}z " + echo + echo "initrd.{l,g}z is the absolute path to the original gzipped or lzmaed initramfs" + echo " is the destination directory for the new compressed initramfs" + echo " is the destination directory for the new casper-uuid-TYPE " + echo + echo "if either path is absent, they will end up in the current directory " + echo "TYPE is determined by an already existing casper-uuid-* or by 'uname -s'" +} + +if [ "$#" = "0" ] || [ "x$1" = x-h ] || [ "x$1" = x--help ]; then + help + exit 3 +fi + +CWD=`pwd` +TEMPDIR=`mktemp -d /tmp/uuid-XXXXXX` +TYPE=`uname -r | cut -d '-' -f 3` + +if echo "$1" | grep ".lz$" >/dev/null; then + COMPRESSOR="lzma" + SUFFIX=".lz" +elif echo "$1" | grep ".gz$" >/dev/null; then + COMPRESSOR="gzip" + SUFFIX=".gz" +else + echo "Unsupported archive type." + exit 2 +fi +if [ -z "$2" ] || [ ! -d "$2" ] || [ "$2" = "." ]; then + COMPRESS_DIR="$CWD" +else + COMPRESS_DIR="$2" +fi + +if [ -z "$3" ] || [ ! -d "$3" ] || [ "$3" = "." ]; then + CASPERDIR="$CWD" +else + CASPERDIR="$3" +fi + +cd "$TEMPDIR" +$COMPRESSOR -cd "$1" -S "$SUFFIX" | cpio -id +uuidgen -r > conf/uuid.conf +find . | cpio --quiet --dereference -o -H newc | $COMPRESSOR -9c > "$COMPRESS_DIR/initrd$SUFFIX" +if [ "$(ls "$CASPERDIR/casper-uuid"-* >/dev/null 2>&1 | wc -l)" = 1 ]; then + cp conf/uuid.conf "$CASPERDIR/casper-uuid"-* +else + cp conf/uuid.conf "$CASPERDIR/casper-uuid-$TYPE" +fi +cd "$CWD" +rm -rf "$TEMPDIR" diff --git a/templates/xbps-casper/files/bin/casper-snapshot b/templates/xbps-casper/files/bin/casper-snapshot new file mode 100755 index 00000000000..1c611749a64 --- /dev/null +++ b/templates/xbps-casper/files/bin/casper-snapshot @@ -0,0 +1,309 @@ +#! /bin/bash + +# casper-snapshot - utility to manage Debian Live systems snapshots +# +# This program mount a device (fallback to /tmpfs under /mnt/snapshot +# and save the /cow (or a different dir) filesystem in it for reusing +# in another casper session. Look at manpage for more info. +# +# Copyright (C) 2006 Marco Amadori +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# +# On Debian systems, the complete text of the GNU General Public License +# can be found in /usr/share/common-licenses/GPL file. + +PROGRAM="`basename $0`" +VERSION=0.0.1 + + +# Source casper conf +if [ -e /etc/casper.conf ]; then + . /etc/casper.conf +else + USERNAME=$(cat /etc/passwd | grep "999" | cut -f1 -d ':') + HOSTNAME=$(hostname) + BUILD_SYSTEM="Debian" +fi + +export USERNAME USERFULLNAME HOSTNAME BUILD_SYSTEM + +# Source helper functions +helpers="/usr/share/initramfs-tools/scripts/casper-helpers" +if [ -e "${helpers}" ]; then + . "${helpers}" +else + echo "Error: I cannot found helper functions \"${helpers}\"." + exit 1 +fi + +# Define LSB log_* functions. +# Depend on lsb-base (>= 3.0-6) to ensure that this file is present. +. /lib/lsb/init-functions + +MOUNTP="" +COW="" +DEV="" +DEST="" +TYPE="" +DESKTOP_LINK="" + +Header () +{ + echo "${PROGRAM} - utility to do Debian Live snapshots" + echo + echo "Usage: ${PROGRAM} [-c|--cow DIRECTORY] [-d|--device DEVICE] [-o|--output FILE] [-t|--type TYPE]" + echo "Usage: ${PROGRAM} [-r|--resync-string STRING]" + echo "Usage: ${PROGRAM} [-h|--help]" + echo "Usage: ${PROGRAM} [-u|--usage]" + echo "Usage: ${PROGRAM} [-v|--version]" +} + +Usage () +{ + MESSAGE=${1} + Header + echo + echo "Try \"${PROGRAM} --help\" for more information." + if [ ! -z "${MESSAGE}" ]; then + echo -e "${MESSAGE}" + exit 1 + else + exit 0 + fi +} + +Help () +{ + Header + echo + echo "Options:" + echo " -c, --cow: specifies the copy on write directory (default: /cow)." + echo " -d, --device: specifies the output snapshot device (default: none)." + echo " -o, --output: specifies the output image file (default: $type dependent)." + echo " -r, --resync-string: internally used to resync previous made snapshots." + echo " -t, --type: specifies the snapshot type between \"squashfs\", \"ext2\" or \"cpio\".gz archive (default: cpio)" + echo -e "\nLook at casper-snapshot(1) man page for more information." + exit 0 +} + +Version () +{ + echo "${PROGRAM}, version ${VERSION}" + echo + echo "Copyright (C) 2006 Marco Amadori " + echo + echo "This program is free software; you can redistribute it and/or modify" + echo "it under the terms of the GNU General Public License as published by" + echo "the Free Software Foundation; either version 2 of the License, or" + echo "(at your option) any later version." + echo + echo "This program is distributed in the hope that it will be useful," + echo "but WITHOUT ANY WARRANTY; without even the implied warranty of" + echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" + echo "GNU General Public License for more details." + echo + echo "You should have received a copy of the GNU General Public License" + echo "along with this program; if not, write to the Free Software" + echo "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA" + echo + echo "On Debian systems, the complete text of the GNU General Public License" + echo "can be found in /usr/share/common-licenses/GPL file." + echo + echo "Homepage: " + exit 0 +} + +Do_snapshot () +{ + case "${TYPE}" in + squashfs) + echo "./tmp/exclude_list" > /tmp/exclude_list + ( cd "${COW}" && find . -name '*.wh.*' >> /tmp/exclude_list ) + mksquashfs "${COW}" "${DEST}" -ef /tmp/exclude_list || exit 1 + rm /tmp/exclude_list + ;; + cpio) + ( cd "${COW}" && find . -path '*.wh.*' -prune -o -print0 | cpio --quiet -o0 -H newc | gzip -9c > "${DEST}" ) || exit 1 + ;; + ext2) + DU_DIM="`du -ks ${COW} | cut -f1`" + REAL_DIM="`expr ${DU_DIM} + ${DU_DIM} / 20`" # Just 5% more to be sure, need something more sophistcated here... + genext2fs --size-in-blocks=${REAL_DIM} --reserved-percentage=0 --root="${COW}" "${DEST}" || exit 1 + ;; + *) + echo "Internal error." + exit 1 + ;; + esac +} + +Is_same_mount () +{ + dir1="`Base_path $1`" + dir2="`Base_path $2`" + if [ "${dir1}" == "${dir2}" ]; then + return 0 + else + return 1 + fi +} + +Parse_args () +{ + # Parse command line + ARGUMENTS="`getopt --longoptions cow:,device:,output,resync-string:,type:,help,usage,version --name=${PROGRAM} --options c:d:o:t:r:,h,u,v --shell sh -- "$@"`" + + if [ "$?" != "0" ]; then + echo "Terminating." >&2 + exit 1 + fi + + eval set -- "${ARGUMENTS}" + + while true; do + case "$1" in + -c|--cow) + SNAP_COW="$2"; shift 2 ;; + -d|--device) + SNAP_DEV="$2"; shift 2 ;; + -o|--output) + SNAP_OUTPUT="$2"; shift 2 ;; + -t|--type) + SNAP_TYPE="$2"; shift 2 ;; + -r|--resync-string) + SNAP_RSTRING="$2"; break ;; + -h|--help) + Help; shift ;; + -u|--usage) + Usage ; shift ;; + -v|--version) + Version; shift ;; + --) + shift; break ;; + *) + echo "Internal error."; exit 1 ;; + esac + done +} + +Mount_device () +{ + dev="$1" + + if [ ! -d "${MOUNTP}" ]; then + mkdir -p "${MOUNTP}" + fi + + if [ -z "${dev}" ]; then + # create a temp + mount -t tmpfs -o rw tmpfs "${MOUNTP}" + if [ ! -L /home/$USERNAME/Desktop/casper-snapshot ]; then + ln -s "${MOUNTP}" /home/$USERNAME/Desktop/casper-snapshot + fi + else + if [ -b "${dev}" ] ; then + try_mount "${dev}" "${MOUNTP}" rw + fi + fi +} + +Defaults () +{ + MOUNTP="/mnt/casper-snapshot" + COW="/cow" + DEV="" + TYPE="cpio" + DESKTOP_LINK=/home/$USERNAME/Desktop/casper-snapshot + + if [ -n "${SNAP_RSTRING}" ]; then + COW=$(echo "${SNAP_RSTRING}" | cut -f1 -d ':') + DEV=$(echo "${SNAP_RSTRING}" | cut -f2 -d ':') + DEST="${MOUNTP}/$(echo "${SNAP_RSTRING}" | cut -f3 -d ':')" + + case "${DEST}" in + *.cpio.gz) + TYPE="cpio" ;; + *.squashfs) + TYPE="squashfs" ;; + "") + TYPE="ext2" ;; + *.ext2) + TYPE="ext2" ;; + *) + Usage "Unrecognized String" ;; + esac + else + DEF_COW="/cow" + # Bad options handling + if [ -z "${SNAP_COW}" ]; then + COW="${DEF_COW}" + else + COW="${SNAP_COW}" + fi + + case "${SNAP_TYPE}" in + "cpio"|"squashfs"|"ext2") + TYPE="${SNAP_TYPE}" + ;; + "") + TYPE="cpio" ;; + *) + Usage "Error: unrecognized snapshot type" + ;; + esac + case "${TYPE}" in + cpio) + DEST="${MOUNTP}/casper-sn.cpio.gz" ;; + squashfs) + DEST="${MOUNTP}/casper-sn.squashfs" ;; + ext2) + DEST="${MOUNTP}/casper-sn.ext2" ;; + *) + echo "Internal error." + exit 1 + ;; + esac + #if [ -d + #if Is_same_mount + fi + + # check vars + if [ ! -d "${COW}" ]; then + Usage "Error: ${COW} is not a directory" + fi + + Mount_device $DEV + +} + +Clean () +{ + if [ -n "$DEV" ]; then + umount "${MOUNTP}" + rmdir "${MOUNTP}" + rm + fi +} + +Main () +{ + Parse_args "$@" + Defaults + Do_snapshot + Clean +} + +Main "$@" diff --git a/templates/xbps-casper/files/casper.conf b/templates/xbps-casper/files/casper.conf new file mode 100644 index 00000000000..55a6a7eb808 --- /dev/null +++ b/templates/xbps-casper/files/casper.conf @@ -0,0 +1,8 @@ +# This file should go in /etc/casper.conf +# Supported variables are: +# USERNAME, USERFULLNAME, HOST, BUILD_SYSTEM + +export USERNAME="anon" +export USERFULLNAME="Anonymous live session user" +export HOST="xbps-live" +export BUILD_SYSTEM="XBPS" diff --git a/templates/xbps-casper/files/conf.d/compcache b/templates/xbps-casper/files/conf.d/compcache new file mode 100644 index 00000000000..c93c77626ca --- /dev/null +++ b/templates/xbps-casper/files/conf.d/compcache @@ -0,0 +1 @@ +COMPCACHE_SIZE="25%" diff --git a/templates/xbps-casper/files/hooks/casper b/templates/xbps-casper/files/hooks/casper new file mode 100755 index 00000000000..14ae7db9924 --- /dev/null +++ b/templates/xbps-casper/files/hooks/casper @@ -0,0 +1,82 @@ +#!/bin/sh -e +# initramfs hook for casper + +PREREQS="" + +# Output pre-requisites +prereqs() +{ + echo "$PREREQS" +} + +case "$1" in + prereqs) + prereqs + exit 0 + ;; +esac + +. /usr/share/initramfs-tools/hook-functions + +manual_add_modules unionfs +manual_add_modules aufs + +# unionfs-fuse, if available +if [ -x /usr/bin/unionfs-fuse ]; then + copy_exec /usr/bin/unionfs-fuse /bin +fi + +# We need losetup +copy_exec /sbin/losetup /sbin + +# Casper hooks +mkdir -p ${DESTDIR}/lib/udev +copy_exec /lib/udev/cdrom_id /lib/udev +copy_exec /lib/udev/path_id /lib/udev + +# cifs boot +if [ -x /sbin/mount.cifs ]; then + copy_exec /sbin/mount.cifs /sbin + for x in cifs; do + manual_add_modules ${x} + done +fi + +# squashfs +manual_add_modules squashfs +manual_add_modules loop + +# random file system modules +manual_add_modules vfat +manual_add_modules ext3 +manual_add_modules ext4 +# needed for vfat. :-/ +manual_add_modules nls_cp437 +manual_add_modules nls_utf8 +manual_add_modules nls_iso8859-1 + +# cdrom modules +manual_add_modules sr_mod +manual_add_modules ide-cd +manual_add_modules sbp2 +manual_add_modules ohci1394 + +# Casper helpers. +for helper in getty login new-uuid snapshot; do + ln -s /sbin/casper-${helper} ${DESTDIR}/bin +done +cp /usr/share/initramfs-tools/scripts/casper-functions $DESTDIR/scripts +cp /usr/share/initramfs-tools/scripts/casper-helpers $DESTDIR/scripts + +auto_add_modules net + +if [ -e /etc/casper.conf ]; then + mkdir -p ${DESTDIR}/etc + cp /etc/casper.conf ${DESTDIR}/etc +fi + +if [ "$CASPER_GENERATE_UUID" ]; then + # Unique ID + mkdir -p $DESTDIR/conf + uuidgen -r > $DESTDIR/conf/uuid.conf +fi diff --git a/templates/xbps-casper/files/scripts/casper b/templates/xbps-casper/files/scripts/casper new file mode 100644 index 00000000000..4611eb57e45 --- /dev/null +++ b/templates/xbps-casper/files/scripts/casper @@ -0,0 +1,655 @@ +#!/bin/sh + +# set -e + +export PATH=/usr/bin:/usr/sbin:/bin:/sbin + +mountpoint=/cdrom +LIVE_MEDIA_PATH=casper + +root_persistence="casper-rw" +home_persistence="home-rw" +root_snapshot_label="casper-sn" +home_snapshot_label="home-sn" + +USERNAME=casper +USERFULLNAME="Live session user" +HOST=live +BUILD_SYSTEM=Custom + +mkdir -p $mountpoint + +[ -f /etc/casper.conf ] && . /etc/casper.conf +export USERNAME USERFULLNAME HOST BUILD_SYSTEM + +. /scripts/casper-helpers + +if [ ! -f /casper.vars ]; then + touch /casper.vars +fi + +parse_cmdline() { + for x in $(cat /proc/cmdline); do + case $x in + showmounts|show-cow) + export SHOWMOUNTS='Yes' ;; + persistent) + export PERSISTENT="Yes" ;; + nopersistent) + export PERSISTENT="" ;; + union=*) + export UNIONFS="${x#union=}";; + ip*) + STATICIP=${x#ip=} + if [ "${STATICIP}" = "" ]; then + STATICIP="frommedia" + fi + export STATICIP ;; + ignore_uuid) + IGNORE_UUID="Yes" ;; + live-media-path=*) + LIVE_MEDIA_PATH="${x#live-media-path=}" + export LIVE_MEDIA_PATH + echo "export LIVE_MEDIA_PATH=\"$LIVE_MEDIA_PATH\"" >> /etc/casper.conf ;; + esac + done + if [ "${UNIONFS}" = "" ]; then + export UNIONFS="aufs" + fi +} + +is_casper_path() { + path=$1 + if [ -d "$path/$LIVE_MEDIA_PATH" ]; then + if [ "$(echo $path/$LIVE_MEDIA_PATH/*.squashfs)" != "$path/$LIVE_MEDIA_PATH/*.squashfs" ] || + [ "$(echo $path/$LIVE_MEDIA_PATH/*.ext2)" != "$path/$LIVE_MEDIA_PATH/*.ext2" ] || + [ "$(echo $path/$LIVE_MEDIA_PATH/*.dir)" != "$path/$LIVE_MEDIA_PATH/*.dir" ]; then + return 0 + fi + fi + return 1 +} + +matches_uuid() { + if [ "$IGNORE_UUID" ] || [ ! -e /conf/uuid.conf ]; then + return 0 + fi + path="$1" + uuid="$(cat /conf/uuid.conf)" + for try_uuid_file in "$path/.disk/casper-uuid"*; do + [ -e "$try_uuid_file" ] || continue + try_uuid="$(cat "$try_uuid_file")" + if [ "$uuid" = "$try_uuid" ]; then + return 0 + fi + done + return 1 +} + +get_backing_device() { + case "$1" in + *.squashfs|*.ext2) + echo $(setup_loop "$1" "loop" "/sys/block/loop*") + ;; + *.dir) + echo "directory" + ;; + *) + panic "Unrecognized casper filesystem: $1" + ;; + esac +} + +match_files_in_dir() { + # Does any files match pattern $1 ? + + local pattern="$1" + if [ "$(echo $pattern)" != "$pattern" ]; then + return 0 + fi + return 1 +} + +mount_images_in_directory() { + directory="$1" + rootmnt="$2" + if match_files_in_dir "$directory/$LIVE_MEDIA_PATH/*.squashfs" || + match_files_in_dir "$directory/$LIVE_MEDIA_PATH/*.ext2" || + match_files_in_dir "$directory/$LIVE_MEDIA_PATH/*.dir"; then + setup_unionfs "$directory/$LIVE_MEDIA_PATH" "$rootmnt" + else + : + fi +} + +is_nice_device() { + sysfs_path="${1#/sys}" + if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-(ide|scsi|usb)|platform-mmc|platform-mxsdhci)"; then + return 0 + fi + return 1 +} + +copy_live_to() { + copyfrom="${1}" + copytodev="${2}" + copyto="${copyfrom}_swap" + + size=$(fs_size "" ${copyfrom} "used") + + if [ "${copytodev}" = "ram" ]; then + # copying to ram: + freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) ) + mount_options="-o size=${size}k" + free_string="memory" + fstype="tmpfs" + dev="/dev/shm" + else + # it should be a writable block device + if [ -b "${copytodev}" ]; then + dev="${copytodev}" + free_string="space" + fstype=$(get_fstype "${dev}") + freespace=$(fs_size "${dev}") + else + [ "$quiet" != "y" ] && log_warning_msg "${copytodev} is not a block device." + return 1 + fi + fi + if [ "${freespace}" -lt "${size}" ] ; then + [ "$quiet" != "y" ] && log_warning_msg "Not enough free ${free_string} (${freespace}k > ${size}k) to copy live media in ${copytodev}." + return 1 + fi + + # begin copying.. + mkdir "${copyto}" + echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}" + mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}" + cp -a ${copyfrom}/* ${copyto} # "cp -a" from busybox also copies hidden files + umount ${copyfrom} + mount -r -o move ${copyto} ${copyfrom} + rmdir ${copyto} + return 0 +} + +do_netmount() { + rc=1 + + modprobe "${MP_QUIET}" af_packet # For DHCP + + /sbin/udevadm trigger + /sbin/udevadm settle + + ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf | tee /netboot.config + + if [ "${NFSROOT}" = "auto" ]; then + NFSROOT=${ROOTSERVER}:${ROOTPATH} + fi + + [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}" + + if [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then + rc=0 + elif do_nfsmount ; then + NETBOOT="nfs" + export NETBOOT + rc=0 + fi + + [ "$quiet" != "y" ] && log_end_msg + return ${rc} +} + +do_nfsmount() { + rc=1 + modprobe "${MP_QUIET}" nfs + if [ -z "${NFSOPTS}" ]; then + NFSOPTS="" + fi + + [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}" + # FIXME: This while loop is an ugly HACK round an nfs bug + i=0 + while [ "$i" -lt 60 ]; do + nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break + sleep 1 + i="$(($i + 1))" + done + return ${rc} +} + +do_cifsmount() { + rc=1 + if [ -x "/sbin/mount.cifs" ]; then + if [ -z "${NFSOPTS}" ]; then + CIFSOPTS="-ouser=root,password=" + else + CIFSOPTS="${NFSOPTS}" + fi + + [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}" + modprobe "${MP_QUIET}" cifs + + if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then + rc=0 + fi + fi + return ${rc} +} + +do_snap_copy () +{ + fromdev="${1}" + todir="${2}" + snap_type="${3}" + + size=$(fs_size "${fromdev}" "" "used") + + if [ -b "${fromdev}" ]; then + # look for free mem + if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]; then + todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' ) + freespace=$(df -k | grep -s ${todev} | awk '{print $4}') + else + freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - )) + fi + + tomount="/mnt/tmpsnap" + if [ ! -d "${tomount}" ] ; then + mkdir -p "${tomount}" + fi + + fstype=$(get_fstype "${fromdev}") + if [ -n "${fstype}" ]; then + # Copying stuff... + mount -t "${fstype}" -o ro,noatime "${fromdev}" "${tomount}" + cp -a "${tomount}"/* ${todir} + umount "${tomount}" + else + log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}" + fi + + rmdir "${tomount}" + if echo ${fromdev} | grep -qs loop; then + losetup -d "${fromdev}" + fi + return 0 + else + return 1 + [ "$quiet" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium" + fi +} + +try_snap () +{ + # Look for $snap_label.* in block devices and copy the contents to $snap_mount + # and remember the device and filename for resync on exit in casper.init + + snap_label="${1}" + snap_mount="${2}" + snap_type="${3}" + + snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2") + if [ ! -z "${snapdata}" ]; then + snapdev="$(echo ${snapdata} | cut -f1 -d ' ')" + snapback="$(echo ${snapdata} | cut -f2 -d ' ')" + snapfile="$(echo ${snapdata} | cut -f3 -d ' ')" + if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\)'; then + # squashfs or ext2 snapshot + dev=$(get_backing_device "${snapback}/${snapfile}") + if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"; then + log_warning_msg "Impossible to include the ${snapfile} Snapshot" + return 1 + fi + else + # cpio.gz snapshot + # Unfortunately klibc's cpio is incompatible with the rest of + # the world; everything else requires -u -d, while klibc doesn't + # implement them. Try to detect whether it's in use. + cpiopath="$(which cpio)" || true + if [ "$cpiopath" ] && grep -aq /lib/klibc "$cpiopath"; then + cpioargs= + else + cpioargs='-u -d' + fi + if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i $cpioargs 2>/dev/null) ; then + log_warning_msg "Impossible to include the ${snapfile} Snapshot" + return 1 + fi + fi + umount "${snapback}" + else + dev=$(find_cow_device "${snap_label}") + if [ -b ${dev} ]; then + if echo "${dev}" | grep -qs loop; then + # strange things happens, user confused? + snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' ) + snapfile=$(basename ${snaploop}) + snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ') + else + snapdev="${dev}" + fi + if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}" ; then + log_warning_msg "Impossible to include the ${snap_label} Snapshot" + return 1 + else + if [ -n "${snapfile}" ]; then + # it was a loop device, user confused + umount ${snapdev} + fi + fi + else + log_warning_msg "Impossible to include the ${snap_label} Snapshot" + return 1 + fi + fi + echo "export ${snap_type}SNAP="/cow${snap_mount#$rootmnt}":${snapdev}:${snapfile}" >> /etc/casper.conf # for resync on reboot/halt + return 0 +} + +setup_unionfs() { + image_directory="$1" + rootmnt="$2" + + case ${UNIONFS} in + aufs|unionfs) + modprobe "${MP_QUIET}" -b ${UNIONFS} || true + if ! cut -f2 /proc/filesystems | grep -q "^${UNIONFS}\$" && \ + [ -x /bin/unionfs-fuse ]; then + UNIONFS="unionfs-fuse" + fi + ;; + esac + + # run-init can't deal with images in a subdir, but we're going to + # move all of these away before it runs anyway. No, we're not, + # put them in / since move-mounting them into / breaks mono and + # some other apps. + + croot="/" + + # Let's just mount the read-only file systems first + rofsstring="" + rofslist="" + if [ "${NETBOOT}" = "nfs" ] ; then + roopt="nfsro" # work around a bug in nfs-unionfs locking + elif [ "${UNIONFS}" = "aufs" ]; then + roopt="rr" + elif [ "${UNIONFS}" = "unionfs-fuse" ]; then + roopt="RO" + else + roopt="ro" + fi + + mkdir -p "${croot}" + for image_type in "ext2" "squashfs" "dir" ; do + for image in "${image_directory}"/*."${image_type}"; do + imagename=$(basename "${image}") + if [ -d "${image}" ]; then + # it is a plain directory: do nothing + rofsstring="${image}=${roopt}:${rofsstring}" + rofslist="${image} ${rofslist}" + elif [ -f "${image}" ]; then + backdev=$(get_backing_device "$image") + fstype=$(get_fstype "${backdev}") + if [ "${fstype}" = "unknown" ]; then + panic "Unknown file system type on ${backdev} (${image})" + fi + mkdir -p "${croot}/${imagename}" + mount -t "${fstype}" -o ro,noatime "${backdev}" "${croot}/${imagename}" || panic "Can not mount $backdev ($image) on ${croot}/${imagename}" && rofsstring="${croot}/${imagename}=${roopt}:${rofsstring}" && rofslist="${croot}/${imagename} ${rofslist}" + fi + done + done + rofsstring=${rofsstring%:} + + mkdir -p /cow + cowdevice="tmpfs" + cow_fstype="tmpfs" + cow_mountopt="rw,noatime,mode=755" + + # Looking for "${root_persistence}" device or file + if [ -n "${PERSISTENT}" ]; then + cowprobe=$(find_cow_device "${root_persistence}") + if [ -b "${cowprobe}" ]; then + cowdevice=${cowprobe} + cow_fstype=$(get_fstype "${cowprobe}") + cow_mountopt="rw,noatime" + else + [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent medium" + fi + fi + + mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || panic "Can not mount $cowdevice on /cow" + + case ${UNIONFS} in + unionfs-fuse) + (ulimit -n 16384; unionfs-fuse -o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid /cow=RW:$rofsstring "$rootmnt" || panic "${UNIONFS} mount failed") + mkdir -p /dev/.initramfs/varrun + pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true + ;; + *) + mount -t ${UNIONFS} -o noatime,dirs=/cow=rw:$rofsstring ${UNIONFS} "$rootmnt" || panic "${UNIONFS} mount failed" + ;; + esac + + # Adding other custom mounts + if [ -n "${PERSISTENT}" ]; then + # directly mount /home + # FIXME: add a custom mounts configurable system + homecow=$(find_cow_device "${home_persistence}" ) + if [ -b "${homecow}" ]; then + mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home" + export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy() + else + [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium" + fi + # Look for other snapshots to copy in + try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT" + try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME" + fi + + if [ -n "${SHOWMOUNTS}" ]; then + for d in ${rofslist}; do + mkdir -p "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}" + case d in + *.dir) # do nothing # mount -o bind "${d}" "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}" + ;; + *) + if [ "${UNIONFS}" = unionfs-fuse ]; then + mount -o bind "${d}" "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}" + else + mount -o move "${d}" "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}" + fi + ;; + esac + done + # shows cow fs on /cow for use by casper-snapshot + mkdir -p "${rootmnt}/cow" + mount -o bind /cow "${rootmnt}/cow" + fi + + # XXX: hardcode the image name for now (xtraeme). + # move the first mount. + mkdir -p "${rootmnt}/rofs" + if [ "${UNIONFS}" = unionfs-fuse ]; then + mount -o bind /filesystem.squashfs "${rootmnt}/rofs" + else + mount -o move /filesystem.squashfs "${rootmnt}/rofs" + fi +} + +check_dev () +{ + sysdev="${1}" + devname="${2}" + skip_uuid_check="${3}" + if [ -z "${devname}" ]; then + devname=$(sys2dev "${sysdev}") + fi + + if [ -d "${devname}" ]; then + mount -o bind "${devname}" $mountpoint || continue + if is_casper_path $mountpoint; then + echo $mountpoint + return 0 + else + umount $mountpoint + fi + fi + + if [ -n "${LIVEMEDIA_OFFSET}" ]; then + loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVEMEDIA_OFFSET}") + devname="${loopdevname}" + fi + + fstype=$(get_fstype "${devname}") + if is_supported_fs ${fstype}; then + mount -t ${fstype} -o ro,noatime "${devname}" $mountpoint || continue + if is_casper_path $mountpoint && \ + ([ "$skip_uuid_check" ] || matches_uuid $mountpoint); then + echo $mountpoint + return 0 + else + umount $mountpoint + fi + fi + + if [ -n "${LIVEMEDIA_OFFSET}" ]; then + losetup -d "${loopdevname}" + fi + return 1 +} + +find_livefs() { + timeout="${1}" + # first look at the one specified in the command line + if [ ! -z "${LIVEMEDIA}" ]; then + if check_dev "null" "${LIVEMEDIA}" "skip_uuid_check"; then + return 0 + fi + fi + # don't start autodetection before timeout has expired + if [ -n "${LIVEMEDIA_TIMEOUT}" ]; then + if [ "${timeout}" -lt "${LIVEMEDIA_TIMEOUT}" ]; then + return 1 + fi + fi + # or do the scan of block devices + for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|fd)"); do + devname=$(sys2dev "${sysblock}") + fstype=$(get_fstype "${devname}") + if /lib/udev/cdrom_id ${devname} > /dev/null; then + if check_dev "null" "${devname}" ; then + return 0 + fi + elif is_nice_device "${sysblock}" ; then + for dev in $(subdevices "${sysblock}"); do + if check_dev "${dev}" ; then + return 0 + fi + done + elif [ "${fstype}" = "squashfs" -o \ + "${fstype}" = "ext4" -o \ + "${fstype}" = "ext3" -o \ + "${fstype}" = "ext2" ]; then + # This is an ugly hack situation, the block device has + # an image directly on it. It's hopefully + # casper, so take it and run with it. + ln -s "${devname}" "${devname}.${fstype}" + echo "${devname}.${fstype}" + return 0 + fi + done + return 1 +} + +set_usplash_timeout() { + if [ -x /sbin/usplash_write ]; then + /sbin/usplash_write "TIMEOUT 120" + fi +} + +mountroot() { + exec 6>&1 + exec 7>&2 + exec > casper.log + exec 2>&1 + tail -f casper.log >&7 & + tailpid="$!" + + parse_cmdline + + wait_for_udev 10 + + set_usplash_timeout + [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount" + run_scripts /scripts/casper-premount + [ "$quiet" != "y" ] && log_end_msg + + # Needed here too because some things (*cough* udev *cough*) + # changes the timeout + + set_usplash_timeout + + if [ ! -z "${NETBOOT}" ]; then + if do_netmount ; then + livefs_root="${mountpoint}" + else + panic "Unable to find a live file system on the network" + fi + else + # Scan local devices for the image + i=0 + while [ "$i" -lt 60 ]; do + livefs_root=$(find_livefs $i) + if [ "${livefs_root}" ]; then + break + fi + sleep 1 + i="$(($i + 1))" + done + fi + + if [ -z "${livefs_root}" ]; then + panic "Unable to find a medium containing a live file system" + fi + + if [ "${TORAM}" ]; then + live_dest="ram" + elif [ "${TODISK}" ]; then + live_dest="${TODISK}" + fi + if [ "${live_dest}" ]; then + log_begin_msg "Copying live_media to ${live_dest}" + copy_live_to "${livefs_root}" "${live_dest}" + log_end_msg + fi + + mount_images_in_directory "${livefs_root}" "${rootmnt}" + + log_end_msg + + # unionfs-fuse needs /dev to be bind-mounted for the duration of + # casper-bottom; udev's init script will take care of things after that + if [ "${UNIONFS}" = unionfs-fuse ]; then + mount -n -o bind /dev "${rootmnt}/dev" + fi + + maybe_break casper-bottom + [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom" + + run_scripts /scripts/casper-bottom + [ "$quiet" != "y" ] && log_end_msg + + if [ "${UNIONFS}" = unionfs-fuse ]; then + umount "${rootmnt}/dev" + fi + + exec 1>&6 6>&- + exec 2>&7 7>&- + kill "$tailpid" + cp casper.log "${rootmnt}/var/log/" + if [ -f /etc/casper.conf ]; then + cp /etc/casper.conf "${rootmnt}/etc/" + fi +} diff --git a/templates/xbps-casper/files/scripts/casper-bottom/05mountpoints b/templates/xbps-casper/files/scripts/casper-bottom/05mountpoints new file mode 100755 index 00000000000..7c61de33ff6 --- /dev/null +++ b/templates/xbps-casper/files/scripts/casper-bottom/05mountpoints @@ -0,0 +1,27 @@ +#!/bin/sh + +PREREQ="" +DESCRIPTION="Moving mount points..." + +. /scripts/casper-functions + +prereqs() +{ + echo "$PREREQ" +} + +case $1 in +# get pre-requisites +prereqs) + prereqs + exit 0 + ;; +esac + +log_begin_msg "$DESCRIPTION" + +# Move to the new root filesystem so that programs there can get at it. +mkdir -p /root/cdrom +mount -n -o move /cdrom /root/cdrom + +log_end_msg diff --git a/templates/xbps-casper/files/scripts/casper-bottom/10adduser b/templates/xbps-casper/files/scripts/casper-bottom/10adduser new file mode 100755 index 00000000000..1bd8658e636 --- /dev/null +++ b/templates/xbps-casper/files/scripts/casper-bottom/10adduser @@ -0,0 +1,30 @@ +#!/bin/sh + +PREREQ="" +DESCRIPTION="Adding live session user..." + +. /scripts/casper-functions + +prereqs() +{ + echo "$PREREQ" +} + +case $1 in +# get pre-requisites +prereqs) + prereqs + exit 0 + ;; +esac + +log_begin_msg "$DESCRIPTION" + +# U6aMy0wojraho is just a blank password +chroot /root useradd -c "$USERFULLNAME" -m $USERNAME -p U6aMy0wojraho + +if [ -f /root/etc/sudoers ]; then + echo "${USERNAME} ALL=(ALL) NOPASSWD: ALL" >> /root/etc/sudoers +fi + +log_end_msg diff --git a/templates/xbps-casper/files/scripts/casper-bottom/12fstab b/templates/xbps-casper/files/scripts/casper-bottom/12fstab new file mode 100755 index 00000000000..10414d7dcdc --- /dev/null +++ b/templates/xbps-casper/files/scripts/casper-bottom/12fstab @@ -0,0 +1,29 @@ +#!/bin/sh + +PREREQ="" +DESCRIPTION="Configuring fstab..." +FSTAB=/root/etc/fstab + +. /scripts/casper-functions + +prereqs() +{ + echo "$PREREQ" +} + +case $1 in +# get pre-requisites +prereqs) + prereqs + exit 0 + ;; +esac + +log_begin_msg "$DESCRIPTION" + +cat > $FSTAB </dev/null | /bin/dd bs=10 count=1 2>/dev/null) || continue + + if [ "$magic" = "SWAPSPACE2" -o "$magic" = "SWAP-SPACE" ]; then +# log "Found $device" + devices="$devices $device" + fi +done + +for device in $devices; do + cat >> $FSTAB < /root/etc/conf.d/locale +fi + +log_end_msg diff --git a/templates/xbps-casper/files/scripts/casper-bottom/15autologin b/templates/xbps-casper/files/scripts/casper-bottom/15autologin new file mode 100755 index 00000000000..bc2d94ec11d --- /dev/null +++ b/templates/xbps-casper/files/scripts/casper-bottom/15autologin @@ -0,0 +1,23 @@ +#!/bin/sh + +PREREQ="" +DESCRIPTION="Setting up automatic login..." + +. /scripts/casper-functions + +prereqs() +{ + echo "$PREREQ" +} + +case $1 in +# get pre-requisites +prereqs) + prereqs + exit 0 + ;; +esac + +log_begin_msg "$DESCRIPTION" + +log_end_msg diff --git a/templates/xbps-casper/files/scripts/casper-bottom/18hostname b/templates/xbps-casper/files/scripts/casper-bottom/18hostname new file mode 100755 index 00000000000..e97513569f2 --- /dev/null +++ b/templates/xbps-casper/files/scripts/casper-bottom/18hostname @@ -0,0 +1,38 @@ +#! /bin/sh + +PREREQ="" +DESCRIPTION="Setting hostname..." + +. /scripts/casper-functions + +prereqs() +{ + echo "$PREREQ" +} + +case $1 in +# get pre-requisites +prereqs) + prereqs + exit 0 + ;; +esac + +log_begin_msg "$DESCRIPTION" + +echo "hostname=$HOST" > /root/etc/conf.d/hostname +cat > /root/etc/hosts < not in initramfs + export PATH="${PATH}:/usr/lib/klibc/bin" +fi + +sys2dev() { + sysdev=${1#/sys} + echo "/dev/$(/sbin/udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})" +} + +subdevices() { + sysblock=$1 + r="" + for dev in "${sysblock}" "${sysblock}"/*; do + if [ -e "${dev}/dev" ]; then + r="${r} ${dev}" + fi + done + echo ${r} +} + +is_supported_fs () { + # FIXME: do something better like the scan of supported filesystems + fstype="${1}" + case ${fstype} in + vfat|iso9660|udf|ext2|ext3|ext4|ntfs) + return 0 + ;; + esac + return 1 +} + +get_fstype() { + local FSTYPE + local FSSIZE + eval $(fstype < $1) + if [ "$FSTYPE" != "unknown" ]; then + echo $FSTYPE + return 0 + fi + /sbin/blkid -s TYPE -o value $1 2>/dev/null +} + +where_is_mounted() { + device=$1 + if grep -q "^$device " /proc/mounts; then + mountpoint="$(grep "^$device " /proc/mounts | awk '{print $2; exit}')" + grep "^$device " /proc/mounts | read d mountpoint rest + echo $mountpoint + return 0 + fi + return 1 +} + +lastline() { + while read lines ; do + line=${lines} + done + echo "${line}" +} + +base_path () +{ + testpath="${1}" + mounts="$(awk '{print $2}' /proc/mounts)" + testpath="$(busybox realpath ${testpath})" + + while true ; do + if echo "${mounts}" | grep -qs "^${testpath}" ; then + set -- `echo "${mounts}" | grep "^${testpath}" | lastline` + echo ${1} + break + else + testpath=`dirname $testpath` + fi + done +} + +fs_size () +{ + # Returns used/free fs kbytes + 5% more + # You could pass a block device as $1 or the mount point as $2 + + dev="${1}" + mountp="${2}" + used="${3}" + + if [ -z "${mountp}" ]; then + mountp=$(where_is_mounted "${dev}") + if [ "$?" -gt 0 ]; then + mountp="/mnt/tmp_fs_size" + mkdir -p "${mountp}" + mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" + doumount=1 + fi + fi + + if [ "${used}" = "used" ]; then + size=$(du -ks ${mountp} | cut -f1) + size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure + else + # free space + size="$(df -k | grep -s ${mountp} | awk '{print $4}')" + fi + + if [ -n "${doumount}" ]; then + umount "${mountp}" + rmdir "${mountp}" + fi + echo "${size}" +} + +setup_loop() { + local fspath=$1 + local module=$2 + local pattern=$3 + local offset=$4 + + modprobe ${MP_QUIET} -b "$module" + /sbin/udevadm settle + + if [ "$module" = loop ]; then + if [ ! -e /dev/loop0 ]; then + # temporary workaround for kernel bug + for i in 0 1 2 3 4 5 6 7; do + mknod "/dev/loop$i" b 7 "$i" || true + done + fi + + dev="$(losetup -f)" + if [ "$dev" ]; then + if [ -n "$offset" ]; then + losetup -o "$offset" "$dev" "$fspath" + else + losetup "$dev" "$fspath" + fi + echo "$dev" + return 0 + else + panic "No loop devices available" + fi + else + for loopdev in $pattern; do + if [ "$(cat $loopdev/size)" -eq 0 ]; then + dev=$(sys2dev "${loopdev}") + if [ -n "$offset" ]; then + losetup -o "$offset" "$dev" "$fspath" + else + losetup "$dev" "$fspath" + fi + echo "$dev" + return 0 + fi + done + panic "No loop devices available" + fi +} + +try_mount () +{ + dev="${1}" + mountp="${2}" + opts="${3}" + + if where_is_mounted ${dev} > /dev/null; then + if [ "${opts}" != "ro" ]; then + mount -o remount,"${opts}" ${dev} $(where_is_mounted ${dev}) || panic "Remounting failed" + fi + mount -o bind $(where_is_mounted ${dev}) ${mountp} || panic "Cannot bind-mount" + else + mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}" + fi +} + +find_cow_device() { + pers_label="${1}" + cow_backing="/${pers_label}-backing" + for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop); do + for dev in $(subdevices "${sysblock}"); do + devname=$(sys2dev "${dev}") + if [ "$(/sbin/blkid -s LABEL -o value $devname 2>/dev/null)" = "${pers_label}" ]; then + echo "$devname" + return + # Do not add any filesystem types here that might be able to + # mount a journalled filesystem and replay the journal. Doing so + # will cause data loss when a live CD is booted on a system + # where filesystems are in use by hibernated operating systems. + elif [ "$(get_fstype ${devname})" = "vfat" ]; then + mkdir -p "${cow_backing}" + try_mount "${devname}" "${cow_backing}" "rw" + if [ -e "${cow_backing}/${pers_label}" ]; then + echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*") + return 0 + else + umount ${cow_backing} + fi + fi + done + done +} + +find_files() +# return the first of $filenames found on vfat and ext2 devices +# FIXME: merge with above function +{ + filenames="${1}" + snap_backing="/snap-backing" + for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop); do + for dev in $(subdevices "${sysblock}"); do + devname=$(sys2dev "${dev}") + devfstype="$(get_fstype ${devname})" + if [ "${devfstype}" = "vfat" ] || [ "${devfstype}" = "ext2" ] ; then # FIXME: all supported block devices should be scanned + mkdir -p "${snap_backing}" + try_mount "${devname}" "${snap_backing}" "ro" + for filename in ${filenames}; do + if [ -e "${snap_backing}/${filename}" ]; then + echo "${devname} ${snap_backing} ${filename}" + return 0 + fi + done + umount ${snap_backing} + fi + done + done +} + + diff --git a/templates/xbps-casper/template b/templates/xbps-casper/template new file mode 100644 index 00000000000..ff1791817cb --- /dev/null +++ b/templates/xbps-casper/template @@ -0,0 +1,37 @@ +# Template file for 'xbps-casper' +pkgname=xbps-casper +version=0.1 +build_style=custom-install +short_desc="Run a live preinstalled system from read-only media" +maintainer="Juan RP " +checksum=b31983b27630b3ef99f68bd8382851a7963542c944b625bdbfd2f2ff301a02d3 +long_desc=" + $pkgname is a modified version of Ubuntu's casper, that is able + to run a live pre-installed system from read-only media." + +noextract=yes +noarch=yes +conf_files="/etc/casper.conf" + +Add_dependency full initramfs-tools + +do_install() +{ + # /etc config files + install -d ${DESTDIR}/etc + install -m 644 ${FILESDIR}/casper.conf ${DESTDIR}/etc + + # Helpers + install -d ${DESTDIR}/sbin + install -m 755 ${FILESDIR}/bin/* ${DESTDIR}/sbin + + # initramfs-tools hooks/scripts + initramfsdir=${DESTDIR}/usr/share/initramfs-tools + install -d ${initramfsdir}/hooks ${initramfsdir}/scripts/casper-bottom \ + ${initramfsdir}/conf.d + install -m 755 ${FILESDIR}/hooks/* ${initramfsdir}/hooks + install -m 755 ${FILESDIR}/scripts/casper-bottom/* \ + ${initramfsdir}/scripts/casper-bottom + install -m 644 ${FILESDIR}/scripts/casper* ${initramfsdir}/scripts + install -m 644 ${FILESDIR}/conf.d/* ${initramfsdir}/conf.d +}