2013-05-29 17:56:12 +04:00
#!/bin/bash -ex
# usage:
# tar2fs chroot.tar image.raw [size_in_bytes [fstype]]
. shell-error
2015-05-31 18:08:19 +03:00
export LANG=C
2013-05-29 17:56:12 +04:00
if [ $# -lt 2 ]; then
fatal "error: tar2fs needs at least two arguments"
fi
# this needs env_keep sudo setup to actually work
if [ -n "$GLOBAL_BUILDDIR" ]; then
WORKDIR="$GLOBAL_BUILDDIR/vmroot"
else
WORKDIR="$(mktemp --tmpdir -d vmroot-XXXXX)"
fi
[ -n "$WORKDIR" ] || fatal "couldn't come up with suitable WORKDIR"
[ -n "$GLOBAL_DEBUG" ] || message "WORKDIR: $WORKDIR"
MB=1048576 # a parted's "megabyte" in bytes is *broken*
2013-07-09 12:34:21 +04:00
2013-05-29 17:56:12 +04:00
SIZE_FACTOR=2 # multiply the sizes found by this value
2013-07-09 12:34:21 +04:00
BOOT_SIZE_FACTOR=2 # multiply /boot size by this value additionally
2013-05-29 17:56:12 +04:00
CUR_BOUNDARY=0 # align first partition at 1MB for performance (+1)
INITRD_MODULES=
BOOTFSTYPE=
BOOTPART=
2017-08-02 17:40:28 +03:00
ARCH="$(arch)" # NB: sudo => no GLOBAL_ will do either; mind qemu-*
case "$ARCH" in
e2k)
BOOTFSTYPE="ext2" # firmware knows it
BLOCKDEV="/dev/sda" # ...hopefully...
BOOTPART="1"
ROOTPART="2"
;;
2019-05-12 09:22:54 +03:00
*)
2018-12-10 13:06:56 +03:00
ROOTPART="1"
BLOCKDEV="/dev/sda"
;;
2013-05-29 17:56:12 +04:00
esac
# figure out the part taken by /boot in the given tarball
boot_size() {
if [ -n "$BOOTPART" ]; then
tar tvf "$1" \
| awk ' \
BEGIN { sum=0 }
/^-.* \.\/boot\// { sum=sum+$3 }
END { print sum }'
else
echo "0"
fi
}
# parted wrapper for convenience
parting() { parted "$LOOPDEV" --align optimal --script -- "$@"; }
# unfortunately parted is insane enough to lump alignment controls
# into unit controls so creating adjacent partitions sized in MiB
# is not as straightforward as it might be... thus "+1" padding;
# see also http://www.gnu.org/software/parted/manual/parted.html#unit
mkpart() {
# a bit different start/end semantics to handle end of device too
local start="$(($CUR_BOUNDARY + 1))" # yes, we lose a megabyte
if [ -n "$1" ]; then
CUR_BOUNDARY="$(($start + $1))"
local end="$CUR_BOUNDARY"MiB
else
local end="-1s" # last sector of the image
fi
parting mkpart primary ext2 "$start"MiB "$end"
}
# a tarball containing chroot with a kernel
TAR="$1"
[ -s "$TAR" ] || fatal "source tarball doesn't really exist"
# a path to the image to be generated
IMG="$2"
[ -d "$(dirname "$IMG")" ] || fatal "target directory doesn't exist"
# 0 means auto; if a value is specified it holds (no /boot subtracted)
ROOTSIZE="$3"
[ -n "$ROOTSIZE" -a "$ROOTSIZE" != 0 ] || unset ROOTSIZE
2013-07-09 12:34:21 +04:00
# image size in bytes
2013-05-29 17:56:12 +04:00
TARSIZE="$(stat -Lc %s "$TAR")"
# /boot size in that tarball
BOOTSIZE="$(boot_size "$TAR")"
DEFSIZE="$(($SIZE_FACTOR * ($TARSIZE - $BOOTSIZE)))" # (exact sizes)
2013-07-09 12:34:21 +04:00
ROOTSIZE="$((${ROOTSIZE:-$DEFSIZE} + $MB - 1))" # for ceil rounding to MB
2013-05-29 17:56:12 +04:00
# image and /boot sizes in megabytes
ROOTSIZEM="$(($ROOTSIZE / $MB))"
2013-07-09 12:34:21 +04:00
BOOTSIZEM="$((($SIZE_FACTOR * $BOOT_SIZE_FACTOR * $BOOTSIZE + $MB - 1) / $MB))"
2013-05-29 17:56:12 +04:00
# tested to work: ext[234], jfs
# NB: xfs doesn't have a spare sector for the bootloader
ROOTFSTYPE="${4:-ext4}"
# single root partition hardwired so far,
# add another image for home/data/swap if needed
ROOTDEV="$BLOCKDEV$ROOTPART"
# last preparations...
MKFS="mkfs.$ROOTFSTYPE ${BOOTFSTYPE:+mkfs.$BOOTFSTYPE}"
for i in losetup sfdisk parted kpartx $MKFS; do
if ! type -t "$i" >&/dev/null; then
fatal "$i required but not found in host system"
fi
done
LOOPDEV="$(losetup --find)" # would be sad about non-binary megabytes too
ROOTFS="$WORKDIR/chroot"
BOOTFS=
if [ -n "$BOOTPART" ]; then
BOOTFS="$ROOTFS/boot"
fi
exit_handler() {
rc=$?
2018-08-02 21:50:16 +03:00
cd /
2013-05-29 17:56:12 +04:00
if [ -n "$ROOTFS" ]; then
umount ${BOOTFS:+"$BOOTFS"} "$ROOTFS"{/dev,/proc,/sys,}
if [ -n "$LOOPDEV" ]; then
2017-08-02 22:09:27 +03:00
kpartx -d -s "$LOOPDEV" || {
sleep 10
kpartx -d -s -v "$LOOPDEV"
}
2013-05-29 17:56:12 +04:00
losetup --detach "$LOOPDEV"
fi
rm -r -- "$ROOTFS"
rmdir -- "$WORKDIR"
fi
exit $rc
}
# handle -e in shebang as well
trap exit_handler EXIT ERR
# prepare disk image and a filesystem inside it
rm -f -- "$IMG"
OFFSET="$(($CUR_BOUNDARY + $BOOTSIZEM + $ROOTSIZEM - 1))"
dd if=/dev/zero of="$IMG" conv=notrunc bs=$MB count=1 seek="$OFFSET"
losetup "$LOOPDEV" "$IMG"
parting mklabel msdos
if [ -n "$BOOTPART" ]; then
BOOTDEV="$BLOCKDEV$BOOTPART"
mkpart "$BOOTSIZEM"
fi
# not ROOTSIZEM but "the rest"; somewhat non-trivial arithmetics lurk in parted
mkpart
2013-07-15 21:22:47 +04:00
kpartx -a -s "$LOOPDEV"
2013-05-29 17:56:12 +04:00
LOOPROOT="/dev/mapper/$(basename "$LOOPDEV")p$ROOTPART"
mkfs."$ROOTFSTYPE" "$LOOPROOT"
if [ -n "$BOOTPART" ]; then
LOOPBOOT="/dev/mapper/$(basename "$LOOPDEV")p$BOOTPART"
mkfs."$BOOTFSTYPE" "$LOOPBOOT"
fi
2014-03-20 19:46:53 +04:00
ROOTUUID="$(blkid -s UUID -o value -c /dev/null "$LOOPROOT")"
if [ -n "$ROOTUUID" ]; then
ROOTDEV="UUID=$ROOTUUID"
else
ROOTDEV="$LOOPROOT"
fi
if [ -n "$BOOTPART" ]; then
BOOTUUID="$(blkid -s UUID -o value -c /dev/null "$LOOPBOOT")"
if [ -n "$ROOTUUID" ]; then
BOOTDEV="UUID=$BOOTUUID"
fi
fi
2013-05-29 17:56:12 +04:00
# mount and populate it
mkdir -pm755 "$ROOTFS"
mount "$LOOPROOT" "$ROOTFS"
if [ -n "$BOOTPART" ]; then
mkdir -pm700 "$BOOTFS"
mount "$LOOPBOOT" "$BOOTFS"
fi
tar -C "$ROOTFS" --numeric-owner -xf "$TAR"
for i in /dev /proc /sys; do mount --bind "$i" "$ROOTFS$i"; done
# loop device so lilo could work...
2014-03-20 19:46:53 +04:00
echo "$ROOTDEV / $ROOTFSTYPE relatime 1 1" >> "$ROOTFS/etc/fstab"
2013-05-29 17:56:12 +04:00
# target device at once
if [ -n "$BOOTPART" ]; then
echo "$BOOTDEV /boot $BOOTFSTYPE defaults 1 2" >> "$ROOTFS/etc/fstab"
fi
2019-01-09 14:12:47 +03:00
# Query ARCH in chroot and redefine arch-dependent variable
ARCH="$(chroot "$ROOTFS" rpm --eval '%_host_cpu')"
if [[ $ARCH = *86* ]]
then # NB: different storage modules might be needed for non-kvm
INITRD_MODULES="sd_mod ata_piix ahci virtio-scsi virtio-blk"
else
INITRD_MODULES=
fi
2014-03-03 16:18:01 +04:00
echo "MODULES_PRELOAD += $INITRD_MODULES $ROOTFSTYPE" >> "$ROOTFS/etc/initrd.mk"
2017-08-02 17:40:28 +03:00
case "$ARCH" in
*86*)
echo "FEATURES += qemu" >> "$ROOTFS/etc/initrd.mk"
;;
e2k)
echo "FEATURES += usb" >> "$ROOTFS/etc/initrd.mk"
;;
esac
2013-05-29 17:56:12 +04:00
2017-08-02 15:44:57 +03:00
# NB: don't stick BOOTFS here, it has slightly different semantics
pushd $ROOTFS/boot
2018-04-23 18:02:08 +03:00
2018-10-18 19:45:23 +03:00
# 4.9.76-elbrus-def-alt1.11.1 -> def
get_label() { echo "${1# *}" | sed -r 's,.*elbrus-([0-9a-z]+)-.*$,\1,'; }
2018-04-23 18:02:08 +03:00
KVERSIONS=
2017-08-02 15:44:57 +03:00
if [ -s .origver ]; then
2018-04-10 14:01:46 +03:00
while read kver; do
chroot "$ROOTFS" make-initrd -k "$kver"
2018-04-23 18:02:08 +03:00
KVERSIONS="$KVERSIONS $kver"
2018-04-10 14:01:46 +03:00
done < .origver
2017-08-02 15:44:57 +03:00
fi
2018-04-23 18:02:08 +03:00
[ -n "$KVERSIONS" ] || fatal "unable to deduce kernel version"
2017-08-02 15:44:57 +03:00
rm -f .origver
2013-05-29 17:56:12 +04:00
# ...target device too
sed -i "s,$LOOPROOT,$ROOTDEV," "$ROOTFS/etc/fstab"
2018-10-18 19:45:23 +03:00
echo "** KVERSIONS=[$KVERSIONS]" >&2
if [ "`echo $KVERSIONS | wc -w`" = 1 ]; then # 2+ labels
echo -e "default=`get_label $KVERSIONS`\n" >> boot.conf
fi
2018-04-23 18:02:08 +03:00
# FIXME: relies on particular (current) kernel package naming scheme
for v in $KVERSIONS; do
2018-10-18 19:45:23 +03:00
l="`get_label "$v"`"
2018-04-23 18:02:08 +03:00
cat >> boot.conf <<EOF
label=$l
partition=0
image=/image-$v
initrd=/initrd-$v.img
cmdline=console=ttyS0,115200 console=tty0 consoleblank=0 hardreset root=UUID=$ROOTUUID
2018-10-18 19:45:23 +03:00
2018-04-23 18:02:08 +03:00
EOF
done
echo "** start of boot.conf"
cat boot.conf
echo "** end of boot.conf"
2017-08-02 17:40:28 +03:00
popd
2015-02-02 14:55:36 +03:00
if [ -x "$ROOTFS"/sbin/lilo ]; then
2013-05-29 17:56:12 +04:00
# configure and install bootloader
2015-05-31 18:08:19 +03:00
REGEXP='^.*: ([0-9]+) cylinders, ([0-9]+) heads, ([0-9]+) sectors/track*$'
set -- $(sfdisk -g "$LOOPDEV" | grep -E "$REGEXP" | sed -r "s@$REGEXP@\1 \2 \3@")
2013-05-29 17:56:12 +04:00
LILO_COMMON="lba32
delay=1
vga=0
image=/boot/vmlinuz
initrd=/boot/initrd.img
2015-12-04 13:22:10 +03:00
append=\"root=$ROOTDEV rootdelay=3 console=tty1 console=ttyS0,115200n8\"
2013-05-29 17:56:12 +04:00
label=linux"
cat > "$ROOTFS"/etc/lilo-loop.conf <<-EOF
boot=$LOOPDEV
disk=$LOOPDEV
bios=0x80
cylinders=$1
heads=$2
sectors=$3
partition=$LOOPROOT
start=63
$LILO_COMMON
EOF
chroot "$ROOTFS" lilo -C /etc/lilo-loop.conf
cat > "$ROOTFS"/etc/lilo.conf <<-EOF
boot=$BLOCKDEV
$LILO_COMMON
EOF
2018-11-26 21:19:10 +03:00
elif [ -x "$ROOTFS"/usr/sbin/grub-efi-autoupdate ]; then
chroot "$ROOTFS" grub-mkconfig -o /boot/grub/grub.cfg
chroot "$ROOTFS" grub-install
elif [ -x "$ROOTFS"/usr/sbin/grub-autoupdate ]; then
chroot "$ROOTFS" grub-mkconfig -o /boot/grub/grub.cfg
chroot "$ROOTFS" grub-install --target=i386-pc "$LOOPDEV"
2013-05-29 17:56:12 +04:00
fi
if [ -n "$SUDO_USER" ]; then
2019-02-26 19:01:07 +03:00
chown "$SUDO_USER:$(id -g "$SUDO_USER")" "$IMG" ||:
2013-05-29 17:56:12 +04:00
fi
2019-01-09 14:12:47 +03:00
# maybe qemu interpreter was copied to chroot;
# this is no longer necessary, remove
rm -rf "$ROOTFS"/.host ||: