Created
August 8, 2016 19:57
-
-
Save carlocaione/78c2af23dd6b27c23968dfc8ace52d1d to your computer and use it in GitHub Desktop.
gpt2mbr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# Copyright (C) 2015 Endless Mobile, Inc. | |
# Licensed under the GPLv2 | |
[ $# -lt 1 ] && exit 1 | |
# TODO: check if in use | |
# TODO: parse both /dev/sdaX or sdaX | |
# TODO: check if partition contains EOS | |
# TODO: check if we are passing a partition and not a disk | |
get_last_char() { | |
ret=$1 | |
while [ "${#ret}" != "1" ]; do | |
ret=${ret#?} | |
done | |
echo $ret | |
} | |
root_part=$1 | |
partno=$(get_last_char ${root_part}) | |
swap_partno=$((partno + 1)) | |
esp_partno=1 | |
root_disk=${root_part%?} | |
swap_part=${root_disk}${swap_partno} | |
esp_part=${root_disk}${esp_partno} | |
esp_part_dev=${esp_part#/dev/} | |
root_disk_dev=${root_disk#/dev/} | |
if [ -z "${root_disk}" ]; then | |
echo "eos-gpt2mbr: no root disk found for $root_part" | |
exit 0 | |
fi | |
# udev might still be busy probing the disk, meaning that it will be in use. | |
udevadm settle | |
# take current partition table | |
parts=$(sfdisk -d $root_disk) | |
# check the last partition on the disk | |
lastpart=$(echo "$parts" | sed -n -e '$ s/[^:]*\([0-9]\) :.*$/\1/p') | |
if [ $lastpart -eq $swap_partno ]; then | |
# already have an extra partition, perhaps we were halfway through creating | |
# a swap partition but didn't finish. Remove it to try again below. | |
parts=$(echo "$parts" | sed '$d') | |
elif [ $lastpart -gt $swap_partno ]; then | |
echo "repartition: found $lastpart partitions?" | |
exit 0 | |
fi | |
# remove ESP | |
parts=$(echo "$parts" | sed -e "/$esp_part_dev/d") | |
# calculate new partition sizes | |
disk_size=$(blockdev --getsz $root_disk) | |
part_size=$(blockdev --getsz $root_part) | |
part_start=$(echo "$parts" | sed -n -e '$ s/.*start=[ ]\+\([0-9]\+\).*$/\1/p') | |
part_end=$(( part_start + part_size )) | |
echo "Dsize $disk_size Psize $part_size Pstart $part_start Pend $part_end" | |
# Calculate the new root partition size, assuming that it will expand to fill | |
# the remainder of the disk | |
new_size=$(( disk_size - part_start )) | |
# If we find ourselves with >100GB free space, we'll use the final 4GB as | |
# a swap partition | |
added_space=$(( new_size - part_size )) | |
if [ $added_space -gt 209715200 ]; then | |
new_size=$(( new_size - 8388608 )) | |
swap_start=$(( part_start + new_size )) | |
# Align swap partition start to 1MB boundary | |
residue=$(( swap_start % 2048 )) | |
if [ $residue -gt 0 ]; then | |
swap_start=$(( swap_start + 2048 - residue )) | |
# might as well bump up root partition size too, instead of leaving a gap | |
new_size=$(( new_size + 2048 - residue )) | |
fi | |
fi | |
if [ $new_size -gt $part_size ]; then | |
echo "Try to resize $root_part to fill $new_size sectors" | |
parts=$(echo "$parts" | sed -e "$ s/size=[0-9\t ]*,/size=$new_size,/") | |
fi | |
if [ -n "$swap_start" ]; then | |
# Create swap partition | |
echo "Create swap partition at $swap_start" | |
parts="$parts | |
start=$swap_start, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F" | |
fi | |
# Reset partition indexes | |
parts=$(echo "$parts" | sed -e "s/${root_disk_dev}.//") | |
# GPT -> DOS | |
parts=$(echo "$parts" | sed -e "s/label: gpt/label: dos/") | |
# Remove GPT types and UUIDs | |
parts=$(echo "$parts" | sed -e "s/, type=.*//") | |
echo "$parts" | |
echo "$parts" | sfdisk --force --no-reread $root_disk | |
ret=$? | |
echo "sfdisk returned $ret" | |
udevadm settle | |
[ -e "$swap_part" ] && mkswap -L eos-swap $swap_part |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment