Menu

Create compressed image on Linux

2016-10-06
2016-11-23
  • Henry Coakley

    Henry Coakley - 2016-10-06

    Is there any way to generate a compressed image file on Linux that dotNet Disk Imager can write to a drive? If not compressed, is there a way to generate an uncompressed image file on Linux?

     
  • Filip Sikora

    Filip Sikora - 2016-10-08

    Hello, I'm not a Linux expert, but AFAIK there is dd command described here. You may want to try it out.

     
  • Henry Coakley

    Henry Coakley - 2016-11-06

    Is that how dotNet Imager creates a compressed image - gzip?

     
  • Filip Sikora

    Filip Sikora - 2016-11-08

    I'm not sure what you're asking, what do you mean by "Is that how dotNet Imager creates a compressed image - gzip?" ?

     
  • Henry Coakley

    Henry Coakley - 2016-11-23

    Never mind - I figured it out. The compressed image is created using normal DEFLATE, so I can take an uncompressed image and "zip" it into a compressed image for dotNetImager. I can create a multi-partition image and zip it up using the bash script below. This same approach could be extended to more partitions, if needed.

    # run as "sudo"
    OUTPUT=$HOME
    TARGET=zipped.img
    
     # Delete old image, if it exists
    rm -rf $OUTPUT/test.img
    
    # Generate a 4M * 1024 file - 4GB
    dd if=/dev/zero of=$OUTPUT/test.img bs=1024 count=0 seek=4M
    
    # Mount the image as a loop device, and create two partitions (DOS and EXT4)
    losetup /dev/loop0 $OUTPUT/test.img
    parted /dev/loop0 --script rm 1
    parted /dev/loop0 --script rm 2
    parted /dev/loop0 --script mklabel msdos
    parted /dev/loop0 --script mkpart primary fat32 0% 128MiB
    parted /dev/loop0 --script mkpart primary ext3 128MiB 100%
    sleep 10
    mkfs.msdos /dev/loop0p1
    mkfs.ext4 /dev/loop0p2
    
    # Fill the first partition
    mkdir -p /mnt/sdcard
    mount /dev/loop0p1 /mnt/sdcard/
    cd /mnt/sdcard
    # Copy files to the partition
    cd .. 
    umount /mnt/sdcard 
    
    # Fill the second partition
    mount /dev/loop0p2 /mnt/sdcard 
    cd /mnt/sdcard 
    # Copy files to the partition
    cd .. 
    umount /mnt/sdcard 
    
    # Remove the loop device
    losetup -d /dev/loop0
    
    # Remove the previous target (if it exists)
    rm -rf $OUTPUT/$TARGET.img
    
    # Zip up the image into a compressed image
    zip $OUTPUT/$TARGET.img $OUTPUT/test.img
    
    # Remove the intermediate image
    rm -rf $OUTPUT/test.img
    
     

    Last edit: Henry Coakley 2016-11-23

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.