diff --git a/mender-convert.sh b/mender-convert.sh index d4ae450..5af70da 100755 --- a/mender-convert.sh +++ b/mender-convert.sh @@ -34,7 +34,7 @@ Expert commands: Options: [-r|--raw-disk-image | -m|--mender-disk-image | -s|--data-part-size-mb | -d|--device-type | -p|--rootfs-partition-id | -i|--demo-host-ip | - -c| --server-cert | -u| --server-url | -t|--tenant-token | + -c|--server-cert | -u|--server-url | -t|--tenant-token | -g|--mender-client -b|--bootloader-toolchain | -a|--artifact-name | -k|--keep] @@ -45,8 +45,8 @@ Options: [-r|--raw-disk-image | -m|--mender-disk-image | -s|--data-part-size-mb data-part-size-mb - data partition size in MB; default value 128MB device-type - target device identification used to build Mender image - rootfs-partition-id - selects root filesystem (rootfs_a|rootfs_b) as the - source filesystem for an artifact + rootfs-partition-id - selects root filesystem (primary|secondary) + as the source filesystem for an artifact demo-host-ip - server demo ip used for testing purposes server-cert - server certificate file server-url - production server url @@ -75,7 +75,10 @@ Examples: --demo-host-ip 192.168.10.2 --keep - Output: ready to use Mender image with Mender client and bootloader installed + Output: + - Mender image: ready to use image with client and bootloader installed + - Mender artifact: update file based on the already built Mender image + - Mender root filesystem: EXT4 image used to produce the Mender artifact To create Mender artifact file from Mender image: @@ -83,7 +86,7 @@ Examples: --mender-disk-image --device-type --artifact-name release-1_1.5.0 - --rootfs-partition-id rootfs_a + --rootfs-partition-id Note: artifact name format is: release-_ @@ -167,7 +170,7 @@ demo_host_ip= # Mender hosted token. tenant_token= -declare -a rootfs_partition_ids=("rootfs_a" "rootfs_b") +declare -a rootfs_partition_ids=("primary" "secondary") declare -a mender_disk_mappings declare -a raw_disk_mappings #Supported devices @@ -493,8 +496,8 @@ do_mender_disk_image_to_artifact() { fi if [ -z "${rootfs_partition_id}" ]; then - echo "Rootfs partition id not set - rootfs_a will be used by default." - rootfs_partition_id="rootfs_a" + echo "Rootfs partition id not set - 'primary' will be used by default." + rootfs_partition_id="primary" fi local supported=$(echo ${supported_devices[@]} | grep -o $device_type | wc -w) @@ -526,20 +529,9 @@ do_mender_disk_image_to_artifact() { create_device_maps $mender_disk_image mender_disk_mappings mount_mender_disk ${mender_disk_mappings[@]} - if [[ $rootfs_partition_id == "rootfs_a" ]]; then - prootfs_size=$rootfs_a_size - rootfs_path=$sdimg_primary_dir - elif [[ $rootfs_partition_id == "rootfs_b" ]]; then - prootfs_size=$rootfs_b_size - rootfs_path=$sdimg_secondary_dir - fi - # Find .sdimg file's dedicated device type. mender_device_type=$( cat $sdimg_data_dir/mender/device_type | sed 's/[^=].*=//' ) - # Set 'artifact name' as passed in the command line. - sudo sed -i '/^artifact/s/=.*$/='${artifact_name}'/' "$rootfs_path/etc/mender/artifact_info" - if [ "$mender_device_type" != "$device_type" ]; then echo "Error: device types of Mender artifact & Mender not matching. Aborting." ret=1 @@ -551,31 +543,61 @@ do_mender_disk_image_to_artifact() { fi if [ $ret -eq 0 ]; then - local rootfs_file=${output_dir}/rootfs.ext4 - - echo "Creating a ext4 file-system image from modified root file-system" - dd if=/dev/zero of=$rootfs_file seek=${prootfs_size} count=0 bs=512 status=none - - sudo mkfs.ext4 -FF $rootfs_file -d $rootfs_path - - fsck.ext4 -fp $rootfs_file - - mender_artifact=${output_dir}/${device_type}_${artifact_name}.mender - echo "Writing Mender artifact to: ${mender_artifact}" - - #Create Mender artifact - mender-artifact write rootfs-image \ - --update ${rootfs_file} \ - --output-path ${mender_artifact} \ - --artifact-name ${artifact_name} \ - --device-type ${device_type} - - ret=$? - [[ $ret -eq 0 ]] && \ - { echo "Writing Mender artifact to ${mender_artifact} succeeded."; } || \ - { echo "Writing Mender artifact to ${mender_artifact} failed."; } - - rm $rootfs_file + if [[ $rootfs_partition_id == "primary" ]]; then + prootfs_start=$rootfs_a_start + prootfs_size=$rootfs_a_size + rootfs_path=$sdimg_primary_dir + elif [[ $rootfs_partition_id == "secondary" ]]; then + prootfs_start=$rootfs_b_start + prootfs_size=$rootfs_b_size + rootfs_path=$sdimg_secondary_dir + fi + + mender_disk_basename=$(basename -- "$mender_disk_image") + mender_disk_filename="${mender_disk_basename%.*}" + mender_rootfs_basename=${mender_disk_filename}.ext4 + mender_rootfs_image=${output_dir}/$mender_rootfs_basename + + # Extract root filesystem ext4 image to use it to generate Mender artifact. + # Ext4 disk image will be also verified in acceptance tests. + extract_file_from_image $mender_disk_image $prootfs_start \ + $prootfs_size $mender_rootfs_basename + + fsck.ext4 -fp $mender_rootfs_image + + # Find first available loopback device. + loopdevice=($(sudo losetup -f || ret=$?)) + + if [ $ret -ne 0 ]; then + echo "Error: cannot find an unused loop device. Aborting." + else + sudo losetup $loopdevice ${mender_rootfs_image} + rootfs_mountpoint=${output_dir}/mnt/${rootfs_partition_id} + mkdir -p ${rootfs_mountpoint} + sudo mount $loopdevice ${rootfs_mountpoint} + + # Set 'artifact name' as passed in the command line. + sudo sed -i '/^artifact/s/=.*$/='${artifact_name}'/' "${rootfs_mountpoint}/etc/mender/artifact_info" + + mender_artifact=${output_dir}/${mender_disk_filename}_${artifact_name}.mender + echo "Writing Mender artifact to: ${mender_artifact}" + + #Create Mender artifact + mender-artifact write rootfs-image \ + --update ${mender_rootfs_image} \ + --output-path ${mender_artifact} \ + --artifact-name ${artifact_name} \ + --device-type ${device_type} + + ret=$? + [[ $ret -eq 0 ]] && \ + { echo "Writing Mender artifact to ${mender_artifact} succeeded."; } || \ + { echo "Writing Mender artifact to ${mender_artifact} failed."; } + + sudo umount -l ${rootfs_mountpoint} + sudo losetup -d $loopdevice + rm -rf ${output_dir}/mnt + fi fi # Clean and detach. @@ -602,6 +624,9 @@ do_from_raw_disk_image() { do_install_bootloader_to_mender_disk_image || rc=$? [[ $rc -ne 0 ]] && { return 1; } + do_mender_disk_image_to_artifact || rc=$? + [[ $rc -ne 0 ]] && { return 1; } + return 0 }