Skip to content

Commit

Permalink
Add an image-default.yaml, pass JSON to create_disk.sh
Browse files Browse the repository at this point in the history
The way we parse an `image.yaml` and then convert it
into arguments for `create_disk.sh` is unwieldy.

Instead, convert `image.yaml` into JSON, inheriting default values
from a separate `image-default.yaml` we ship.  Then pass
that JSON to `create_disk.sh`.

This only converts the `rootfs` argument, but if people
are OK with this I can go ahead and convert everything
else - getting us to where the only argument to `create_disk.sh`
is a JSON file that's already been pre-validated.
  • Loading branch information
cgwalters committed Nov 11, 2020
1 parent b8c22bf commit b769f56
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
20 changes: 12 additions & 8 deletions src/cmd-buildextend-metal
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,19 @@ if [ "${image_type}" = dasd ] || [ "${image_type}" = metal4k ]; then
ignition_platform_id=metal
fi

yaml2json() {
python3 -c 'import sys, json, yaml; json.dump(yaml.safe_load(sys.stdin), sys.stdout)' < "$1" > "$2"
}

# Convert the image.yaml to JSON so that it can be more easily parsed
# by the shell script in create_disk.sh.
yaml2json "$configdir/image.yaml" image-config.json
yaml2json "/usr/lib/coreos-assembler/image-default.yaml" image-default.json
cat image-default.json image-config.json | jq -s add > image.json

# bootfs
bootfs_type="$(python3 -c 'import sys, yaml; print(yaml.safe_load(sys.stdin).get("bootfs", "ext4"))' < "$configdir/image.yaml")"

# First parse the old luks_rootfs flag (a custom "stringified bool")
rootfs_type="$(python3 -c 'import sys, yaml; lf=yaml.safe_load(sys.stdin).get("luks_rootfs", ""); print("luks" if lf.lower() in ("yes", "true") else "")' < "$configdir/image.yaml")"
if [ -z "${rootfs_type}" ]; then
# Now the newer rootfs flag
rootfs_type="$(python3 -c 'import sys, yaml; print(yaml.safe_load(sys.stdin).get("rootfs", "xfs"))' < "$configdir/image.yaml")"
fi
rootfs_type=$(jq -re .rootfs < image.json)

# fs-verity requires block size = page size. We need to take that into account
# in the disk size estimation due to higher fragmentation on larger blocks.
Expand Down Expand Up @@ -253,6 +257,7 @@ target_drive=("-drive" "if=none,id=target,format=${image_format},file=${path}.tm

runvm "${target_drive[@]}" -- \
/usr/lib/coreos-assembler/create_disk.sh \
--config "$(pwd)"/image.json \
--buildid "${build}" \
--imgid "${img}" \
--grub-script /usr/lib/coreos-assembler/grub.cfg \
Expand All @@ -262,7 +267,6 @@ runvm "${target_drive[@]}" -- \
--ostree-remote "${ostree_remote}" \
--ostree-repo "${ostree_repo}" \
--rootfs-size "${rootfs_size}" \
--rootfs "${rootfs_type}" \
"${disk_args[@]}"
/usr/lib/coreos-assembler/finalize-artifact "${path}.tmp" "${path}"

Expand Down
17 changes: 14 additions & 3 deletions src/create_disk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ Options:
--ostree-repo: location of the ostree repo
--rootfs-size: Create the root filesystem with specified size
--boot-verity: Provide this to enable ext4 fs-verity for /boot
--rootfs: xfs|ext4verity|luks
--no-x86-bios-partition: don't create a BIOS partition on x86_64
You probably don't want to run this script by hand. This script is
run as part of 'coreos-assembler build'.
EOC
}

config=
disk=
rootfs_size="0"
boot_verity=0
rootfs_type="xfs"
x86_bios_partition=1
extrakargs=""

while [ $# -gt 0 ];
do
flag="${1}"; shift;
case "${flag}" in
--config) config="${1}"; shift;;
--disk) disk="${1}"; shift;;
--buildid) buildid="${1}"; shift;;
--imgid) imgid="${1}"; shift;;
Expand All @@ -68,7 +68,6 @@ do
--ostree-repo) ostree="${1}"; shift;;
--rootfs-size) rootfs_size="${1}"; shift;;
--boot-verity) boot_verity=1;;
--rootfs) rootfs_type="${1}" shift;;
--no-x86-bios-partition) x86_bios_partition=0;;
*) echo "${flag} is not understood."; usage; exit 10;;
esac;
Expand All @@ -84,6 +83,7 @@ arch="$(uname -m)"

disk=$(realpath /dev/disk/by-id/virtio-target)

config="${config:?--config must be defined}"
buildid="${buildid:?--buildid must be defined}"
imgid="${imgid:?--imgid must be defined}"
ostree="${ostree:?--ostree-repo must be defined}"
Expand All @@ -92,6 +92,17 @@ remote_name="${remote_name:?--ostree-remote must be defined}"
grub_script="${grub_script:?--grub-script must be defined}"
os_name="${os_name:?--os_name must be defined}"

getconfig() {
k=$1
jq -re .$k < ${config}
}

# First parse the old luks_rootfs flag (a custom "stringified bool")
if test "$(getconfig luks_rootfs)" = "yes"; then
rootfs_type=luks
else
rootfs_type=$(getconfig rootfs)
fi
case "${rootfs_type}" in
xfs|ext4verity|luks|btrfs) ;;
*) echo "Invalid rootfs type: ${rootfs_type}" 1>&2; exit 1;;
Expand Down
2 changes: 2 additions & 0 deletions src/image-default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This file contains defaults for image.yaml that is used by create_disk.sh
rootfs: "xfs"

0 comments on commit b769f56

Please sign in to comment.