Version 1 (modified by trac, 7 years ago) ( diff )


Linux Block Storage Devices

Imaging a Block device

To place a root filesystem on a removable block storage device (mSATA disk, microSD card, USB Mass Storage device) you need to to the following:

  1. partition the device
  2. format the partition(s)
  3. install the rootfs to the partition

The popular Linux filesystem used today is ext4 which has the following features:

  • built-in to most kernels (allowing it to be your root filesystem)
  • journalling support
  • can be re-sized even if mounted (via resize2fs)

The following commands will perform the above steps on a Linux development host. For convenience we set an env variable to represent the block device - take care to set this properly so as not to format a partition vital to your host (such as its rootfs)

  1. Determine device:
  1. Partition device:
    printf ",,83,,\n" | sudo sfdisk ${DEVICE}
    • here we create a single Linux (type 83) partition - you can adjust the parameters to create multiple partitions depending on your needs
  1. format the partition as ext4
    sudo mkfs.ext4 ${DEVICE}1
    • Note that if you put this into a script I've found you need to have a second or so of a sleep following the sfdisk to allow the OS to re-scan the partition table before it will realize that the partition exists
  1. mount the partition:
    sudo mount ${DEVICE}1 /mnt/disk
    • obviously make sure /mnt/disk exists - this is simply a mount-point
  1. un-archive the rootfs tarball:
    sudo tar -C /mnt/disk -xvf rootfs.tar.bz2
    • if instead you have a rootfs directory created with fakeroot or deboostrap you can copy it with a sudo cp -rupv rootfs/* /mnt/disk
  1. unmount the partition:
    sudo umount /mnt/disk

Disk Images

Compressed Disk Images (technically 'block device images') can be easily created on a Linux system with dd and gzip. This works for any block device (ie rotational disk, SSD, uSD, USB Mass Storage disk) and doesn't care at all what is on the disk. This is a very common technique for distributing pre-built OS images because it preserves the partitioning scheme of the disk you are imaging.

Compared to distributing filesystem images or archives of filesystem contents this has method has some pro's and con's:

  • pro's:
    • Other OS's can install this type of image onto another block storage device (such as Windows via USB Image Tool)
  • con's:
    • images are created from a fixed-size block device and require the same or larger size block device to be installed onto
    • after installation any additional storage space (ie installing an image meant for a 2GB microSD onto a 16GB microSD) can not be used until the disk is re-partitioned and re-sized
  • device sizes vary greatly - various manufacturers 4GB stick will likely not have the same number of cylinders (thus bytes) as another. If you try to expand an image onto a smaller device it will fail. Therefore you should probably tell dd to only grab perhaps 95% of the disk. For example, if you are imaging a 4GB stick (1024*0.95=972). I have 3x 4GB block devices here and the smallest is shown by sfdisk as 3.677GiB.
    sudo dd if=/dev/sdc bs=4M count=941 conv=notrunc,noerror | gzip -c > disk-img.gz

Creating a disk image

Procedure to create disk image from /dev/sdc:

  1. (optional) zero out the storage device first (if not already done) to allow for the best compression as even if previous contents have been 'deleted' the data still exists (the filesystem directory table has just unlinked from it):
    sudo dd if=/dev/zero of=/dev/sdc bs=4M
  1. copy and gzip to a file:
    sudo dd if=/dev/sdc bs=4M conv=sync,noerror | gzip -c > disk-img.gz
    • you can use another compressor other than gzip if you wish but make sure those needing to use these images can support that compression
    • the conv=sync,noerror tells dd to not fail on error and pad any partial or failed blocks with zeros

Note that these steps can take some time depending on the speed of your storage device. For example each step can take 10mins on an 8GB micro-SD over USB or 3mins on a 2GB micro-SD over USB

Using a disk image

Procedure to write a disk image back to /dev/sdc:

Note that this can take a some time depending on the write speed of your storage device. For example you will likely see times such as 4mins for 2GB and 30mins for 8GB micro-SD

Attachments (1)

Download all attachments as: .zip

Note: See TracWiki for help on using the wiki.