Merge pull request #27 from marineam/rootfs

Rootfs by UUID
This commit is contained in:
Michael Marineau 2013-07-08 13:38:21 -07:00
commit 7320602927
5 changed files with 64 additions and 45 deletions

View File

@ -8,6 +8,7 @@ import json
import os
import re
import sys
import uuid
from optparse import OptionParser
# First sector we can use.
@ -35,7 +36,7 @@ def LoadPartitionConfig(filename):
valid_keys = set(('_comment', 'metadata', 'layouts'))
valid_layout_keys = set((
'_comment', 'type', 'num', 'label', 'blocks', 'block_size', 'fs_blocks',
'fs_block_size', 'features'))
'fs_block_size', 'features', 'uuid'))
if not os.path.exists(filename):
raise ConfigNotFound('Partition config %s was not found!' % filename)
@ -77,6 +78,15 @@ def LoadPartitionConfig(filename):
raise InvalidLayout(
'Filesystem may not be larger than partition: %s %s: %d > %d' %
(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:
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.
for partition in partitions:
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['label']))
partition['label'], partition['uuid']))
if partition['type'] == 'efi':
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'
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):
"""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>'],
'func': GetNum,
},
'readuuid': {
'usage': ['<image_type>', '<partition_config_file>', '<label>'],
'func': GetUuid,
},
'debug': {
'usage': ['<image_type>', '<partition_config_file>'],
'func': DoDebugOutput,

View File

@ -37,6 +37,9 @@ FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
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.
# Also, set which device mapper correspondes to verity
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 correct bootloader configuration.
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}
# 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
GRUB_DIR="${FLAGS_to}/boot/grub"
@ -77,11 +81,11 @@ timeout 0
title CoreOS A
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
root (hd0,0)
kernel /syslinux/vmlinuz.B ${common_args} root=HDROOTB cros_legacy
kernel /syslinux/vmlinuz.B ${common_args} root=${ROOTB} cros_legacy
EOF
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"
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
# /boot/syslinux must be installed in partition 12 as /syslinux/.
@ -126,7 +129,7 @@ EOF
label coreos.A
menu label coreos.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
info "Emitted ${SYSLINUX_DIR}/root.A.cfg"
@ -134,7 +137,7 @@ EOF
label coreos.B
menu label coreos.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
info "Emitted ${SYSLINUX_DIR}/root.B.cfg"

View File

@ -112,6 +112,14 @@ get_num() {
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() {
local image_type=$1
get_disk_layout_path

View File

@ -21,6 +21,7 @@
{
"num": 3,
"label":"ROOT-A",
"uuid":"7130c94a-213a-4e5a-8e26-6cce9662f132",
"type":"coreos-rootfs",
"blocks":"4194304",
"fs_blocks":"262144"
@ -28,6 +29,7 @@
{
"num": 4,
"label":"ROOT-B",
"uuid":"e03dd35c-7c2d-4a47-b3fe-27f15780a57c",
"type":"coreos-rootfs",
"blocks":"4194304",
"fs_blocks":"262144"
@ -35,6 +37,7 @@
{
"num": 5,
"label":"ROOT-C",
"uuid":"d82521b4-07ac-4f1c-8840-ddefedc332f3",
"type":"coreos-rootfs",
"blocks":"1"
},
@ -68,6 +71,7 @@
{
"num": 3,
"label":"ROOT-A",
"uuid":"7130c94a-213a-4e5a-8e26-6cce9662f132",
"type":"coreos-rootfs",
"blocks":"2539520",
"fs_blocks":"262144"
@ -75,6 +79,7 @@
{
"num": 4,
"label":"ROOT-B",
"uuid":"e03dd35c-7c2d-4a47-b3fe-27f15780a57c",
"type":"coreos-rootfs",
"blocks":"1"
}
@ -89,6 +94,7 @@
{
"num": 3,
"label":"ROOT-A",
"uuid":"7130c94a-213a-4e5a-8e26-6cce9662f132",
"type":"coreos-rootfs",
"blocks":"860160",
"fs_blocks":"102400"
@ -96,6 +102,7 @@
{
"num": 4,
"label":"ROOT-B",
"uuid":"e03dd35c-7c2d-4a47-b3fe-27f15780a57c",
"type":"coreos-rootfs",
"blocks":"1"
},
@ -110,6 +117,7 @@
{
"num": 3,
"label":"ROOT-A",
"uuid":"7130c94a-213a-4e5a-8e26-6cce9662f132",
"type":"coreos-rootfs",
"blocks":"2097152",
"fs_blocks":"262144"
@ -117,6 +125,7 @@
{
"num": 4,
"label":"ROOT-B",
"uuid":"e03dd35c-7c2d-4a47-b3fe-27f15780a57c",
"type":"coreos-rootfs",
"blocks":"2097152",
"fs_blocks":"262144"

View File

@ -145,41 +145,6 @@ fi
TEMP_PMBR="${TEMP_DIR}"/pmbr
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
PARTITION_SCRIPT_PATH=$( tempfile )
write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"