Compare commits
No commits in common. "6e69a63a4cabf3f34cc6a36e19bcfac5a09d3af4" and "4b071f025309e66e9429122a6052448cfcb74ce8" have entirely different histories.
6e69a63a4c
...
4b071f0253
8 changed files with 19 additions and 106 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,4 +3,3 @@ v00001/
|
|||
boot/
|
||||
image.squashfs
|
||||
cache/
|
||||
tmp.qcow2
|
||||
|
|
41
alpine.lua
41
alpine.lua
|
@ -5,35 +5,39 @@ 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,
|
||||
})
|
||||
|
||||
|
@ -42,20 +46,10 @@ 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),
|
||||
})
|
||||
|
@ -74,7 +68,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),
|
||||
|
@ -88,7 +82,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,
|
||||
})
|
||||
|
@ -110,20 +104,17 @@ function alpine.make_world(rootfs_path, packages)
|
|||
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))
|
||||
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'",
|
||||
params))
|
||||
local status = os.execute(t("rm {{rootfs_path}}/etc/apk/cache",
|
||||
{ rootfs_path = rootfs_path }))
|
||||
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,
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
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")
|
|
@ -7,12 +7,7 @@ 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,
|
||||
}
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
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;'
|
||||
]])
|
|
@ -23,6 +23,8 @@ 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() {
|
||||
|
@ -233,9 +235,6 @@ 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", [[
|
||||
|
@ -256,8 +255,6 @@ 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
|
||||
|
||||
|
|
6
qemu.sh
6
qemu.sh
|
@ -5,12 +5,8 @@ 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 modules=ext4 init=/sbin/runit-init $append" $qemuappend
|
||||
-append "root=/dev/sda rootfstype=squashfs init=/sbin/runit-init $append" $qemuappend
|
||||
|
|
|
@ -29,9 +29,6 @@ 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))
|
||||
|
@ -41,12 +38,10 @@ 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))
|
||||
|
|
Loading…
Reference in a new issue