Skip to content

Instantly share code, notes, and snippets.

@Vbitz
Last active April 26, 2024 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vbitz/421b3343d65e032b09777aa11ac70b50 to your computer and use it in GitHub Desktop.
Save Vbitz/421b3343d65e032b09777aa11ac70b50 to your computer and use it in GitHub Desktop.
Starlark script to handle basic init including networking for a Linux system.
def init(ctx):
# Mount /proc filesystem.
linux_mount("proc", "proc", "/proc", ensure_path = True)
# Mount other filesystems.
linux_mount("devtmpfs", "devtmpfs", "/dev", ensure_path = True)
linux_mount("sysfs", "none", "/sys", ensure_path = True)
linux_mount("cgroup2", "cgroup2", "/sys/fs/cgroup")
linux_mount("bpf", "/bpf", "/sys/fs/bpf")
linux_mount("debugfs", "debugfs", "/sys/kernel/debug", ignore_error = True)
linux_mount("devpts", "devpts", "/dev/pts", ensure_path = True)
linux_mount("tmpfs", "tmpfs", "/dev/shm", ensure_path = True)
# Symlink /dev/fd to /proc/self/fd
path_symlink("/proc/self/fd", "/dev/fd")
# Configure Networking
linux_network_interface_up("lo")
# Use a larger MTU for eth0 to improve performance.
# If this number is too large it can affect compatibility and decrease performance.
linux_network_interface_up("eth0", mtu = 4096)
# Configure the network interface with DHCP.
# Set the router ip as well. This will enable the next stage.
ctx.router_ip = linux_network_interface_configure("eth0")
# Write /etc/resolv.conf
path_ensure("/etc")
# Ensure that out /etc/resolv.conf is being written. On some distributions this is a symlink.
if path_exists("/etc/resolv.conf"):
path_remove("/etc/resolv.conf")
# Write the router_ip as the name server.
file_write("/etc/resolv.conf", "nameserver {}\n".format(ctx.router_ip))
# Ensure the random pool is large enough.
linux_ensure_random_pool()
# Set PATH and HOME to sensible defaults.
if env_get("PATH", "") == "":
env_set("PATH", "/bin:/usr/bin:/sbin:/usr/sbin")
if env_get("HOME", "/") == "/":
env_set("HOME", "/root")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment