in Education by
Is it possible to create a complete SD image in linux without having root privileges (that is, no loopback mount)? I'm looking for a way to automate embedded system image creation. The image should include a specific partition structure and partitions formatted to FAT and ext2 populated with files from the build system. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Minimal runnable sfdisk + mke2fs example without sudo In this example, we will create, without sudo or setsuid, an image file that contains two ext2 partitions, each populated with files from a host directory. We will then use sudo losetup just to mount the partitions to test that the Linux kernel can actually read them as explained at: How to mount one partition from an image file that contains multiple partitions on Linux? For more details, see: sfdisk: deals with the partition table: https://superuser.com/questions/332252/how-to-create-and-format-a-partition-using-a-bash-script/1132834#1132834 mke2fs: deals with EXT formatting of partitions: https://superuser.com/questions/605196/how-to-create-ext2-image-without-superuser-rights/1366762#1366762 The example: #!/usr/bin/env bash # Input params. root_dir_1=root1 root_dir_2=root2 partition_file_1=part1.ext2 partition_file_2=part2.ext2 partition_size_1_megs=32 partition_size_2_megs=32 img_file=img.img block_size=512 # Calculated params. mega="$(echo '2^20' | bc)" partition_size_1=$(($partition_size_1_megs * $mega)) partition_size_2=$(($partition_size_2_megs * $mega)) # Create a test directory to convert to ext2. mkdir -p "$root_dir_1" echo content-1 > "${root_dir_1}/file-1" mkdir -p "$root_dir_2" echo content-2 > "${root_dir_2}/file-2" # Create the 2 raw ext2 images. rm -f "$partition_file_1" mke2fs \ -d "$root_dir_1" \ -r 1 \ -N 0 \ -m 5 \ -L '' \ -O ^64bit \ "$partition_file_1" \ "${partition_size_1_megs}M" \ ; rm -f "$partition_file_2" mke2fs \ -d "$root_dir_2" \ -r 1 \ -N 0 \ -m 5 \ -L '' \ -O ^64bit \ "$partition_file_2" \ "${partition_size_2_megs}M" \ ; # Default offset according to part_table_offset=$((2**20)) cur_offset=0 bs=1024 dd if=/dev/zero of="$img_file" bs="$bs" count=$((($part_table_offset + $partition_size_1 + $partition_size_2)/$bs)) skip="$(($cur_offset/$bs))" printf " type=83, size=$(($partition_size_1/$block_size)) type=83, size=$(($partition_size_2/$block_size)) " | sfdisk "$img_file" cur_offset=$(($cur_offset + $part_table_offset)) # TODO: can we prevent this and use mke2fs directly on the image at an offset? # Tried -E offset= but could not get it to work. dd if="$partition_file_1" of="$img_file" bs="$bs" seek="$(($cur_offset/$bs))" cur_offset=$(($cur_offset + $partition_size_1)) rm "$partition_file_1" dd if="$partition_file_2" of="$img_file" bs="$bs" seek="$(($cur_offset/$bs))" cur_offset=$(($cur_offset + $partition_size_2)) rm "$partition_file_2" # Test the ext2 by mounting it with sudo. # sudo is only used for testing, the image is completely ready at this point. # losetup automation functions from: # https://stackoverflow.com/questions/1419489/how-to-mount-one-partition-from-an-image-file-that-contains-multiple-partitions/39675265#39675265 loop-mount-partitions() ( set -e img="$1" dev="$(sudo losetup --show -f -P "$img")" echo "$dev" | sed -E 's/.*[^[:digit:]]([[:digit:]]+$)/\1/g' for part in "${dev}p"*; do if [ "$part" = "${dev}p*" ]; then # Single partition image. part="${dev}" fi dst="/mnt/$(basename "$part")" echo "$dst" 1>&2 sudo mkdir -p "$dst" sudo mount "$part" "$dst" done ) loop-unmount-partitions() ( set -e for loop_id in "$@"; do dev="/dev/loop${loop_id}" for part in "${dev}p"*; do if [ "$part" = "${dev}p*" ]; then part="${dev}" fi dst="/mnt/$(basename "$part")" sudo umount "$dst" done sudo losetup -d "$dev" done ) loop_id="$(loop-mount-partitions "$img_file")" sudo cmp /mnt/loop0p1/file-1 "${root_dir_1}/file-1" sudo cmp /mnt/loop0p2/file-2 "${root_dir_2}/file-2" loop-unmount-partitions "$loop_id" Tested on Ubuntu 18.04. GitHub upstream.

Related questions

0 votes
    I am ploating simple linear regression plot in R and I want to save it as JPEG or PNG file, How can I do ... it is, Is it possible? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    i have this problem that i want to upload image on a server through multi-part form but its keep ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    i have this problem that i want to upload image on a server through multi-part form but its keep ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I have a very long query. I would like to split it in several lines in Python. A way to do it in JavaScript ... '\ 'def_id=' + def_id Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Which of the following principle of cyber security restricts how privileges are initiated whenever any object or subject is ... Open-Design Fail-safe Defaults None of the above...
asked Mar 6, 2021 in Technology by JackTerrance
0 votes
    Create a nested list as follows using both ordered and unordered list tags: 1. Operating System o Disk operating ... Unix o Linux Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    The process through which the on-disk tree files periodically merged to create larger consolidated stores is called __________....
asked Jan 26, 2021 in Technology by JackTerrance
0 votes
    In data flow diagrams (DFD), the data flow shape represents boundary between trust levels or privileges. A. True B. False...
asked Feb 27, 2023 in Technology by JackTerrance
0 votes
    __________ is achieved by distributing privileges for accomplishing a task to different people. (1)Principle of Least Privilege (2)Privilege Escalation (3)Separation of duties...
asked May 31, 2021 in Technology by JackTerrance
0 votes
    without mentioning which of the following for browser to find and display an image? (a) source of the image (b) ... none of the given Select the correct answer from above options...
asked Dec 15, 2021 in Education by JackTerrance
0 votes
    How do we create and preload an image object in JavaScript? (a) Use new keyword (b) Call Image() ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    Each Kafka partition has one server which acts as the _________....
asked Nov 16, 2022 in Education by JackTerrance
0 votes
    a) North East in the Twilight of Independence - Cabinet Mission, the Grouping Plan and Partition a) Congress ... and its Consequences Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    I have a data pipeline set up in Azure where I send messages to an IoTHub which then routes those ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
...