Merge pull request #324 from marineam/grub

Grub2 preview
This commit is contained in:
Michael Marineau 2014-09-02 10:22:27 -07:00
commit 3de1613a99
8 changed files with 69 additions and 29 deletions

View File

@ -201,9 +201,12 @@ start_image() {
}
finish_image() {
local disk_layout="$1"
local root_fs_dir="$2"
local image_contents="$3"
local image_name="$1"
local disk_layout="$2"
local root_fs_dir="$3"
local image_contents="$4"
local disk_img="${BUILD_DIR}/${image_name}"
# Record directories installed to the state partition.
# Explicitly ignore entries covered by existing configs.
@ -217,12 +220,13 @@ finish_image() {
${tmp_ignore} "${root_fs_dir}/etc"
# Only configure bootloaders if there is a boot partition
if mountpoint -q "${root_fs_dir}"/boot/efi; then
if mountpoint -q "${root_fs_dir}"/boot; then
${BUILD_LIBRARY_DIR}/configure_bootloaders.sh \
--arch=${ARCH} \
--disk_layout="${disk_layout}" \
--disk_image="${disk_img}" \
--boot_dir="${root_fs_dir}"/usr/boot \
--esp_dir="${root_fs_dir}"/boot/efi \
--esp_dir="${root_fs_dir}"/boot \
--boot_args="${FLAGS_boot_args}"
fi

View File

@ -24,6 +24,7 @@ DEFINE_string boot_args "" \
"Additional boot arguments to pass to the commandline (Default: '')"
DEFINE_string disk_layout "base" \
"The disk layout type to use for this image."
DEFINE_string disk_image "" "The disk image."
# Parse flags
FLAGS "$@" || exit 1
@ -50,7 +51,7 @@ GRUB_DIR="${FLAGS_boot_dir}/grub"
SYSLINUX_DIR="${FLAGS_boot_dir}/syslinux"
# Build configuration files for pygrub/pvgrub
configure_grub() {
configure_pvgrub() {
sudo mkdir -p "${GRUB_DIR}"
# Add hvc0 for hypervisors
@ -176,12 +177,28 @@ copy_to_esp() {
done
}
# Install GRUB2 to the disk image
install_grub() {
# Install under boot/coreos/grub instead of boot/grub to prevent
# more recent versions of pygrub that attempt to read grub2 configs
# from finding it, pygrub and pvgrub must stick with using menu.lst
sudo mkdir -p "${FLAGS_esp_dir}/coreos/grub"
sudo grub-install \
--target=i386-pc \
--modules=part_gpt \
--boot-directory="${FLAGS_esp_dir}/coreos" \
"${FLAGS_disk_image}"
sudo cp "${BUILD_LIBRARY_DIR}/grub.cfg" \
"${FLAGS_esp_dir}/coreos/grub/grub.cfg"
}
[[ -d "${FLAGS_esp_dir}" ]] || die_notrace "--esp_dir is required"
[[ -f "${FLAGS_disk_image}" ]] || die_notrace "--disk_image is required"
if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then
configure_grub
configure_pvgrub
configure_syslinux
if [[ -n "${FLAGS_esp_dir}" ]]; then
copy_to_esp
fi
copy_to_esp
else
error "No bootloader configuration for ${FLAGS_arch}"
fi

View File

@ -112,7 +112,7 @@ EOF
# The remount services are provided by coreos-base/coreos-init
systemd_enable "${root_fs_dir}" "multi-user.target" "remount-usr.service"
finish_image "${disk_layout}" "${root_fs_dir}" "${image_contents}"
finish_image "${image_name}" "${disk_layout}" "${root_fs_dir}" "${image_contents}"
upload_image -d "${BUILD_DIR}/${image_name}.bz2.DIGESTS" \
"${BUILD_DIR}/${image_contents}" \
"${BUILD_DIR}/${image_packages}" \

View File

@ -12,13 +12,13 @@
"type":"efi",
"blocks":"262144",
"fs_type":"vfat",
"mount":"/boot/efi",
"mount":"/boot",
"features": ["syslinux"]
},
"2":{
"label":"BOOT-B",
"type":"coreos-reserved",
"blocks":"131072"
"label":"BIOS-BOOT",
"type":"bios",
"blocks":"4096"
},
"3":{
"label":"USR-A",

View File

@ -162,6 +162,7 @@ def LoadPartitionConfig(options):
# metadata. Kinda odd but the best place I've got with this data structure.
if layout_name == options.disk_layout:
metadata['blocks'] = disk_block_count
metadata['bytes'] = disk_block_count * metadata['block_size']
# Verify 'base' before other layouts because it is inherited by the others
@ -267,15 +268,21 @@ def WritePartitionTable(options, config=None, partitions=None):
if not (config and partitions):
config, partitions = LoadPartitionConfig(options)
# If we are not creating a fresh image all partitions must be compatible.
if not options.create:
if options.create:
Cgpt('create', '-c', '-s', config['metadata']['blocks'], options.disk_image)
else:
# If we are not creating a fresh image all partitions must be compatible.
GetPartitionTableFromImage(options, config, partitions)
if not all(p['image_compat'] for p in partitions.itervalues()):
raise InvalidLayout("New disk layout is incompatible existing image")
Cgpt('create', '-c', '-s', config['metadata']['blocks'], options.disk_image)
# Extend the disk image size as needed
with open(options.disk_image, 'r+') as image_fd:
image_fd.truncate(config['metadata']['bytes'])
Cgpt('repair', options.disk_image)
syslinux = None
syslinux = False
hybrid = None
for partition in partitions.itervalues():
if partition['type'] != 'blank':
Cgpt('add', '-i', partition['num'],
@ -287,13 +294,16 @@ def WritePartitionTable(options, config=None, partitions=None):
options.disk_image)
features = partition.get('features', [])
if not syslinux and 'syslinux' in features:
syslinux = partition['num']
if not hybrid and ('syslinux' in features or 'hybrid' in features):
hybrid = partition['num']
if 'syslinux' in features:
syslinux = True
if hybrid:
# Enable legacy boot flag and generate a hybrid MBR partition table
Cgpt('add', '-i', hybrid, '-B1', options.disk_image)
if syslinux and options.mbr_boot_code:
# Enable legacy boot flag and generate a hybrid MBR partition table
Cgpt('add', '-i', syslinux, '-B1', options.disk_image)
try:
with open(options.mbr_boot_code, 'r') as mbr_fd:
mbr_code = mbr_fd.read()

9
build_library/grub.cfg Normal file
View File

@ -0,0 +1,9 @@
# Use both default console and ttyS0
serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
terminal_input console serial
terminal_output console serial
# Re-use the existing syslinux configuration
echo "Loading SYSLINUX configuration..."
insmod syslinuxcfg
syslinux_configfile -s /syslinux/syslinux.cfg

View File

@ -81,7 +81,7 @@ EOF
disable_read_write=${FLAGS_FALSE}
fi
finish_image "${disk_layout}" "${root_fs_dir}" "${image_contents}"
finish_image "${image_name}" "${disk_layout}" "${root_fs_dir}" "${image_contents}"
# Make the filesystem un-mountable as read-write.
if [[ ${disable_read_write} -eq ${FLAGS_TRUE} ]]; then

View File

@ -276,7 +276,7 @@ setup_disk_image() {
"${BUILD_LIBRARY_DIR}/disk_util" --disk_layout="${disk_layout}" \
mount "${VM_TMP_IMG}" "${VM_TMP_ROOT}"
local SYSLINUX_DIR="${VM_TMP_ROOT}/boot/efi/syslinux"
local SYSLINUX_DIR="${VM_TMP_ROOT}/boot/syslinux"
if [[ $(_get_vm_opt BOOT_KERNEL) -eq 0 ]]; then
sudo mv "${SYSLINUX_DIR}/default.cfg.A" "${SYSLINUX_DIR}/default.cfg"
fi
@ -336,9 +336,9 @@ _run_onmetal_fs_hook() {
local arg='8250.nr_uarts=5 console=ttyS4,115200n8 modprobe.blacklist=mei_me'
local timeout=150 # 15 seconds
local totaltimeout=3000 # 5 minutes
sudo sed -i "${VM_TMP_ROOT}/boot/efi/syslinux/boot_kernel.cfg" \
sudo sed -i "${VM_TMP_ROOT}/boot/syslinux/boot_kernel.cfg" \
-e 's/console=[^ ]*//g' -e "s/\\(append.*$\\)/\\1 ${arg}/"
sudo sed -i "${VM_TMP_ROOT}/boot/efi/syslinux/syslinux.cfg" \
sudo sed -i "${VM_TMP_ROOT}/boot/syslinux/syslinux.cfg" \
-e "s/^TIMEOUT [0-9]*/TIMEOUT ${timeout}/g" \
-e "s/^TOTALTIMEOUT [0-9]*/TOTALTIMEOUT ${totaltimeout}/g"
}
@ -346,7 +346,7 @@ _run_onmetal_fs_hook() {
_run_gce_fs_hook() {
# HACKITY HACK until OEMs can customize bootloader configs
local arg='console=ttyS0,115200n8'
sudo sed -i "${VM_TMP_ROOT}/boot/efi/syslinux/boot_kernel.cfg" \
sudo sed -i "${VM_TMP_ROOT}/boot/syslinux/boot_kernel.cfg" \
-e 's/console=[^ ]*//g' -e "s/\\(append.*$\\)/\\1 ${arg}/"
}