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})) 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'", { 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}}'", { path = path, 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 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}}'", { 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) 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 local err = alpine.write_file(rootfs_path, "/etc/apk/world", utils.join_table(packages, "\n")) if err then return err end 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'", 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}}'", { password = password, rootfs_path = rootfs_path, user = user, })) if not (status == 0) then return status end end return alpine