windvn/input/stage98/bin/login

124 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env python3
# This script is ran when booting into the OS, and the setup isn't finished
import os
import traceback
import subprocess
debug = False
os.environ["PATH"] = "/sbin:/usr/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
def procWait(proc):
rtn = proc.wait()
if debug: print(rtn)
if rtn != 0: raise processError
def call(*args,**kwargs):
if debug: print(str(args[0]))
proc = subprocess.Popen(*args,**kwargs)
procWait(proc)
def callo(*args,**kwargs):
data = b""
proc = subprocess.Popen(*args,**kwargs,stdout=subprocess.PIPE)
while True:
b = proc.stdout.read()
if b == b"": break
data += b
procWait(proc)
return data.decode("utf-8")
def mchoice(choices):
while True:
choice = input("Choice: ").lower()
if not choice in choices: continue
return choice
def main():
while True:
print("Welcome to opus-dvn.")
print("1: Finish installation")
print("2: Clone to other device")
print("3: Open bash")
print("4: Shut down")
choice = mchoice(["1","2","3","4"])
if choice == "1":
if os.path.isfile("/isRawImage"):
print("Resizing / to fill disk...")
dev = callo(["findmnt","-n","-o","SOURCE","/"]).strip("\t\r\n")
disk = dev[:-1]
part = dev[-1]
call(["growpart",disk,part])
call(["resize2fs",dev])
os.remove("/isRawImage")
call(["dpkg-reconfigure","keyboard-configuration"])
call(["apt","install","console-setup","locales","tzdata","--yes"])
call(["dpkg-reconfigure","locales"])
call(["dpkg-reconfigure","tzdata"])
print("")
hostname = input("Hostname: ")
with open("/etc/hosts","w",encoding="utf-8") as fh:
fh.write("""\
127.0.0.1 localhost
127.0.1.1 $HOSTNAME
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters\
""".replace("$HOSTNAME",hostname)
)
with open("/etc/hostname","w",encoding="utf-8") as fh:
fh.write(hostname)
os.environ["HOSTNAME"] = hostname
print("")
print("Configure your network now, using nmtui? (y/n)")
if mchoice(["y","n"]) == "y": call(["nmtui"])
print("")
call(["groupadd","-f","sudo"])
username = False
while True:
username = input("First user (Administrator): ").lower().strip(" ")
if username == "root": continue
try:
call(["id","-u",username])
except:
try:
call(["adduser",username])
except:
try:
call(["deluser","--remove-home",username])
except:
continue
try:
call(["usermod","-a","-G","sudo",username])
except:
print("Warning: Couldn't add user to sudo group!")
break
print("")
print("Booting into new environment ...")
os.remove("/bin/login")
os.rename("/bin/login.bak","/bin/login")
return
if choice == "2":
print("Not implemented.")
continue
if choice == "3":
call(["bash"])
return
if choice == "4":
call(["poweroff"])
return
try:
main()
except:
print(traceback.format_exc())
input("Press RETURN to quit.")