diff options
| -rw-r--r-- | classes/sdcard_image-rpi.bbclass | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/classes/sdcard_image-rpi.bbclass b/classes/sdcard_image-rpi.bbclass new file mode 100644 index 0000000..1ac3e84 --- /dev/null +++ b/classes/sdcard_image-rpi.bbclass | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | inherit image | ||
| 2 | |||
| 3 | # Add the fstypes we need | ||
| 4 | IMAGE_FSTYPES_append = " tar.bz2 rpi-sdimg" | ||
| 5 | |||
| 6 | # Ensure required utilities are present | ||
| 7 | IMAGE_DEPENDS_rpi-sdimg = "genext2fs-native e2fsprogs-native bcm2835-bootfiles bcm2835-kernel-image" | ||
| 8 | |||
| 9 | # Register this as an avalable type of image. | ||
| 10 | IMAGE_TYPES_append = " rpi-sdimg" | ||
| 11 | |||
| 12 | # Change this to match your host distro | ||
| 13 | LOSETUP ?= "/sbin/losetup" | ||
| 14 | |||
| 15 | # Since these need to go in /etc/fstab we can hardcode them | ||
| 16 | # Since the vars are weakly assigned, you can override them from your local.conf | ||
| 17 | LOOPDEV ?= "/dev/loop1" | ||
| 18 | LOOPDEV_BOOT ?= "/dev/loop2" | ||
| 19 | LOOPDEV_FS ?= "/dev/loop3" | ||
| 20 | |||
| 21 | # Default to 4GiB images | ||
| 22 | SDIMG_SIZE ?= "444" | ||
| 23 | |||
| 24 | # FS type for rootfs | ||
| 25 | ROOTFSTYPE ?= "ext4" | ||
| 26 | |||
| 27 | BOOTPARTNAME ?= "${MACHINE}" | ||
| 28 | |||
| 29 | IMAGEDATESTAMP = "${@time.strftime('%Y.%m.%d',time.gmtime())}" | ||
| 30 | |||
| 31 | # Additional files and/or directories to be copied into the vfat partition from the IMAGE_ROOTFS. | ||
| 32 | FATPAYLOAD ?= "" | ||
| 33 | |||
| 34 | IMAGE_CMD_rpi-sdimg () { | ||
| 35 | SDIMG=${WORKDIR}/sd.img | ||
| 36 | |||
| 37 | # sanity check fstab entry for boot partition mounting | ||
| 38 | if [ "x$(cat /etc/fstab | grep ${LOOPDEV_BOOT} | grep ${WORKDIR}/tmp-mnt-boot | grep user || true)" = "x" ]; then | ||
| 39 | echo "/etc/fstab entries need to be created with the user flag for the loop devices like:" | ||
| 40 | echo "${LOOPDEV_BOOT} ${WORKDIR}/tmp-mnt-boot vfat user 0 0" | ||
| 41 | false | ||
| 42 | fi | ||
| 43 | |||
| 44 | # cleanup loops | ||
| 45 | for loop in ${LOOPDEV} ${LOOPDEV_BOOT} ${LOOPDEV_FS} ; do | ||
| 46 | ${LOSETUP} -d $loop || true | ||
| 47 | done | ||
| 48 | |||
| 49 | # If an SD image is already present, reuse and reformat it | ||
| 50 | if [ ! -e ${SDIMG} ] ; then | ||
| 51 | dd if=/dev/zero of=${SDIMG} bs=$(echo '255 * 63 * 512' | bc) count=${SDIMG_SIZE} | ||
| 52 | fi | ||
| 53 | |||
| 54 | ${LOSETUP} ${LOOPDEV} ${SDIMG} | ||
| 55 | |||
| 56 | # Create partition table | ||
| 57 | dd if=/dev/zero of=${LOOPDEV} bs=1024 count=1024 | ||
| 58 | SIZE=$(/sbin/fdisk -l ${LOOPDEV} | grep Disk | grep bytes | awk '{print $5}') | ||
| 59 | CYLINDERS=$(echo $SIZE/255/63/512 | bc) | ||
| 60 | { | ||
| 61 | echo ,9,0x0C,* | ||
| 62 | echo ,,,- | ||
| 63 | } | /sbin/sfdisk -D -H 255 -S 63 -C ${CYLINDERS} ${LOOPDEV} | ||
| 64 | |||
| 65 | # Prepare loop devices for boot and filesystem partitions | ||
| 66 | BOOT_OFFSET=32256 | ||
| 67 | FS_OFFSET_SECT=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /"|cut -d " " -f 2) | ||
| 68 | FS_OFFSET=$(echo "$FS_OFFSET_SECT * 512" | bc) | ||
| 69 | FS_SIZE_BLOCKS=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /g" \ | ||
| 70 | |cut -d " " -f 4 | cut -d "+" -f 1) | ||
| 71 | |||
| 72 | LOOPDEV_BLOCKS=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep FAT | perl -p -i -e "s/\s+/ /g"|cut -d " " -f 5) | ||
| 73 | LOOPDEV_BYTES=$(echo "$LOOPDEV_BLOCKS * 1024" | bc) | ||
| 74 | |||
| 75 | ${LOSETUP} -d ${LOOPDEV} | ||
| 76 | |||
| 77 | ${LOSETUP} ${LOOPDEV_BOOT} ${SDIMG} -o ${BOOT_OFFSET} | ||
| 78 | |||
| 79 | /sbin/mkfs.vfat ${LOOPDEV_BOOT} -n ${BOOTPARTNAME} $LOOPDEV_BLOCKS | ||
| 80 | |||
| 81 | # Prepare boot partion. First mount the boot partition, and copy the bootloader and supporting files. | ||
| 82 | |||
| 83 | mkdir -p ${WORKDIR}/tmp-mnt-boot | ||
| 84 | mount $LOOPDEV_BOOT ${WORKDIR}/tmp-mnt-boot | ||
| 85 | |||
| 86 | echo "Copying bootloader and prepended kernel.img into the boot partition" | ||
| 87 | cp -v ${DEPLOY_DIR_IMAGE}/bcm2835-bootfiles/* ${WORKDIR}/tmp-mnt-boot || true | ||
| 88 | |||
| 89 | if [ -n ${FATPAYLOAD} ] ; then | ||
| 90 | echo "Copying payload into VFAT" | ||
| 91 | for entry in ${FATPAYLOAD} ; do | ||
| 92 | # add the || true to stop aborting on vfat issues like not supporting .~lock files | ||
| 93 | cp -av ${IMAGE_ROOTFS}$entry ${WORKDIR}/tmp-mnt-boot || true | ||
| 94 | done | ||
| 95 | fi | ||
| 96 | |||
| 97 | echo "${IMAGE_NAME}-${IMAGEDATESTAMP}" > ${IMAGE_ROOTFS}/etc/image-version-info | ||
| 98 | |||
| 99 | cp -v ${IMAGE_ROOTFS}/etc/image-version-info ${WORKDIR}/tmp-mnt-boot || true | ||
| 100 | |||
| 101 | # Cleanup VFAT mount | ||
| 102 | echo "Cleaning up VFAT mount" | ||
| 103 | umount ${WORKDIR}/tmp-mnt-boot | ||
| 104 | ${LOSETUP} -d ${LOOPDEV_BOOT} || true | ||
| 105 | |||
| 106 | # Prepare rootfs parition | ||
| 107 | echo "Creating rootfs loopback" | ||
| 108 | ${LOSETUP} ${LOOPDEV_FS} ${SDIMG} -o ${FS_OFFSET} | ||
| 109 | |||
| 110 | FS_NUM_INODES=$(echo $FS_SIZE_BLOCKS / 4 | bc) | ||
| 111 | |||
| 112 | case "${ROOTFSTYPE}" in | ||
| 113 | ext3) | ||
| 114 | genext2fs -z -N $FS_NUM_INODES -b $FS_SIZE_BLOCKS -d ${IMAGE_ROOTFS} ${LOOPDEV_FS} | ||
| 115 | tune2fs -L ${IMAGE_NAME} -j ${LOOPDEV_FS} | ||
| 116 | ;; | ||
| 117 | ext4) | ||
| 118 | genext2fs -z -N $FS_NUM_INODES -b $FS_SIZE_BLOCKS -d ${IMAGE_ROOTFS} ${LOOPDEV_FS} | ||
| 119 | tune2fs -L ${IMAGE_NAME} -j -O extents,uninit_bg,dir_index ${LOOPDEV_FS} | ||
| 120 | ;; | ||
| 121 | *) | ||
| 122 | echo "Please set ROOTFSTYPE to something supported" | ||
| 123 | exit 1 | ||
| 124 | ;; | ||
| 125 | esac | ||
| 126 | |||
| 127 | ${LOSETUP} -d ${LOOPDEV_FS} || true | ||
| 128 | |||
| 129 | gzip -c ${WORKDIR}/sd.img > ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}-${IMAGEDATESTAMP}.img.gz | ||
| 130 | rm -f ${WORKDIR}/sd.img | ||
| 131 | } | ||
