define-alpine/alpine.lua

136 lines
4.1 KiB
Lua
Raw Normal View History

2022-06-18 00:06:19 +00:00
local alpine = {}
local utils = require("utils")
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}))
2022-06-18 00:06:19 +00:00
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 }))
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", {
2022-06-18 00:06:19 +00:00
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}}", {
2022-06-18 00:06:19 +00:00
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}}", {
real_path = real_path,
})
-- XXX: Usar lua-posix
local status = os.execute(cmd)
if not (status == 0) then return status end
end
2022-06-18 15:05:18 +00:00
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
2022-06-18 00:06:19 +00:00
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}}", {
2022-06-18 00:06:19 +00:00
real_path = real_path,
real_dirname = utils.dirname(real_path),
})
-- XXX: Usar lua-posix
local status = os.execute(cmd)
if not (status == 0) then return status end
local file, err = io.open(real_path, "w+")
if not file then return err end
file:write(content)
file:close()
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}}", {
real_path = real_path,
target = target,
real_dirname = utils.dirname(real_path),
})
-- XXX: Usar lua-posix
local status = os.execute(cmd)
if not (status == 0) then return status end
end
function alpine.chmod(rootfs_path, path, perms)
local real_path = rootfs_path..path
local cmd = t("chmod {{perms}} {{real_path}}", {
real_path = real_path,
perms = perms,
})
-- XXX: Usar lua-posix
local status = os.execute(cmd)
if not (status == 0) then return status end
end
-- Returns nil when no failure, otherwise string error or apk's status code
function alpine.make_world(rootfs_path, packages)
2022-06-18 01:46:16 +00:00
local err = alpine.symlink(rootfs_path, "/etc/apk/cache", "../../../cache")
if err then return err end
local err = alpine.mkdir(rootfs_path, "../cache")
if err then return err end
2022-06-18 01:41:34 +00:00
local err = alpine.write_file(rootfs_path,
2022-06-18 00:06:19 +00:00
"/etc/apk/world",
utils.join_table(packages, "\n"))
if err then return err end
2022-06-18 01:46:16 +00:00
local status = os.execute("sudo apk upgrade --clean-protected --root "..rootfs_path)
if not (status == 0) then return status end
local status = os.execute(t("rm {{rootfs_path}}/etc/apk/cache",
{ rootfs_path = rootfs_path }))
2022-06-18 00:06:19 +00:00
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}}", {
2022-06-18 00:06:19 +00:00
password = password,
rootfs_path = rootfs_path,
user = user,
}))
if not (status == 0) then return status end
end
return alpine