# craft `craft` is a minimal build automation tool for Linux. ## Build Executing the `build.sh` script inside the root of the craft source code will create the craft Linux executable. Only the `go` tool and a posix shell is required. ## Usage Call the `craft` tool like this: ``` ./craft -repo https://example.com -commit 8f36088eee1cfbec4ddac4d652101db3b29eed45 ``` This will create a temporary directory, clone the repository including submodules to that location, execute the `build` function from the `craft.sh` file inside the root of the repo and remove the temporary directory. Log output is redirected to the calling terminal. ## Build Arch Linux guest virtual machine for builds Craft will use a QEMU virtual machine to execute build processes. The following commands can be used to create an Arch Linux based virtual machine suitable for this purpose. It is expected that the Arch Linux installation image file (`archlinux-2024.02.01-x86_64.iso`) or a symbolic link to it is present in the root of the craft repository. ``` # create virtual machine disk file and boot with installation media qemu-img create -f qcow2 vms/archlinux.qcow2 30G qemu-system-x86_64 \ -enable-kvm \ -m 4G \ -nic user,model=virtio \ -drive file=vms/archlinux.qcow2,media=disk,if=virtio \ -smp cpus=4 \ -nographic \ -cdrom archlinux-2024.02.01-x86_64.iso # press TAB in bootloader menu and append ` console=ttyS0` to kernel args # check if time is synchronized timedatectl # create partitions and file systems parted /dev/vda --script mklabel msdos parted /dev/vda --script mkpart primary 1MiB 2GiB parted /dev/vda --script set 1 boot on parted /dev/vda --script mkpart primary 2GiB 100% mkfs.vfat -n BOOT /dev/vda1 mkfs.btrfs -L ROOT /dev/vda2 # mount root and boot partition mount /dev/vda2 /mnt mount --mkdir /dev/vda1 /mnt/boot # install Arch Linux packages pacstrap -K /mnt \ base \ linux \ linux-firmware \ syslinux \ btrfs-progs \ networkmanager \ chrony \ nano \ neovim \ htop \ which \ openssh \ rsync \ base-devel \ git \ go # system configuration genfstab -L /mnt >> /mnt/etc/fstab arch-chroot /mnt ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime hwclock --systohc sed -i 's/#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g' /etc/locale.gen locale-gen echo 'LANG=en_US.UTF-8' > /etc/locale.conf echo 'craft-archlinux' > /etc/hostname mkinitcpio -P echo 'root' | passwd -s systemctl enable NetworkManager systemctl enable chronyd sed -i 's|#PermitRootLogin prohibit-password|PermitRootLogin yes|g' /etc/ssh/sshd_config echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFB/sCmZZ9lffCfAjbNCHRsW95/s75p5qMp+9Ch4/NPn' > /root/.ssh/authorized_keys systemctl enable sshd # bootloader installation mkdir -p /boot/syslinux cp /usr/lib/syslinux/bios/*.c32 /boot/syslinux/ extlinux --install /boot/syslinux dd bs=440 count=1 conv=notrunc if=/usr/lib/syslinux/bios/mbr.bin of=/dev/vda sed -i 's|root=/dev/sda3 rw|root=/dev/vda2 rw console=ttyS0|g' /boot/syslinux/syslinux.cfg exit poweroff ``` ## Start Arch Linux virtual machine for automated builds The following command starts the Arch Linux QEMU virtual machine in a read-only mode (`snapshot=on`) to prevent altering the original image file. Furthermore the local port 22 of the VM is forwarded to the localhost port 9999 of the host. Thus clients on the host can use SSH to connect to the VM. Again no graphical display is used and only output of the serial console is shown in the terminal. Since the `console=ttyS0` kernel parameter was set in the bootloader configuration of the VM, no manual editing of kernel parameters during runtime is needed (in contrast to the current Arch Linux installation image). ``` qemu-system-x86_64 \ -enable-kvm \ -m 4G \ -net nic,model=virtio -net user,hostfwd=tcp:127.0.0.1:9999-:22 \ -drive file=vms/archlinux.qcow2,media=disk,snapshot=on,if=virtio \ -smp cpus=4 \ -nographic ``` ## Use SSH to log into guest virtual machine Since the SSH server of the guest VM is available to the host on localhost port 9999 it is possible to use the `ssh` command to log into the VM from the host using the password set in image creation with the `passwd` command. ``` ssh \ -p 9999 \ -o UserKnownHostsFile=/dev/null \ -o StrictHostKeyChecking=no \ -i keys/craft_ed25519 \ root@localhost ```