Changes between Initial Version and Version 1 of linux/blockdev


Ignore:
Timestamp:
10/22/2017 05:28:45 AM (7 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • linux/blockdev

    v1 v1  
     1[[PageOutline]]
     2
     3= Linux Block Storage Devices =
     4
     5== Imaging a Block device ==
     6To place a root filesystem on a removable block storage device (mSATA disk, microSD card, USB Mass Storage device) you need to to the following:
     7 1. partition the device
     8 2. format the partition(s)
     9 3. install the rootfs to the partition
     10
     11The popular Linux filesystem used today is ext4 which has the following features:
     12 * built-in to most kernels (allowing it to be your root filesystem)
     13 * journalling support
     14 * can be re-sized even if mounted (via resize2fs)
     15
     16The 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)
     17
     181. Determine device:
     19{{{
     20DEVICE=/dev/sdc
     21}}}
     22
     232. Partition device:
     24{{{
     25printf ",,83,,\n" | sudo sfdisk ${DEVICE}
     26}}}
     27 * here we create a single Linux (type 83) partition - you can adjust the parameters to create multiple partitions depending on your needs
     28
     293. format the partition as ext4
     30{{{
     31sudo mkfs.ext4 ${DEVICE}1
     32}}}
     33 * 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
     34
     353. mount the partition:
     36{{{
     37sudo mount ${DEVICE}1 /mnt/disk
     38}}}
     39 * obviously make sure /mnt/disk exists - this is simply a mount-point
     40
     414. un-archive the rootfs tarball:
     42{{{
     43sudo tar -C /mnt/disk -xvf rootfs.tar.bz2
     44}}}
     45 * if instead you have a rootfs directory created with fakeroot or deboostrap you can copy it with a {{{sudo cp -rupv rootfs/*  /mnt/disk}}}
     46
     475. unmount the partition:
     48{{{
     49sudo umount /mnt/disk
     50}}}
     51
     52
     53== Disk Images ==
     54Compressed 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.
     55
     56Compared to distributing filesystem images or archives of filesystem contents this has method has some pro's and con's:
     57 * pro's:
     58  * Other OS's can install this type of image onto another block storage device (such as Windows via [http://www.alexpage.de/usb-image-tool/ USB Image Tool])
     59 * con's:
     60  * images are created from a fixed-size block device and require the same or larger size block device to be installed onto
     61  * 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
     62
     63 * 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.
     64{{{
     65sudo dd if=/dev/sdc bs=4M count=941 conv=notrunc,noerror | gzip -c > disk-img.gz
     66}}}
     67
     68=== Creating a disk image ===
     69Procedure to create disk image from /dev/sdc:
     701. (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):
     71{{{
     72sudo dd if=/dev/zero of=/dev/sdc bs=4M
     73}}}
     74
     752. copy and gzip to a file:
     76{{{
     77sudo dd if=/dev/sdc bs=4M conv=sync,noerror | gzip -c > disk-img.gz
     78}}}
     79 * you can use another compressor other than gzip if you wish but make sure those needing to use these images can support that compression
     80 * the conv=sync,noerror tells dd to not fail on error and pad any partial or failed blocks with zeros
     81
     82Note 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
     83
     84=== Using a disk image ===
     85Procedure to write a disk image back to /dev/sdc:
     86 * Linux:
     87{{{
     88zcat disk-img.gz | sudo dd of=/dev/sdc bs=4M
     89}}}
     90 * Windows:
     91  - see [http://www.alexpage.de/usb-image-tool/ USB Image Tool]
     92
     93Note 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
     94