186 lines
4.5 KiB
Plaintext
Raw Normal View History

2016-02-10 23:51:54 -08:00
#!/bin/bash -ex
IMAGE_STR="# CLOUD_IMG: This file was created/modified by the Cloud Image build process"
FS_LABEL="cloudimg-rootfs"
. config/binary
2017-11-23 20:27:16 +01:00
. config/functions
2016-02-06 02:05:14 -08:00
BOOTPART_START=
BOOTPART_END=
BOOT_MOUNTPOINT=
2016-02-06 02:42:10 -08:00
ROOTPART_START=1
2016-02-06 02:05:14 -08:00
my_d=$(dirname $(readlink -f ${0}))
2016-02-10 23:51:54 -08:00
case $ARCH:$SUBARCH in
ppc64el:*|powerpc:*)
echo "POWER disk images are handled separately"
exit 0
;;
2018-03-06 12:58:38 +00:00
amd64|arm64|armhf)
echo "We only create EFI images for $ARCH."
exit 0
;;
2016-02-10 23:51:54 -08:00
armhf:raspi2)
# matches the size of the snappy image
IMAGE_SIZE=$((4*1000*1000*1000))
BOOTPART_START=8192s
BOOTPART_END=138M
BOOT_MOUNTPOINT=/boot/firmware
;;
*)
;;
esac
create_empty_partition_table() {
parted "$1" --script -- mklabel msdos
}
create_empty_partition() {
local disk="$1"
local part="$2"
local start="$3"
local end="$4"
2016-02-12 09:49:17 -08:00
local type="$5"
local bootable="$6"
parted_prefix="parted $disk --script --"
2016-02-12 09:49:17 -08:00
${parted_prefix} mkpart primary "$type" "$start" "$end"
if [ -n "$bootable" ]; then
${parted_prefix} set "$part" B
fi
${parted_prefix} print
${parted_prefix} align-check opt "$part"
}
disk_image=binary/boot/disk.ext4
create_empty_disk_image "${disk_image}"
create_empty_partition_table "${disk_image}"
ROOTPART=1
ROOT_BOOTABLE=1
if [ -n "$BOOTPART_START" ]; then
ROOTPART=2
ROOTPART_START="$BOOTPART_END"
ROOT_BOOTABLE=
2016-02-12 09:49:17 -08:00
create_empty_partition "$disk_image" 1 "$BOOTPART_START" "$BOOTPART_END" fat32 1
fi
2016-02-12 09:49:17 -08:00
create_empty_partition "${disk_image}" "$ROOTPART" "$ROOTPART_START" -1 ext2 "$ROOT_BOOTABLE"
mount_image "${disk_image}" "$ROOTPART"
partuuid=$(blkid -s PARTUUID -o value "$rootfs_dev_mapper")
# Copy the chroot in to the disk
make_ext4_partition "${rootfs_dev_mapper}"
mkdir mountpoint
mount "${rootfs_dev_mapper}" mountpoint
if [ -n "$BOOT_MOUNTPOINT" ]; then
boot_dev_mapper="${rootfs_dev_mapper%%[0-9]}1"
# assume fat32 for now
mkfs.vfat -n system-boot "$boot_dev_mapper"
mkdir -p "mountpoint/$BOOT_MOUNTPOINT"
mount "$boot_dev_mapper" "mountpoint/$BOOT_MOUNTPOINT"
fi
cp -a chroot/* mountpoint/
setup_mountpoint mountpoint
2016-02-10 23:51:54 -08:00
case $ARCH:$SUBARCH in
armhf:raspi2)
chroot mountpoint flash-kernel \
--machine "Raspberry Pi 2 Model B"
# not the best place for this, but neither flash-kernel nor
# u-boot have provisions for installing u-boot via maintainer
# script
${my_d}/raspi2/mkknlimg --dtok \
mountpoint/usr/lib/u-boot/rpi_2/u-boot.bin \
mountpoint/boot/firmware/uboot.bin
2016-02-10 23:51:54 -08:00
;;
*) ;;
esac
2016-02-06 00:37:20 -08:00
case $ARCH in
amd64|i386) should_install_grub=1;;
*) should_install_grub=0;;
esac
if [ "${should_install_grub}" -eq 1 ]; then
echo "(hd0) ${loop_device}" > mountpoint/tmp/device.map
chroot mountpoint grub-install ${loop_device}
chroot mountpoint grub-bios-setup \
--boot-image=i386-pc/boot.img \
--core-image=i386-pc/core.img \
--skip-fs-probe \
--device-map=/tmp/device.map \
${loop_device}
rm mountpoint/tmp/device.map
if [ -n "$partuuid" ]; then
echo "partuuid found for root device; forcing it in Grub"
mkdir -p mountpoint/etc/default/grub.d
echo "GRUB_FORCE_PARTUUID=$partuuid" >> mountpoint/etc/default/grub.d/40-force-partuuid.cfg
divert_grub mountpoint
chroot mountpoint update-grub
undivert_grub mountpoint
fi
fi
2016-02-06 00:37:20 -08:00
if [ "$ARCH" = "s390x" ]; then
2016-01-13 10:19:09 +00:00
# Do ZIPL install bits
chroot mountpoint apt-get -qqy install s390-tools sysconfig-hardware
2016-01-13 10:19:09 +00:00
# Write out cloudy zipl.conf for future kernel updates
cat << EOF > mountpoint/etc/zipl.conf
# This has been modified by the cloud image build process
[defaultboot]
default=ubuntu
[ubuntu]
target = /boot
image = /boot/vmlinuz
parameters = root=LABEL=cloudimg-rootfs
EOF
ZIPL_EXTRA_PARAMS=
if [ -e mountpoint/boot/initrd.img-* ]; then
# Kernel initramfs hooks end up creating a copy
# rather than a symlink FIXME
pushd mountpoint/boot
ln -sf initrd.img-* initrd.img
popd
echo "ramdisk = /boot/initrd.img" >> mountpoint/etc/zipl.conf
2017-11-14 19:04:21 +01:00
ZIPL_EXTRA_PARAMS=--ramdisk=/boot/initrd.img
fi
2016-01-13 10:19:09 +00:00
# Create bootmap file
chroot mountpoint /sbin/zipl -V \
--image=/boot/vmlinuz \
--parameters='root=LABEL=cloudimg-rootfs' \
--target=/boot/ \
--targetbase=$loop_device \
--targettype=SCSI \
--targetblocksize=512 \
--targetoffset=2048 \
$ZIPL_EXTRA_PARAMS
2016-01-13 10:19:09 +00:00
fi
if [ -n "$BOOT_MOUNTPOINT" ]; then
umount "mountpoint/$BOOT_MOUNTPOINT"
fi
umount_partition mountpoint
rmdir mountpoint
clean_loops
trap - EXIT