Compare commits

...

3 commits

Author SHA1 Message Date
6e69a63a4c alpine: Usar comillas para todo y limpiar
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-06-18 12:09:50 -03:00
c54d827579 Modulo de nginx 2022-06-18 12:05:42 -03:00
9453a03815 Modulo de data 2022-06-18 12:05:18 -03:00
8 changed files with 106 additions and 19 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ v00001/
boot/
image.squashfs
cache/
tmp.qcow2

View file

@ -5,39 +5,35 @@ local t = require("utils/templater")
-- Returns nil when no failure, otherwise apk's status code
function alpine.init_rootfs(path, alpine_base_version, alpine_version)
local status = os.execute(t("sudo rm -rf {{path}} && mkdir -p {{path}}", {path = path}))
local status = os.execute(t("sudo rm -rf '{{path}}' && mkdir -p '{{path}}'", {path = path}))
if not (status == 0) then return status end
local url = t("https://dl-cdn.alpinelinux.org/alpine/v{{base_version}}/releases/x86_64/alpine-minirootfs-{{version}}-x86_64.tar.gz", {
base_version = alpine_base_version,
version = alpine_version
})
local status = os.execute(t("cd {{path}} && wget --no-verbose -O- {{url}} | tar zx", { path = path, url = url }))
local status = os.execute(t("cd '{{path}}' && wget --no-verbose -O- '{{url}}' | tar zx", { path = path, url = url }))
if not (status == 0) then return status end
end
function alpine.move_boot(path)
local status = os.execute(t("sudo rm -rf {{path}}/../boot && sudo mv {{path}}/boot {{path}}/../ && sudo mkdir {{path}}/boot", {
local status = os.execute(t("sudo rm -rf '{{path}}/../boot' && sudo mv '{{path}}/boot' '{{path}}/../' && sudo mkdir '{{path}}/boot'", {
path = path,
}))
if not (status == 0) then return status end
end
function alpine.make_squashfs(path, output_path)
local status = os.execute(t("sudo mksquashfs {{path}} {{output_path}} -comp zstd -Xcompression-level 3 -noappend -quiet && sudo chown $(id -u):$(id -g) {{output_path}}", {
local status = os.execute(t("sudo mksquashfs '{{path}}' '{{output_path}}' -comp zstd -Xcompression-level 3 -noappend -quiet && sudo chown $(id -u):$(id -g) '{{output_path}}'", {
path = path,
output_path = output_path,
}))
if not (status == 0) then return status end
-- status = os.execute(t("qemu-img convert {{output_path}} {{output_path}}.qcow2 -O qcow2", {
-- output_path = output_path,
-- }))
-- if not (status == 0) then return status end
end
function alpine.mkdir(rootfs_path, path)
local real_path = rootfs_path..path
local cmd = t("mkdir -p {{real_path}}", {
local cmd = t("mkdir -p '{{real_path}}'", {
real_path = real_path,
})
@ -46,10 +42,20 @@ 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
local cmd = t("mkdir -p {{real_dirname}} && test -f {{real_path}} || exit 0 && sudo chown $(id -u) {{real_path}}", {
local cmd = t("mkdir -p '{{real_dirname}}' && test -f '{{real_path}}' || exit 0 && sudo chown $(id -u) '{{real_path}}'", {
real_path = real_path,
real_dirname = utils.dirname(real_path),
})
@ -68,7 +74,7 @@ end
function alpine.symlink(rootfs_path, path, target)
local real_path = rootfs_path..path
local cmd = t("mkdir -p {{real_dirname}} && ln -s {{target}} {{real_path}}", {
local cmd = t("mkdir -p '{{real_dirname}}' && ln -s '{{target}}' '{{real_path}}'", {
real_path = real_path,
target = target,
real_dirname = utils.dirname(real_path),
@ -82,7 +88,7 @@ end
function alpine.chmod(rootfs_path, path, perms)
local real_path = rootfs_path..path
local cmd = t("chmod {{perms}} {{real_path}}", {
local cmd = t("chmod '{{perms}}' '{{real_path}}'", {
real_path = real_path,
perms = perms,
})
@ -104,17 +110,20 @@ function alpine.make_world(rootfs_path, packages)
utils.join_table(packages, "\n"))
if err then return err end
local status = os.execute("sudo apk upgrade --clean-protected --root "..rootfs_path)
local params = { rootfs_path = rootfs_path }
local status = os.execute(t("sudo apk upgrade --clean-protected --root '{{rootfs_path}}'",
params))
if not (status == 0) then return status end
local status = os.execute(t("rm {{rootfs_path}}/etc/apk/cache",
{ rootfs_path = rootfs_path }))
local status = os.execute(t("rm '{{rootfs_path}}/etc/apk/cache'",
params))
if not (status == 0) then return status end
end
-- Returns nil when no failure, otherwise status code
function alpine.set_password(rootfs_path, user, password)
local status = os.execute(t("echo '{{password}}\n{{password}}' | sudo chroot {{rootfs_path}} passwd {{user}}", {
local status = os.execute(t("echo '{{password}}\n{{password}}' | sudo chroot '{{rootfs_path}}' passwd '{{user}}'", {
password = password,
rootfs_path = rootfs_path,
user = user,

51
modules/data.lua Normal file
View file

@ -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")

View file

@ -7,7 +7,12 @@ local function add_mount(mount)
table.insert(mounts, mount)
add_file("/etc/fstab", utils.join_table(mounts, "\n"))
end
local function add_tmpfs(path)
add_mount("tmpfs "..path.." tmpfs defaults,noexec,nosuid 0 0")
mkdir(path)
end
modules.fstab = {
add_mount = add_mount,
add_tmpfs = add_tmpfs,
}

9
modules/nginx.lua Normal file
View file

@ -0,0 +1,9 @@
modules.nginx = {}
add_packages({ "nginx" })
modules.fstab.add_tmpfs("/var/lib/nginx/tmp")
modules.data.add_data_dir("/data/nginx/logs", "/var/log/nginx", "nginx", "nginx")
modules.runit.add_service("nginx", [[#!/bin/sh
exec 2>&1
mkdir /run/nginx || exit 1
exec nginx -g 'daemon off;'
]])

View file

@ -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

View file

@ -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

View file

@ -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,10 +41,12 @@ local function module(name)
require("modules/" .. name)
end
module "fstab"
module "data"
module "kernel"
module "runit"
module "hostname"
module "dhcpcd"
module "nginx"
print("=> Installing and upgrading packages...")
utils.expect_nil(alpine.make_world(root, packages))