Browse Source

add grub build system

next_step
Luigi Cruz 3 years ago
parent
commit
a10c50b1cf
7 changed files with 96 additions and 11 deletions
  1. +1
    -0
      .dockerignore
  2. +3
    -1
      Dockerfile
  3. +1
    -0
      ansible/roles/base/templates/etc/netplan/netcfg.yaml
  4. +7
    -1
      ansible/roles/grub/tasks/main.yaml
  5. +5
    -5
      ansible/tasks/mount.yaml
  6. +2
    -1
      ansible/tasks/umount.yaml
  7. +77
    -3
      build_grub.sh

+ 1
- 0
.dockerignore View File

@@ -3,3 +3,4 @@ work/
deploy/
apt-cacher-ng/
.git/objects/*
build/

+ 3
- 1
Dockerfile View File

@@ -18,7 +18,9 @@ RUN apt install -y --no-install-recommends \
kpartx \
lbzip2 \
sudo \
e2fsprogs
e2fsprogs \
dosfstools \
qemu-utils

RUN python3 -m pip install ansible



+ 1
- 0
ansible/roles/base/templates/etc/netplan/netcfg.yaml View File

@@ -4,3 +4,4 @@ network:
ethernets:
eth0:
dhcp4: true
dhcp6: true

+ 7
- 1
ansible/roles/grub/tasks/main.yaml View File

@@ -6,7 +6,13 @@

- name: Install GRUB Bootloader
apt:
name: grub2
name: grub-efi-amd64

- name: Install GRUB System
shell: |
set -e
grub-install --target=x86_64-efi
GRUB_DISABLE_OS_PROBER=true update-grub

- name: Install Linux firmware
apt:


+ 5
- 5
ansible/tasks/mount.yaml View File

@@ -1,8 +1,8 @@
---
- name: Mount pseudo filesystems
connection: local
shell: mount -o bind '{{ item }}' '{{ lookup('env', 'ANSIBLE_ROOTFS_DIR') }}{{ item }}'
with_items:
- /proc
- /dev
- /dev/pts
shell: |
mount -o bind,ro /dev {{ lookup('env', 'ANSIBLE_ROOTFS_DIR') }}/dev
mount -o bind,ro /dev/pts {{ lookup('env', 'ANSIBLE_ROOTFS_DIR') }}/dev/pts
mount -t proc none {{ lookup('env', 'ANSIBLE_ROOTFS_DIR') }}/proc
mount -t sysfs none {{ lookup('env', 'ANSIBLE_ROOTFS_DIR') }}/sys

+ 2
- 1
ansible/tasks/umount.yaml View File

@@ -5,4 +5,5 @@
with_items:
- /proc
- /dev/pts
- /dev
- /dev
- /sys

+ 77
- 3
build_grub.sh View File

@@ -4,15 +4,89 @@ set -e

INPUT_DIR="$1"
IMAGE_NAME="$(basename $INPUT_DIR)"
GRUB_IMG="$TARGET$BUILD/export.img"
GRUB_DIR="$TARGET$BUILD/grub"
LOOP_DEV="/dev/loop5"

printf "Copying base rootfs... "
rm -fr $GRUB_DIR
printf "Creating image file... "
if [ "$(lsblk | grep $(basename $LOOP_DEV))" ]; then
losetup -d $LOOP_DEV
fi
rm -fr $GRUB_IMG
printf "(OK)\n"

echo "Allocating image file..."
if [ -f $GRUB_IMG ]; then
rm -fr $GRUB_IMG
fi
qemu-img create $GRUB_IMG 10G
parted -s -a optimal -- $GRUB_IMG \
mklabel gpt \
mkpart primary fat32 1MiB 270MiB \
mkpart primary ext4 1GiB -0 \
name 1 uefi \
name 2 root \
set 1 esp on
losetup --partscan $LOOP_DEV $GRUB_IMG
partprobe $LOOP_DEV
echo "Allocating image file... (OK)"

printf "Find partitions UUID... "
ROOT_UUID=$(blkid | grep "^$LOOP_DEV" | grep ' PARTLABEL="root" ' | grep -o ' PARTUUID="[^"]\+"' | sed -e 's/^ //' )
EFI_UUID=$(blkid | grep "^$LOOP_DEV" | grep ' PARTLABEL="uefi" ' | grep -o ' PARTUUID="[^"]\+"' | sed -e 's/^ //' )
printf "(OK)\n"

echo "Formatting image file..."
mkfs -t vfat -F 32 -n EFI ${LOOP_DEV}p1
mkfs -t ext4 -L root ${LOOP_DEV}p2
echo "Formatting image file... (OK)"

printf "Mounting root filesystem... "
if [ -d "$GRUB_DIR" ]; then
umount -q $GRUB_DIR || true
rm -fr $GRUB_DIR
fi
mkdir -p $GRUB_DIR
mount $ROOT_UUID $GRUB_DIR
printf "(OK)\n"

printf "Copying filesystem to image... "
cp -rp $INPUT_DIR/. $GRUB_DIR/
printf "(OK)\n"

printf "Mounting efi filesystem... "
if [ -d "$GRUB_DIR/boot/efi" ]; then
umount -q $GRUB_DIR/boot/efi || true
rm -fr $GRUB_DIR/boot/efi
fi
mkdir -p $GRUB_DIR/boot/efi
mount $EFI_UUID $GRUB_DIR/boot/efi
printf "(OK)\n"

echo "Run GRUB installation tasks..."
pushd $TARGET/ansible > /dev/null
export ANSIBLE_ROOTFS_DIR=$GRUB_DIR
ansible-playbook -v 03_grub.yaml
echo "Run GRUB installation tasks... (OK)"
echo "Run GRUB installation tasks... (OK)"

printf "Installing fstab... "
echo "$ROOT_UUID / ext4 errors=remount-ro 0 1" > $GRUB_DIR/etc/fstab
echo "$EFI_UUID /boot/efi vfat defaults 0 1" >> $GRUB_DIR/etc/fstab
printf "(OK)\n"

printf "Umounting everything... "
umount -q $GRUB_DIR/boot/efi
umount -q $GRUB_DIR
printf "(OK)\n"

echo "Checking filesystem..."
fsck -y ${LOOP_DEV}p2
echo "Checking filesystem... (OK)"

echo "Detaching loop device..."
losetup -d $LOOP_DEV
echo "Detaching loop device... (OK)"

printf "Moving export image file... "
mv $GRUB_IMG "${EXPORT_DIR}/${PISDR_IMG_NAME}-${IMAGE_NAME}-amd64.img" > /dev/null
printf "(OK)\n"

Loading…
Cancel
Save