mirror of
https://github.com/flatcar/scripts.git
synced 2025-11-28 22:12:10 +01:00
commit
7320602927
@ -8,6 +8,7 @@ import json
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
import uuid
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
# First sector we can use.
|
# First sector we can use.
|
||||||
@ -35,7 +36,7 @@ def LoadPartitionConfig(filename):
|
|||||||
valid_keys = set(('_comment', 'metadata', 'layouts'))
|
valid_keys = set(('_comment', 'metadata', 'layouts'))
|
||||||
valid_layout_keys = set((
|
valid_layout_keys = set((
|
||||||
'_comment', 'type', 'num', 'label', 'blocks', 'block_size', 'fs_blocks',
|
'_comment', 'type', 'num', 'label', 'blocks', 'block_size', 'fs_blocks',
|
||||||
'fs_block_size', 'features'))
|
'fs_block_size', 'features', 'uuid'))
|
||||||
|
|
||||||
if not os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
raise ConfigNotFound('Partition config %s was not found!' % filename)
|
raise ConfigNotFound('Partition config %s was not found!' % filename)
|
||||||
@ -77,6 +78,15 @@ def LoadPartitionConfig(filename):
|
|||||||
raise InvalidLayout(
|
raise InvalidLayout(
|
||||||
'Filesystem may not be larger than partition: %s %s: %d > %d' %
|
'Filesystem may not be larger than partition: %s %s: %d > %d' %
|
||||||
(layout_name, part['label'], part['fs_bytes'], part['bytes']))
|
(layout_name, part['label'], part['fs_bytes'], part['bytes']))
|
||||||
|
|
||||||
|
if 'uuid' in part:
|
||||||
|
try:
|
||||||
|
# double check the string formatting
|
||||||
|
part['uuid'] = str(uuid.UUID(part['uuid']))
|
||||||
|
except ValueError as e:
|
||||||
|
raise InvalidLayout('Invalid uuid %r: %s' % (part['uuid'], e))
|
||||||
|
else:
|
||||||
|
part['uuid'] = str(uuid.uuid4())
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
raise InvalidLayout('Layout is missing required entries: %s' % e)
|
raise InvalidLayout('Layout is missing required entries: %s' % e)
|
||||||
|
|
||||||
@ -291,9 +301,9 @@ def WriteLayoutFunction(options, sfile, func_name, image_type, config):
|
|||||||
# Pass 2: Write out all the cgpt add commands.
|
# Pass 2: Write out all the cgpt add commands.
|
||||||
for partition in partitions:
|
for partition in partitions:
|
||||||
if partition['type'] != 'blank':
|
if partition['type'] != 'blank':
|
||||||
sfile.write('$GPT add -i %d -b $CURR -s %s -t %s -l %s $1 && ' % (
|
sfile.write('$GPT add -i %d -b $CURR -s %s -t %s -l %s -u %s $1 && ' % (
|
||||||
partition['num'], str(partition['var']), partition['type'],
|
partition['num'], str(partition['var']), partition['type'],
|
||||||
partition['label']))
|
partition['label'], partition['uuid']))
|
||||||
if partition['type'] == 'efi':
|
if partition['type'] == 'efi':
|
||||||
sfile.write('$GPT boot -p -b $2 -i %d $1\n' % partition['num'])
|
sfile.write('$GPT boot -p -b $2 -i %d $1\n' % partition['num'])
|
||||||
|
|
||||||
@ -475,6 +485,26 @@ def GetNum(options, image_type, layout_filename, label):
|
|||||||
return '-1'
|
return '-1'
|
||||||
|
|
||||||
|
|
||||||
|
def GetUuid(options, image_type, layout_filename, label):
|
||||||
|
"""Returns the unique partition UUID for a given label.
|
||||||
|
|
||||||
|
Note: Only useful if the UUID is specified in the config file, otherwise
|
||||||
|
the value returned unlikely to be what is actually used in the image.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
options: Flags passed to the script
|
||||||
|
image_type: Type of image eg base/test/dev/prod
|
||||||
|
layout_filename: Path to partition configuration file
|
||||||
|
label: Label of the partition you want to read from
|
||||||
|
Returns:
|
||||||
|
String containing the requested UUID
|
||||||
|
"""
|
||||||
|
|
||||||
|
partitions = GetPartitionTableFromConfig(options, layout_filename, image_type)
|
||||||
|
partition = GetPartitionByLabel(partitions, label)
|
||||||
|
return partition['uuid']
|
||||||
|
|
||||||
|
|
||||||
def DoDebugOutput(options, image_type, layout_filename):
|
def DoDebugOutput(options, image_type, layout_filename):
|
||||||
"""Prints out a human readable disk layout in on-disk order.
|
"""Prints out a human readable disk layout in on-disk order.
|
||||||
|
|
||||||
@ -547,6 +577,10 @@ def main(argv):
|
|||||||
'usage': ['<image_type>', '<partition_config_file>', '<label>'],
|
'usage': ['<image_type>', '<partition_config_file>', '<label>'],
|
||||||
'func': GetNum,
|
'func': GetNum,
|
||||||
},
|
},
|
||||||
|
'readuuid': {
|
||||||
|
'usage': ['<image_type>', '<partition_config_file>', '<label>'],
|
||||||
|
'func': GetUuid,
|
||||||
|
},
|
||||||
'debug': {
|
'debug': {
|
||||||
'usage': ['<image_type>', '<partition_config_file>'],
|
'usage': ['<image_type>', '<partition_config_file>'],
|
||||||
'func': DoDebugOutput,
|
'func': DoDebugOutput,
|
||||||
|
|||||||
@ -37,6 +37,9 @@ FLAGS "$@" || exit 1
|
|||||||
eval set -- "${FLAGS_ARGV}"
|
eval set -- "${FLAGS_ARGV}"
|
||||||
switch_to_strict_mode
|
switch_to_strict_mode
|
||||||
|
|
||||||
|
# Useful for getting partition UUID values
|
||||||
|
. "${BUILD_LIBRARY_DIR}/disk_layout_util.sh" || exit 1
|
||||||
|
|
||||||
# Only let dm-verity block if rootfs verification is configured.
|
# Only let dm-verity block if rootfs verification is configured.
|
||||||
# Also, set which device mapper correspondes to verity
|
# Also, set which device mapper correspondes to verity
|
||||||
dev_wait=0
|
dev_wait=0
|
||||||
@ -63,10 +66,11 @@ verity_common="${verity_common} dm_verity.dev_wait=${dev_wait}"
|
|||||||
# The templates are used by the installer to populate partition 12 with
|
# The templates are used by the installer to populate partition 12 with
|
||||||
# the correct bootloader configuration.
|
# the correct bootloader configuration.
|
||||||
if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then
|
if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then
|
||||||
# TODO: For some reason the /dev/disk/by-uuid is not being generated by udev
|
|
||||||
# in the initramfs. When we figure that out, switch to root=UUID=${UUID}.
|
|
||||||
sudo mkdir -p ${FLAGS_to}
|
sudo mkdir -p ${FLAGS_to}
|
||||||
|
|
||||||
|
# Get partition UUIDs from the json config
|
||||||
|
ROOTA="PARTUUID=$(get_uuid base ROOT-A)"
|
||||||
|
ROOTB="PARTUUID=$(get_uuid base ROOT-B)"
|
||||||
|
|
||||||
# Build configuration files for pygrub/pvgrub
|
# Build configuration files for pygrub/pvgrub
|
||||||
GRUB_DIR="${FLAGS_to}/boot/grub"
|
GRUB_DIR="${FLAGS_to}/boot/grub"
|
||||||
@ -77,11 +81,11 @@ timeout 0
|
|||||||
|
|
||||||
title CoreOS A
|
title CoreOS A
|
||||||
root (hd0,0)
|
root (hd0,0)
|
||||||
kernel /syslinux/vmlinuz.A ${common_args} root=HDROOTA cros_legacy
|
kernel /syslinux/vmlinuz.A ${common_args} root=${ROOTA} cros_legacy
|
||||||
|
|
||||||
title CoreOS B
|
title CoreOS B
|
||||||
root (hd0,0)
|
root (hd0,0)
|
||||||
kernel /syslinux/vmlinuz.B ${common_args} root=HDROOTB cros_legacy
|
kernel /syslinux/vmlinuz.B ${common_args} root=${ROOTB} cros_legacy
|
||||||
EOF
|
EOF
|
||||||
info "Emitted ${GRUB_DIR}/menu.lst.A"
|
info "Emitted ${GRUB_DIR}/menu.lst.A"
|
||||||
|
|
||||||
@ -91,7 +95,6 @@ EOF
|
|||||||
sudo sh -c "cat ${GRUB_DIR}/menu.lst.A >> ${GRUB_DIR}/menu.lst.B"
|
sudo sh -c "cat ${GRUB_DIR}/menu.lst.A >> ${GRUB_DIR}/menu.lst.B"
|
||||||
info "Emitted ${GRUB_DIR}/menu.lst.B"
|
info "Emitted ${GRUB_DIR}/menu.lst.B"
|
||||||
|
|
||||||
# HDROOTA will be replaced in image_to_vm.sh
|
|
||||||
sudo cp ${GRUB_DIR}/menu.lst.A ${GRUB_DIR}/menu.lst
|
sudo cp ${GRUB_DIR}/menu.lst.A ${GRUB_DIR}/menu.lst
|
||||||
|
|
||||||
# /boot/syslinux must be installed in partition 12 as /syslinux/.
|
# /boot/syslinux must be installed in partition 12 as /syslinux/.
|
||||||
@ -126,7 +129,7 @@ EOF
|
|||||||
label coreos.A
|
label coreos.A
|
||||||
menu label coreos.A
|
menu label coreos.A
|
||||||
kernel vmlinuz.A
|
kernel vmlinuz.A
|
||||||
append ${common_args} root=HDROOTA i915.modeset=1 cros_legacy
|
append ${common_args} root=${ROOTA} i915.modeset=1 cros_legacy
|
||||||
EOF
|
EOF
|
||||||
info "Emitted ${SYSLINUX_DIR}/root.A.cfg"
|
info "Emitted ${SYSLINUX_DIR}/root.A.cfg"
|
||||||
|
|
||||||
@ -134,7 +137,7 @@ EOF
|
|||||||
label coreos.B
|
label coreos.B
|
||||||
menu label coreos.B
|
menu label coreos.B
|
||||||
kernel vmlinuz.B
|
kernel vmlinuz.B
|
||||||
append ${common_args} root=HDROOTB i915.modeset=1 cros_legacy
|
append ${common_args} root=${ROOTB} i915.modeset=1 cros_legacy
|
||||||
EOF
|
EOF
|
||||||
info "Emitted ${SYSLINUX_DIR}/root.B.cfg"
|
info "Emitted ${SYSLINUX_DIR}/root.B.cfg"
|
||||||
|
|
||||||
|
|||||||
@ -112,6 +112,14 @@ get_num() {
|
|||||||
cgpt_py readnum "${image_type}" "${DISK_LAYOUT_PATH}" ${label}
|
cgpt_py readnum "${image_type}" "${DISK_LAYOUT_PATH}" ${label}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get_uuid() {
|
||||||
|
local image_type=$1
|
||||||
|
local label=$2
|
||||||
|
get_disk_layout_path
|
||||||
|
|
||||||
|
cgpt_py readuuid "${image_type}" "${DISK_LAYOUT_PATH}" ${label}
|
||||||
|
}
|
||||||
|
|
||||||
check_valid_layout() {
|
check_valid_layout() {
|
||||||
local image_type=$1
|
local image_type=$1
|
||||||
get_disk_layout_path
|
get_disk_layout_path
|
||||||
|
|||||||
@ -21,6 +21,7 @@
|
|||||||
{
|
{
|
||||||
"num": 3,
|
"num": 3,
|
||||||
"label":"ROOT-A",
|
"label":"ROOT-A",
|
||||||
|
"uuid":"7130c94a-213a-4e5a-8e26-6cce9662f132",
|
||||||
"type":"coreos-rootfs",
|
"type":"coreos-rootfs",
|
||||||
"blocks":"4194304",
|
"blocks":"4194304",
|
||||||
"fs_blocks":"262144"
|
"fs_blocks":"262144"
|
||||||
@ -28,6 +29,7 @@
|
|||||||
{
|
{
|
||||||
"num": 4,
|
"num": 4,
|
||||||
"label":"ROOT-B",
|
"label":"ROOT-B",
|
||||||
|
"uuid":"e03dd35c-7c2d-4a47-b3fe-27f15780a57c",
|
||||||
"type":"coreos-rootfs",
|
"type":"coreos-rootfs",
|
||||||
"blocks":"4194304",
|
"blocks":"4194304",
|
||||||
"fs_blocks":"262144"
|
"fs_blocks":"262144"
|
||||||
@ -35,6 +37,7 @@
|
|||||||
{
|
{
|
||||||
"num": 5,
|
"num": 5,
|
||||||
"label":"ROOT-C",
|
"label":"ROOT-C",
|
||||||
|
"uuid":"d82521b4-07ac-4f1c-8840-ddefedc332f3",
|
||||||
"type":"coreos-rootfs",
|
"type":"coreos-rootfs",
|
||||||
"blocks":"1"
|
"blocks":"1"
|
||||||
},
|
},
|
||||||
@ -68,6 +71,7 @@
|
|||||||
{
|
{
|
||||||
"num": 3,
|
"num": 3,
|
||||||
"label":"ROOT-A",
|
"label":"ROOT-A",
|
||||||
|
"uuid":"7130c94a-213a-4e5a-8e26-6cce9662f132",
|
||||||
"type":"coreos-rootfs",
|
"type":"coreos-rootfs",
|
||||||
"blocks":"2539520",
|
"blocks":"2539520",
|
||||||
"fs_blocks":"262144"
|
"fs_blocks":"262144"
|
||||||
@ -75,6 +79,7 @@
|
|||||||
{
|
{
|
||||||
"num": 4,
|
"num": 4,
|
||||||
"label":"ROOT-B",
|
"label":"ROOT-B",
|
||||||
|
"uuid":"e03dd35c-7c2d-4a47-b3fe-27f15780a57c",
|
||||||
"type":"coreos-rootfs",
|
"type":"coreos-rootfs",
|
||||||
"blocks":"1"
|
"blocks":"1"
|
||||||
}
|
}
|
||||||
@ -89,6 +94,7 @@
|
|||||||
{
|
{
|
||||||
"num": 3,
|
"num": 3,
|
||||||
"label":"ROOT-A",
|
"label":"ROOT-A",
|
||||||
|
"uuid":"7130c94a-213a-4e5a-8e26-6cce9662f132",
|
||||||
"type":"coreos-rootfs",
|
"type":"coreos-rootfs",
|
||||||
"blocks":"860160",
|
"blocks":"860160",
|
||||||
"fs_blocks":"102400"
|
"fs_blocks":"102400"
|
||||||
@ -96,6 +102,7 @@
|
|||||||
{
|
{
|
||||||
"num": 4,
|
"num": 4,
|
||||||
"label":"ROOT-B",
|
"label":"ROOT-B",
|
||||||
|
"uuid":"e03dd35c-7c2d-4a47-b3fe-27f15780a57c",
|
||||||
"type":"coreos-rootfs",
|
"type":"coreos-rootfs",
|
||||||
"blocks":"1"
|
"blocks":"1"
|
||||||
},
|
},
|
||||||
@ -110,6 +117,7 @@
|
|||||||
{
|
{
|
||||||
"num": 3,
|
"num": 3,
|
||||||
"label":"ROOT-A",
|
"label":"ROOT-A",
|
||||||
|
"uuid":"7130c94a-213a-4e5a-8e26-6cce9662f132",
|
||||||
"type":"coreos-rootfs",
|
"type":"coreos-rootfs",
|
||||||
"blocks":"2097152",
|
"blocks":"2097152",
|
||||||
"fs_blocks":"262144"
|
"fs_blocks":"262144"
|
||||||
@ -117,6 +125,7 @@
|
|||||||
{
|
{
|
||||||
"num": 4,
|
"num": 4,
|
||||||
"label":"ROOT-B",
|
"label":"ROOT-B",
|
||||||
|
"uuid":"e03dd35c-7c2d-4a47-b3fe-27f15780a57c",
|
||||||
"type":"coreos-rootfs",
|
"type":"coreos-rootfs",
|
||||||
"blocks":"2097152",
|
"blocks":"2097152",
|
||||||
"fs_blocks":"262144"
|
"fs_blocks":"262144"
|
||||||
|
|||||||
@ -145,41 +145,6 @@ fi
|
|||||||
TEMP_PMBR="${TEMP_DIR}"/pmbr
|
TEMP_PMBR="${TEMP_DIR}"/pmbr
|
||||||
dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
|
dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
|
||||||
|
|
||||||
# Setup the bootloader configs to be correct for the image type
|
|
||||||
TEMP_ESP_MNT=$(mktemp -d)
|
|
||||||
cleanup() {
|
|
||||||
safe_umount "${TEMP_ESP_MNT}"
|
|
||||||
rmdir "${TEMP_ESP_MNT}"
|
|
||||||
}
|
|
||||||
trap cleanup INT TERM EXIT
|
|
||||||
mkdir -p "${TEMP_ESP_MNT}"
|
|
||||||
enable_rw_mount "${TEMP_ESP}"
|
|
||||||
sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}"
|
|
||||||
|
|
||||||
SYSLINUX_DIR=${TEMP_ESP_MNT}/syslinux
|
|
||||||
BOOT_DIR=${TEMP_ESP_MNT}/boot
|
|
||||||
GRUB_DIR=${BOOT_DIR}/grub
|
|
||||||
|
|
||||||
# Assume that if we are booting syslinux we are fully virtualized
|
|
||||||
sudo sed -i -e "s%HDROOTA%/dev/sda${NUM_ROOTFS_A}%g" ${SYSLINUX_DIR}/root.A.cfg
|
|
||||||
sudo sed -i -e "s%HDROOTB%/dev/sda${NUM_ROOTFS_B}%g" ${SYSLINUX_DIR}/root.B.cfg
|
|
||||||
|
|
||||||
# Update the menu.lst to be right for xen pygrub/pv-grub or just guess for
|
|
||||||
# everything else.
|
|
||||||
if [ "${FLAGS_format}" = "xen" ]; then
|
|
||||||
sudo sed -i -e "s%HDROOTA%/dev/xvda${NUM_ROOTFS_A}%g" ${GRUB_DIR}/menu.lst
|
|
||||||
sudo sed -i -e "s%HDROOTB%/dev/xvda${NUM_ROOTFS_B}%g" ${GRUB_DIR}/menu.lst
|
|
||||||
else
|
|
||||||
sudo sed -i -e "s%HDROOTA%/dev/sda${NUM_ROOTFS_A}%g" ${GRUB_DIR}/menu.lst
|
|
||||||
sudo sed -i -e "s%HDROOTB%/dev/sda${NUM_ROOTFS_B}%g" ${GRUB_DIR}/menu.lst
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat ${GRUB_DIR}/menu.lst ${SYSLINUX_DIR}/root.A.cfg
|
|
||||||
|
|
||||||
# Unmount everything prior to building a final image
|
|
||||||
trap - INT TERM EXIT
|
|
||||||
cleanup
|
|
||||||
|
|
||||||
# Set up a new partition table
|
# Set up a new partition table
|
||||||
PARTITION_SCRIPT_PATH=$( tempfile )
|
PARTITION_SCRIPT_PATH=$( tempfile )
|
||||||
write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
|
write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user