paul@154 | 1 | #!/bin/sh |
paul@154 | 2 | |
paul@244 | 3 | # Make a test filesystem. |
paul@244 | 4 | # |
paul@244 | 5 | # Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@244 | 6 | # |
paul@244 | 7 | # This program is free software; you can redistribute it and/or |
paul@244 | 8 | # modify it under the terms of the GNU General Public License as |
paul@244 | 9 | # published by the Free Software Foundation; either version 2 of |
paul@244 | 10 | # the License, or (at your option) any later version. |
paul@244 | 11 | # |
paul@244 | 12 | # This program is distributed in the hope that it will be useful, |
paul@244 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@244 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@244 | 15 | # GNU General Public License for more details. |
paul@244 | 16 | # |
paul@244 | 17 | # You should have received a copy of the GNU General Public License |
paul@244 | 18 | # along with this program; if not, write to the Free Software |
paul@244 | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@244 | 20 | # Boston, MA 02110-1301, USA |
paul@244 | 21 | |
paul@239 | 22 | PROGNAME=$(basename "$0") |
paul@154 | 23 | |
paul@244 | 24 | # Handle program options. |
paul@244 | 25 | |
paul@155 | 26 | if [ "$1" = '-q' ] ; then |
paul@155 | 27 | QUIET=$1 |
paul@155 | 28 | shift 1 |
paul@155 | 29 | else |
paul@155 | 30 | QUIET= |
paul@155 | 31 | fi |
paul@155 | 32 | |
paul@239 | 33 | PKGDIR=$(realpath "$1") |
paul@308 | 34 | PROGRAMS_DIR=$(realpath "$2") |
paul@308 | 35 | E2ACCESS_DIR=$(realpath "$3") |
paul@308 | 36 | TARGET=$(realpath "$4") |
paul@154 | 37 | |
paul@308 | 38 | if [ ! -e "$PKGDIR" ] || [ ! -e "$PROGRAMS_DIR" ] || [ ! -e "$E2ACCESS_DIR" ] || [ ! "$TARGET" ] ; then |
paul@239 | 39 | cat 1>&2 <<EOF |
paul@308 | 40 | Usage: $PROGNAME [ -q ] <package directory> <programs directory> <e2access directory> <target> |
paul@239 | 41 | |
paul@239 | 42 | Package directory: $PKGDIR |
paul@308 | 43 | Programs directory: $PROGRAMS_DIR |
paul@239 | 44 | e2access directory: $E2ACCESS_DIR |
paul@239 | 45 | Target filesystem: $TARGET |
paul@239 | 46 | EOF |
paul@154 | 47 | exit 1 |
paul@154 | 48 | fi |
paul@154 | 49 | |
paul@154 | 50 | # Initialise the program details and environment. |
paul@154 | 51 | |
paul@154 | 52 | E2ACCESS="$E2ACCESS_DIR"/e2access |
paul@154 | 53 | export LD_LIBRARY_PATH="$E2ACCESS_DIR" |
paul@154 | 54 | |
paul@241 | 55 | OPTIONS='-u 1000 -g 1000' |
paul@241 | 56 | |
paul@244 | 57 | # Define a convenience function for e2access invocation. |
paul@244 | 58 | |
paul@241 | 59 | e2access() |
paul@241 | 60 | { |
paul@244 | 61 | "$E2ACCESS" $OPTIONS "$TARGET" $* |
paul@241 | 62 | } |
paul@241 | 63 | |
paul@154 | 64 | # Make a filesystem for the example. |
paul@154 | 65 | |
paul@154 | 66 | mkdir tmp_e2test |
paul@154 | 67 | cd tmp_e2test |
paul@239 | 68 | |
paul@239 | 69 | # Set a sensible umask. |
paul@239 | 70 | |
paul@239 | 71 | umask 0022 |
paul@239 | 72 | |
paul@239 | 73 | mkdir -p home/paulb/public |
paul@239 | 74 | echo "Public!" > home/paulb/public/message |
paul@239 | 75 | |
paul@239 | 76 | # Set a restrictive umask. |
paul@239 | 77 | |
paul@239 | 78 | umask 0077 |
paul@239 | 79 | |
paul@239 | 80 | echo "Not public!" > home/paulb/public/message2 |
paul@239 | 81 | |
paul@239 | 82 | mkdir -p home/paulb/private |
paul@239 | 83 | echo "Private!" > home/paulb/private/message |
paul@239 | 84 | |
paul@239 | 85 | # Set a permissive umask. |
paul@239 | 86 | |
paul@239 | 87 | umask 0000 |
paul@239 | 88 | |
paul@239 | 89 | mkdir -p home/paulb/shared |
paul@239 | 90 | echo "Editable!" > home/paulb/shared/message |
paul@239 | 91 | |
paul@239 | 92 | # Restore the umask for the remaining files. |
paul@239 | 93 | |
paul@239 | 94 | umask 0022 |
paul@239 | 95 | |
paul@154 | 96 | mkdir -p home/paulb/many |
paul@154 | 97 | cd home/paulb/many |
paul@154 | 98 | |
paul@154 | 99 | # Populate the directory with plenty of files. |
paul@154 | 100 | |
paul@154 | 101 | for N in `seq 1 400`; do |
paul@154 | 102 | echo "Contents of #$N." > "file-$N".txt |
paul@154 | 103 | done |
paul@154 | 104 | |
paul@154 | 105 | cd .. |
paul@154 | 106 | |
paul@308 | 107 | # Put file in the created home directory. |
paul@154 | 108 | |
paul@154 | 109 | cp "$PKGDIR/../docs/LICENCE.txt" . |
paul@154 | 110 | |
paul@308 | 111 | # Put some programs in the same place. |
paul@308 | 112 | |
paul@308 | 113 | cp "$PROGRAMS_DIR/dstest_"* . |
paul@308 | 114 | |
paul@154 | 115 | # Leave the filesystem root. |
paul@154 | 116 | |
paul@154 | 117 | cd ../.. |
paul@154 | 118 | |
paul@154 | 119 | # Create a filesystem image. |
paul@154 | 120 | |
paul@154 | 121 | EXTRA=2000 |
paul@154 | 122 | SIZE=$(du -s -k home | cut -f 1) |
paul@154 | 123 | TOTAL=$(($SIZE + $EXTRA)) |
paul@154 | 124 | |
paul@154 | 125 | dd if=/dev/zero of="$TARGET" bs=1024 count=$TOTAL |
paul@154 | 126 | |
paul@154 | 127 | if ! $(/sbin/mkfs.ext2 -q "$TARGET") ; then |
paul@154 | 128 | exit 1 |
paul@154 | 129 | fi |
paul@154 | 130 | |
paul@239 | 131 | # Add the directories and files to the image. |
paul@154 | 132 | |
paul@308 | 133 | for DIR in home/paulb home/paulb/private home/paulb/public home/paulb/shared home/paulb/many ; do |
paul@244 | 134 | e2access mkdir "$DIR" |
paul@244 | 135 | e2access copy-in $(find "$DIR" -maxdepth 1 -type f | sort) "$DIR" |
paul@239 | 136 | done |
paul@154 | 137 | |
paul@154 | 138 | # Leave the root of the filesystem. |
paul@154 | 139 | |
paul@154 | 140 | cd .. |
paul@154 | 141 | |
paul@155 | 142 | if [ ! "$QUIET" ] ; then |
paul@244 | 143 | e2access ls '' |
paul@244 | 144 | e2access ls 'home' |
paul@244 | 145 | e2access ls 'home/paulb' |
paul@244 | 146 | e2access ls 'home/paulb/many' |
paul@244 | 147 | e2access ls 'home/paulb/private' |
paul@244 | 148 | e2access ls 'home/paulb/public' |
paul@244 | 149 | e2access ls 'home/paulb/shared' |
paul@155 | 150 | fi |
paul@154 | 151 | |
paul@154 | 152 | rm -r tmp_e2test |
paul@239 | 153 | |
paul@239 | 154 | # vim: tabstop=4 expandtab shiftwidth=4 |