diff options
author | binary <me@rgoncalves.se> | 2020-11-10 20:46:04 +0100 |
---|---|---|
committer | binary <me@rgoncalves.se> | 2020-11-10 20:46:04 +0100 |
commit | 0a673e300800b91342499cec9cd482b5d2d9c603 (patch) | |
tree | 752309b2f30928063340afefe39682679491a451 /roles/vmm/files | |
parent | 89c7516b1f9ec4ce7ce2947f98f2070c242f4459 (diff) | |
download | infrastructure-0a673e300800b91342499cec9cd482b5d2d9c603.tar.gz |
Fully working init for alpine vm
Diffstat (limited to 'roles/vmm/files')
-rw-r--r-- | roles/vmm/files/init_vm_alpine.yml (renamed from roles/vmm/files/init_vm_serial.py) | 105 |
1 files changed, 66 insertions, 39 deletions
diff --git a/roles/vmm/files/init_vm_serial.py b/roles/vmm/files/init_vm_alpine.yml index 46e34d7..d9dc0d5 100644 --- a/roles/vmm/files/init_vm_serial.py +++ b/roles/vmm/files/init_vm_alpine.yml @@ -5,23 +5,67 @@ import subprocess import sys import os -import time - +from serial_macro import * USAGE = f"USAGE: {sys.argv[0]} vm_guest gate ip mask ssh_key" +def init_network(): + send_cmds(ser, [ + [1, "setup-interfaces"], + [1, ""], + [1, f"{IP}"], + [1, f"{MASK}"], + [1, f"{GATE}"], + [1, "no"], + [1, "ifdown -a"], + [10, "ifup -a"], + [1, "rc-update add networking"] + ]) -def send_cmd(ser, delay, cmd): - ser.write(f"{cmd}\n".encode("utf-8")) - time.sleep(delay) -def send_cmds(ser, cmds): - for cmd in cmds: - send_cmd(ser, cmd[0], cmd[1]) +def init_dns(): + send_cmds(ser, [ + [1, f"setup-dns"], + [1, ""], + [1, f"{DNS}"], + ]) + + +def init_disk(): + send_cmds(ser, [ + [10, "apk add e2fsprogs sfdisk syslinux"], + [1, "setup-disk"], + [1, ""], + [10, "sys"], + [30, "y"], + ]) + + +def init_ssh(): + send_cmds(ser, [ + [5, "apk add openssh"], + [1, "mkdir /root/.ssh"], + [1, f"echo '{SSHKEY}' > /root/.ssh/authorized_keys"], + [1, f"echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config"], + [1, "/etc/init.d/sshd restart"] + ]) + +def init_packages(): + send_cmds(ser, [ + [1, "echo https://mirror.ungleich.ch/mirror/packages/alpine/latest-stable/main/ > /etc/apk/repositories "], + [5, "apk update"] + ]) def main(): + global ser + global IP + global GATE + global MASK + global DNS + global SSHKEY + COM = "/dev/" BAUD = 115200 TIMEOUT = 1 @@ -48,40 +92,23 @@ def main(): SSHKEY = sys.argv[6] ser = serial.Serial(COM, BAUD, timeout=TIMEOUT) - send_cmd(ser, 1, "root") - - # virtual interface - send_cmds(ser, [ - [1, "setup-interfaces"], - [1, ""], - [1, f"{IP}"], - [1, f"{MASK}"], - [1, f"{GATE}"], - [1, "no"], - [1, "ifdown -a"], - [10, "ifup -a"] - ]) - - # dns - send_cmds(ser, [ - [1, "setup-dns"], - [1, f"{HOST}"], - [1, f"{DNS}"] - ]) - - # ssh - send_cmds(ser, [ - [5, "apk add openssh"], - [1, "mkdir /root/.ssh"], - [1, f"echo '{SSHKEY}' > /root/.ssh/authorized_keys"], - [1, f"echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config"], - [1, "/etc/init.d/sshd restart"] - ]) - + # first boot :: live + init_network() + init_dns() + init_packages() + init_disk() + send_cmd(ser, 70, "reboot") ser.close() - print(COM) + ser = serial.Serial(COM, BAUD, timeout=TIMEOUT) + send_cmd(ser, 1, "root") + # second boot :: disk + init_network() + init_dns() + init_ssh() + init_packages() + ser.close() if __name__ == "__main__": |