From d0fe5e6f9a0591544cc433a89da8a2c5a8146564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Mon, 23 Jan 2017 23:52:15 +0100 Subject: [PATCH] initramfs: add support for root.img with partition table Installing grub on root.img require some space before the filesystem. Create it by adding partition table to root.img. This commit take care of assembling dmroot device when such partition table is present, while preserving compatibility with partition-less images. QubesOS/qubes-issues#2577 --- dracut/full-dmroot/qubes_cow_setup.sh | 28 +++++++++++++++++++++------ dracut/simple/init.sh | 26 ++++++++++++++++++++----- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/dracut/full-dmroot/qubes_cow_setup.sh b/dracut/full-dmroot/qubes_cow_setup.sh index c02391d..13ef76a 100644 --- a/dracut/full-dmroot/qubes_cow_setup.sh +++ b/dracut/full-dmroot/qubes_cow_setup.sh @@ -56,14 +56,21 @@ log_begin "Waiting for /dev/xvda* devices..." while ! [ -e /dev/xvda ]; do sleep 0.1; done log_end +# prefer first partition if exists +if [ -b /dev/xvda1 ]; then + ROOT_DEV=xvda1 +else + ROOT_DEV=xvda +fi + SWAP_SIZE=$(( 1024 * 1024 * 2 )) # sectors, 1GB -if [ `cat /sys/block/xvda/ro` = 1 ] ; then +if [ `cat /sys/class/block/$ROOT_DEV/ro` = 1 ] ; then log_begin "Qubes: Doing COW setup for AppVM..." while ! [ -e /dev/xvdc ]; do sleep 0.1; done - VOLATILE_SIZE=$(cat /sys/block/xvdc/size) # sectors - ROOT_SIZE=$(cat /sys/block/xvda/size) # sectors + VOLATILE_SIZE=$(cat /sys/class/block/xvdc/size) # sectors + ROOT_SIZE=$(cat /sys/class/block/$ROOT_DEV/size) # sectors if [ $VOLATILE_SIZE -lt $SWAP_SIZE ]; then die "volatile.img smaller than 1GB, cannot continue" fi @@ -78,7 +85,7 @@ EOF mkswap /dev/xvdc1 while ! [ -e /dev/xvdc2 ]; do sleep 0.1; done - echo "0 `cat /sys/block/xvda/size` snapshot /dev/xvda /dev/xvdc2 N 16" | \ + echo "0 `cat /sys/class/block/$ROOT_DEV/size` snapshot /dev/$ROOT_DEV /dev/xvdc2 N 16" | \ dmsetup --noudevsync create dmroot || die "Qubes: FATAL: cannot create dmroot!" log_end else @@ -92,8 +99,17 @@ EOF fi while ! [ -e /dev/xvdc1 ]; do sleep 0.1; done mkswap /dev/xvdc1 - echo "0 `cat /sys/block/xvda/size` linear /dev/xvda 0" | \ - dmsetup --noudevsync create dmroot || die "Qubes: FATAL: cannot create dmroot!" + if [ "$ROOT_DEV" = "xvda1" ]; then + # grub2-probe for some reason can't parse dm-linear pointing at + # partition device directly, but can when it points at it indirectly + # through base device and offset; lets make life easier for users of + # grub2-install + DM_ROOT="/dev/xvda $(cat /sys/class/block/$ROOT_DEV/start)" + else + DM_ROOT="/dev/$ROOT_DEV 0" + fi + echo "0 `cat /sys/class/block/$ROOT_DEV/size` linear $DM_ROOT" | \ + dmsetup create dmroot || { echo "Qubes: FATAL: cannot create dmroot!"; exit 1; } log_end fi dmsetup mknodes dmroot diff --git a/dracut/simple/init.sh b/dracut/simple/init.sh index e61467d..58e989f 100644 --- a/dracut/simple/init.sh +++ b/dracut/simple/init.sh @@ -20,14 +20,21 @@ die() { echo "Waiting for /dev/xvda* devices..." while ! [ -e /dev/xvda ]; do sleep 0.1; done +# prefer first partition if exists +if [ -b /dev/xvda1 ]; then + ROOT_DEV=xvda1 +else + ROOT_DEV=xvda +fi + SWAP_SIZE=$(( 1024 * 1024 * 2 )) # sectors, 1GB -if [ `cat /sys/block/xvda/ro` = 1 ] ; then +if [ `cat /sys/class/block/$ROOT_DEV/ro` = 1 ] ; then echo "Qubes: Doing COW setup for AppVM..." while ! [ -e /dev/xvdc ]; do sleep 0.1; done - VOLATILE_SIZE=$(cat /sys/block/xvdc/size) # sectors - ROOT_SIZE=$(cat /sys/block/xvda/size) # sectors + VOLATILE_SIZE=$(cat /sys/class/block/xvdc/size) # sectors + ROOT_SIZE=$(cat /sys/class/block/$ROOT_DEV/size) # sectors if [ $VOLATILE_SIZE -lt $SWAP_SIZE ]; then die "volatile.img smaller than 1GB, cannot continue" fi @@ -43,7 +50,7 @@ EOF mkswap /dev/xvdc1 while ! [ -e /dev/xvdc2 ]; do sleep 0.1; done - echo "0 `cat /sys/block/xvda/size` snapshot /dev/xvda /dev/xvdc2 N 16" | \ + echo "0 `cat /sys/class/block/$ROOT_DEV/size` snapshot /dev/$ROOT_DEV /dev/xvdc2 N 16" | \ dmsetup create dmroot || { echo "Qubes: FATAL: cannot create dmroot!"; exit 1; } echo Qubes: done. else @@ -57,7 +64,16 @@ EOF fi while ! [ -e /dev/xvdc1 ]; do sleep 0.1; done mkswap /dev/xvdc1 - echo "0 `cat /sys/block/xvda/size` linear /dev/xvda 0" | \ + if [ "$ROOT_DEV" = "xvda1" ]; then + # grub2-probe for some reason can't parse dm-linear pointing at + # partition device directly, but can when it points at it indirectly + # through base device and offset; lets make life easier for users of + # grub2-install + DM_ROOT="/dev/xvda $(cat /sys/class/block/$ROOT_DEV/start)" + else + DM_ROOT="/dev/$ROOT_DEV 0" + fi + echo "0 `cat /sys/class/block/$ROOT_DEV/size` linear $DM_ROOT" | \ dmsetup create dmroot || { echo "Qubes: FATAL: cannot create dmroot!"; exit 1; } echo Qubes: done. fi