From 9453a03815bea3a2655ba3f22637a226b30dd09c Mon Sep 17 00:00:00 2001 From: Nulo Date: Sat, 18 Jun 2022 12:05:18 -0300 Subject: [PATCH] Modulo de data --- .gitignore | 1 + alpine.lua | 10 ++++++++++ modules/data.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++++ modules/runit.lua | 7 +++++-- qemu.sh | 6 +++++- setup.lua | 4 ++++ 6 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 modules/data.lua diff --git a/.gitignore b/.gitignore index b9b4cd4..3bdcd8f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ v00001/ boot/ image.squashfs cache/ +tmp.qcow2 diff --git a/alpine.lua b/alpine.lua index 7114ab0..64c885a 100644 --- a/alpine.lua +++ b/alpine.lua @@ -46,6 +46,16 @@ function alpine.mkdir(rootfs_path, path) if not (status == 0) then return status end end +function alpine.touch(rootfs_path, path) + local real_path = rootfs_path..path + + local cmd = t("touch '{{real_path}}'", { real_path = real_path }) + + -- XXX: Usar lua-posix + local status = os.execute(cmd) + if not (status == 0) then return status end +end + function alpine.write_file(rootfs_path, path, content) local real_path = rootfs_path..path diff --git a/modules/data.lua b/modules/data.lua new file mode 100644 index 0000000..11ebefb --- /dev/null +++ b/modules/data.lua @@ -0,0 +1,51 @@ +local t = require "../utils/templater" +local dirs = {} + +local function generate_mount_data() + local string = "#!/bin/sh\n" + for i=1,#dirs do + local dir = dirs[i] + if dir.type == "file" then + touch(dir.mountpoint) + string = string .. t([[ +touch -a '{{path}}' || exit 1 +mount '{{path}}' '{{mountpoint}}' -o bind,umask=100,uid={{uid}},gid={{gid}} || exit 1 +]], + dir + ) + elseif dir.type == "dir" then + mkdir(dir.mountpoint) + string = string .. t([[ +mkdir -p '{{path}}' || exit 1 +mount '{{path}}' '{{mountpoint}}' -o bind,umask=100,uid={{uid}},gid={{gid}} || exit 1 +]], + dir + ) + end + end + add_file("/usr/local/bin/mount-data", string) + chmod("/usr/local/bin/mount-data", 700) +end +local function add_data_dir(path, mountpoint, uid, gid) + table.insert(dirs, { + type = "dir", + path = path, mountpoint = mountpoint, uid = uid, gid = gid + }) + generate_mount_data() +end +local function add_data_file(path, mountpoint, uid, gid) + table.insert(dirs, { + type = "file", + path = path, mountpoint = mountpoint, uid = uid, gid = gid + }) + generate_mount_data() +end + +modules.data = { + add_data_dir = add_data_dir, + add_data_file = add_data_file, +} + +mkdir("/data") +-- XXX: hardcodeado +modules.fstab.add_mount("/dev/sdb /data ext4 defaults 0 0") diff --git a/modules/runit.lua b/modules/runit.lua index ca149db..886cc6b 100644 --- a/modules/runit.lua +++ b/modules/runit.lua @@ -23,8 +23,6 @@ modules.runit = { add_service = add_service, } -modules.fstab.add_mount("tmpfs /var/log tmpfs defaults 0 0") - -- Estos scripts fueron robados de Void Linux add_executable("/etc/runit/functions", [[ msg() { @@ -235,6 +233,9 @@ fi msg "Mounting all non-network filesystems..." mount -a -t "nosysfs,nonfs,nonfs4,nosmbfs,nocifs" -O no_netdev || emergency_shell +# data module +msg "Creating and mounting data directories..." +/usr/local/bin/mount-data || emergency_shell ]]) add_executable("/etc/runit/core-services/04-swap.sh", [[ @@ -255,6 +256,8 @@ msg "Setting hostname..." hostname -F /etc/hostname ]]) +modules.data.add_data_file("/data/dmesg.log", "/var/log/dmesg.log", "root", "root") + -- Initial boot add_executable("/etc/runit/1", [[#!/bin/sh diff --git a/qemu.sh b/qemu.sh index c3dbe06..00b9c4d 100755 --- a/qemu.sh +++ b/qemu.sh @@ -5,8 +5,12 @@ if test "$NOGRAPHIC" = true; then qemuappend="-nographic" fi +qemu-img create -f qcow2 tmp.qcow2 1G +mkfs.ext4 tmp.qcow2 + sudo chown root:$(id -u) -R boot/ && sudo chmod g+rw -R boot/ qemu-system-x86_64 -enable-kvm -m 2048 \ -drive file=image.squashfs,media=disk \ + -drive file=tmp.qcow2,media=disk \ -kernel boot/vmlinuz-virt -initrd boot/initramfs-virt \ - -append "root=/dev/sda rootfstype=squashfs init=/sbin/runit-init $append" $qemuappend + -append "root=/dev/sda rootfstype=squashfs modules=ext4 init=/sbin/runit-init $append" $qemuappend diff --git a/setup.lua b/setup.lua index 6c1185d..836d2a7 100644 --- a/setup.lua +++ b/setup.lua @@ -29,6 +29,9 @@ end function mkdir(path) utils.expect_nil(alpine.mkdir(root, path)) end +function touch(path) + utils.expect_nil(alpine.touch(root, path)) +end print("=> Initializing rootfs...") utils.expect_nil(alpine.init_rootfs(root, alpine_base_version, alpine_version)) @@ -38,6 +41,7 @@ local function module(name) require("modules/" .. name) end module "fstab" +module "data" module "kernel" module "runit" module "hostname"