Arch install automation

 2020-08-06

background

I have an OEM desktop that I use as a headless (without a display) server. I found that I nuke this server very often when I mess something up and have to reinstall. With my recent move, I switched to Arch as my primary operating system. Reinstalling became a hassel. This post will take a different approach. Rather than give instructions on how to make or build everything, I will talk about what I did and why I did it.

The plan

First, I need to make a custom ISO. This iso had to boot regularly, somehow tell my laptop its IP, and finally get sshed into by Ansible, which will allow Ansible to install Arch for me. I broke the problem down into stages. Stage 1: Get the IP Stage 2: Auth into ssh without a password Stage 3: Install arch

Stage 1

Stage 1 was the simplest part of the project. I started by finding out how I could get my IP. For this goal, I employed Python. I used Python due to my familiarity with it. To get the IP address, I used socket. This is the function I used to get my IP.

def get_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    ip = s.getsockname()[0]
    s.close()
    return ip

It's a nifty little piece of code that pings Google's DNS server and then gets the current IP from the socket and closes the socket. The next part stumped me for a while. How would I send the IP to my laptop? After about 5 minutes of deliberation, I reasoned that I might be able to run a Django webserver. After 5 more minutes of thinking, I came to the conclusion that while the idea was right and using a web server would be a good choice, Django was overkill. Enter our next contender, Flask. Flask is the arch of the python web dev world. While Django neatly handles 99% of the work. Flask is the opposite. You need to configure and handle everything (well, almost everything).

I would like to show my flask code, but it's nothing special. It receives the request (a get in this case) and prints the IP, which is sent as a parameter of the get request. To send the get request, I used the requests Python module. The function for this was surprisingly simple to use.

def send_ip(ip):
    URL = "http://127.0.0.1:5000"
    PARAMS = {"ip": ip}
    r = requests.get(url=URL, data=PARAMS)
    print(r)
    print(r.text)

Currently the url is set to localhost, but you can change it to the IP of your system. This is where my first road block was hit. I originally planned to use systemd to load up and run the program for me. Sadly, the system's IP isn't set until the DHCP negotiations are made by dhcpcd. So I moved to Plan B using the zshrc. At this point, I spent 2 days learning, making, and testing a custom Arch iso. At this point, I realized that I could tackle stage 2 with the ISO. The only side effect present is that when you ssh, you will not see a propmt on the iso.

Stage 2

A password isn't the only way to login to a remote host using ssh. The alternative, more secure method is a SSH key. A key pair (one private and one public) can be made fairly quickly. I recommend the following the guide by github on how to set it up. So I add a section in the iso to copy my key into the iso. allowing me to login and execute commands as root without ever inputting the password.

Stage 3

This was what held me back for a short while (okay, it wasn't a short while, it was a month). At one point, I thought of using "chef" instead of ansible. While I did start porting to Chef. It quickly ended because Chef just doesn't have modules like ansible parted module. The install is pretty standard except instead of using arch-chroot /mnt you have to run command outside and when it needs to be run inside a chroot you use arch-chroot /mnt command. There was 2 problems that really stumped me when I ran into them. The first one was the grub install. I used grub over systemd because the server only had BIOS support.

I discovered you don't give grub-install a partition but rather the entier disk like grub-install /dev/sda. After this the install was pretty smooth sailing. Until I hit the user creation section. Currently I haven't figured out how to fix the issue but the one solution I am trying to get working is chpasswd. If someone figures out how it works please do hit me up. I really would like to solve it. I  will be uploading everything to my github so anyone can use it. One last thing. If you want to install arch do it yourself. Do your research and it will pay off. Installing arch teachs you alot about how linux works. Also don't use arch as your first Linux distro use linux mint or pop os

  — This is nullrequest signing off