BuildSystem: Write my own disk image perm updater
If user's bash does not have bultin stat, updating image perms was terribly slow. This patch adds a simple c program that does the job without exec overhead
This commit is contained in:
parent
d33a8eac9c
commit
78cd054d59
|
|
@ -3,3 +3,4 @@
|
||||||
build/
|
build/
|
||||||
base/
|
base/
|
||||||
script/fakeroot-context
|
script/fakeroot-context
|
||||||
|
tools/update-image-perms
|
||||||
|
|
|
||||||
|
|
@ -42,9 +42,17 @@ build_toolchain () {
|
||||||
$BANAN_TOOLCHAIN_DIR/build.sh
|
$BANAN_TOOLCHAIN_DIR/build.sh
|
||||||
}
|
}
|
||||||
|
|
||||||
|
build_tools() {
|
||||||
|
perm_tool="$BANAN_TOOLS_DIR/update-image-perms"
|
||||||
|
if [ ! -f "$perm_tool" ] || [ "$perm_tool" -ot "$perm_tool.c" ]; then
|
||||||
|
gcc -O2 -Wall -Wextra -Werror -o "$perm_tool" "$perm_tool.c" || exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
create_image () {
|
create_image () {
|
||||||
build_target all
|
build_target all
|
||||||
build_target install
|
build_target install
|
||||||
|
build_tools
|
||||||
$BANAN_SCRIPT_DIR/image.sh "$1"
|
$BANAN_SCRIPT_DIR/image.sh "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@ export BANAN_BUILD_DIR="$BANAN_ROOT_DIR/build"
|
||||||
|
|
||||||
export BANAN_PORT_DIR="$BANAN_ROOT_DIR/ports"
|
export BANAN_PORT_DIR="$BANAN_ROOT_DIR/ports"
|
||||||
|
|
||||||
|
export BANAN_TOOLS_DIR="$BANAN_ROOT_DIR/tools"
|
||||||
|
|
||||||
export BANAN_SYSROOT="$BANAN_BUILD_DIR/sysroot"
|
export BANAN_SYSROOT="$BANAN_BUILD_DIR/sysroot"
|
||||||
|
|
||||||
export BANAN_FAKEROOT="$BANAN_BUILD_DIR/fakeroot-context"
|
export BANAN_FAKEROOT="$BANAN_BUILD_DIR/fakeroot-context"
|
||||||
|
|
|
||||||
|
|
@ -20,57 +20,38 @@ if [ -z $BANAN_BUILD_DIR ]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$1" == "full" ] || [ ! -f $BANAN_DISK_IMAGE_PATH ]; then
|
if [ "$1" == "full" ] || [ ! -f "$BANAN_DISK_IMAGE_PATH" ]; then
|
||||||
$BANAN_SCRIPT_DIR/image-create.sh || exit 1
|
"$BANAN_SCRIPT_DIR/image-create.sh" || exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
set -u
|
set -u
|
||||||
|
|
||||||
MOUNT_DIR="$BANAN_BUILD_DIR/mount"
|
MOUNT_DIR="$BANAN_BUILD_DIR/mount"
|
||||||
mkdir -p $MOUNT_DIR
|
mkdir -p "$MOUNT_DIR"
|
||||||
|
|
||||||
LOOP_DEV="$(sudo losetup --show -Pf $BANAN_DISK_IMAGE_PATH || exit 1)"
|
LOOP_DEV="$(sudo losetup --show -Pf "$BANAN_DISK_IMAGE_PATH" || exit 1)"
|
||||||
ROOT_PARTITION="${LOOP_DEV}p2"
|
ROOT_PARTITION="${LOOP_DEV}p2"
|
||||||
if [ ! -b $ROOT_PARTITION ]; then
|
if [ ! -b "$ROOT_PARTITION" ]; then
|
||||||
echo "Failed to probe partitions for banan disk image." >&2
|
echo "Failed to probe partitions for banan disk image." >&2
|
||||||
sudo losetup -d $LOOP_DEV
|
sudo losetup -d "$LOOP_DEV"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if sudo mount $ROOT_PARTITION $MOUNT_DIR; then
|
if sudo mount "$ROOT_PARTITION" "$MOUNT_DIR"; then
|
||||||
if (($BANAN_INITRD)); then
|
if (($BANAN_INITRD)); then
|
||||||
fakeroot -i $BANAN_FAKEROOT tar -C $BANAN_SYSROOT -cf $BANAN_SYSROOT.initrd .
|
fakeroot -i "$BANAN_FAKEROOT" tar -C "$BANAN_SYSROOT" -cf "$BANAN_SYSROOT.initrd" .
|
||||||
|
|
||||||
sudo mkdir -p $MOUNT_DIR/boot
|
sudo mkdir -p "$MOUNT_DIR/boot"
|
||||||
sudo cp $BANAN_BUILD_DIR/kernel/banan-os.kernel $MOUNT_DIR/boot/banan-os.kernel
|
sudo cp "$BANAN_BUILD_DIR/kernel/banan-os.kernel" "$MOUNT_DIR/boot/banan-os.kernel"
|
||||||
sudo mv $BANAN_SYSROOT.initrd $MOUNT_DIR/boot/banan-os.initrd
|
sudo mv "$BANAN_SYSROOT.initrd" "$MOUNT_DIR/boot/banan-os.initrd"
|
||||||
else
|
else
|
||||||
sudo rsync -rulHpt $BANAN_SYSROOT/ $MOUNT_DIR
|
sudo rsync -rulHpt "$BANAN_SYSROOT/" "$MOUNT_DIR"
|
||||||
|
|
||||||
fakeroot -i $BANAN_FAKEROOT find $BANAN_SYSROOT -printf './%P|%U|%G|%04m\n' >$BANAN_BUILD_DIR/sysroot-perms.txt
|
fakeroot -i "$BANAN_FAKEROOT" find "$BANAN_SYSROOT" -printf '%P|%U|%G|%04m\n' >"$BANAN_BUILD_DIR/sysroot-perms.txt"
|
||||||
sudo bash -c "
|
sudo "$BANAN_TOOLS_DIR/update-image-perms" "$MOUNT_DIR" "$BANAN_BUILD_DIR/sysroot-perms.txt"
|
||||||
if enable stat &>/dev/null; then
|
|
||||||
while IFS='|' read -r path uid gid mode; do
|
|
||||||
full=\"$MOUNT_DIR/\$path\"
|
|
||||||
stat \"\$full\"
|
|
||||||
if [[ \${STAT[uid]} != \$uid ]] || [[ \${STAT[gid]} != \$gid ]] || [[ \${STAT[perms]} != \$mode ]]; then
|
|
||||||
chown -h \"\$uid:\$gid\" \"\$full\"
|
|
||||||
test ! -h \"\$full\" && chmod \"\$mode\" \"\$full\"
|
|
||||||
fi
|
|
||||||
done <$BANAN_BUILD_DIR/sysroot-perms.txt
|
|
||||||
else
|
|
||||||
while IFS='|' read -r path uid gid mode; do
|
|
||||||
full=\"$MOUNT_DIR/\$path\"
|
|
||||||
if [[ \$(stat -c '%u %g %a' \"\$full\") != \"\$uid \$gid \$mode\" ]]; then
|
|
||||||
chown -h \"\$uid:\$gid\" \"\$full\"
|
|
||||||
test ! -h \"\$full\" && chmod \"\$mode\" \"\$full\"
|
|
||||||
fi
|
|
||||||
done <$BANAN_BUILD_DIR/sysroot-perms.txt
|
|
||||||
fi
|
|
||||||
"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sudo umount $MOUNT_DIR
|
sudo umount "$MOUNT_DIR"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sudo losetup -d $LOOP_DEV
|
sudo losetup -d "$LOOP_DEV"
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,60 @@
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
if (argc != 3)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "usage: %s MOUNT_POINT PERMS_FILE\n", argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mount_fd = open(argv[1], O_RDONLY | O_DIRECTORY);
|
||||||
|
if (mount_fd == -1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "could not open %s: %s\n", argv[1], strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE* perm_fp = fopen(argv[2], "r");
|
||||||
|
if (perm_fp == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "could not open %s: %s\n", argv[2], strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char buffer[1024];
|
||||||
|
while (fgets(buffer, sizeof(buffer), perm_fp))
|
||||||
|
{
|
||||||
|
char path[PATH_MAX];
|
||||||
|
uid_t uid;
|
||||||
|
gid_t gid;
|
||||||
|
mode_t mode;
|
||||||
|
|
||||||
|
if (sscanf(buffer, "%[^|\n]|%d|%d|%o\n", path, &uid, &gid, &mode) != 4)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
struct stat st;
|
||||||
|
if (fstatat(mount_fd, path, &st, AT_SYMLINK_NOFOLLOW) != 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (st.st_uid != uid || st.st_gid != gid)
|
||||||
|
if (fchownat(mount_fd, path, uid, gid, AT_SYMLINK_NOFOLLOW) != 0)
|
||||||
|
fprintf(stderr, "fchownat: %s: %s\n", path, strerror(errno));
|
||||||
|
|
||||||
|
const mode_t mode_mask = S_ISUID | S_ISGID | S_ISVTX | 0777;
|
||||||
|
if (mode != (st.st_mode & mode_mask))
|
||||||
|
if (fchmodat(mount_fd, path, mode, AT_SYMLINK_NOFOLLOW) != 0)
|
||||||
|
fprintf(stderr, "fchmodat: %s: %s\n", path, strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(perm_fp);
|
||||||
|
close(mount_fd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue