Changes between Version 1 and Version 2 of linux/blockdev
- Timestamp:
- 10/22/2017 06:02:01 AM (7 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
linux/blockdev
v1 v2 5 5 == Imaging a Block device == 6 6 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: 7 1. partition the device8 2. format the partition(s)9 3. install the rootfs to the partition7 1. Partition the device 8 2. Format the partition(s) 9 3. Install the rootfs to the partition 10 10 11 11 The 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 support14 * can be re-sized even if mounted (via resize2fs)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 15 16 16 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) 17 17 18 18 1. Determine device: 19 {{{ 19 {{{#!bash 20 20 DEVICE=/dev/sdc 21 21 }}} 22 22 23 23 2. Partition device: 24 {{{ 25 printf " ,,83,,\n" | sudo sfdisk${DEVICE}24 {{{#!bash 25 printf "2048,,L,,\n" | sudo sfdisk -uS ${DEVICE} 26 26 }}} 27 * here we create a single Linux (type 83) partition - you can adjust the parameters to create multiple partitions depending on your needs27 * here we create a single Linux (type 83) partition offset at 1MiB - you can adjust the parameters to create multiple partitions depending on your needs 28 28 29 3. format the partition as ext430 {{{ 29 3. Format the partition as ext4 30 {{{#!bash 31 31 sudo mkfs.ext4 ${DEVICE}1 32 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 sfdiskto allow the OS to re-scan the partition table before it will realize that the partition exists33 * 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 34 35 3. mount the partition:36 {{{ 35 3. Mount the partition: 36 {{{#!bash 37 37 sudo mount ${DEVICE}1 /mnt/disk 38 38 }}} 39 * obviouslymake sure /mnt/disk exists - this is simply a mount-point39 * make sure /mnt/disk exists - this is simply a mount-point 40 40 41 4. un-archive the rootfs tarball:42 {{{ 41 4. Un-archive the rootfs tarball: 42 {{{#!bash 43 43 sudo tar -C /mnt/disk -xvf rootfs.tar.bz2 44 44 }}} 45 * if instead you have a rootfs directory created with fakeroot or deboostrapyou can copy it with a {{{sudo cp -rupv rootfs/* /mnt/disk}}}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 46 47 5. unmount the partition:48 {{{ 47 5. Unmount the partition: 48 {{{#!bash 49 49 sudo umount /mnt/disk 50 50 }}} 51 51 52 52 53 === Imaging from U-Boot === 54 For block device based boards, Gateworks has created an installable U-Boot script that will apply block device image files containing either an entire disk image or a single partition image (see the [#DiskImages below] section for creating disk images). This can be particularly useful for updating the firmware of a running target. 55 56 To update a block device: 57 '''1. Compress your image 58 In most situations the DRAM of your device will be smaller than the image file required to update your system, therefore it is necessary to split the image file and apply the resulting compressed parts in a piece wise update. 59 {{{#!bash 60 FILE=openwrt_1602_8gb.img 61 # Split file every 500M with suffix ".part" and decimal increment 62 split -d -b 500M ${FILE} ${FILE}.part 63 gzip ${FILE}.* 64 for i in $(ls ${FILE}.*); do 65 mv $i ${FILE}.gz.$(echo $i | cut -d'.' -f3) 66 done 67 68 # Result will be 16 files such as openwrt_1602_8gb.img.gz.partXX 69 # which adhere to the naming convention expected by the script 70 }}} 71 If you have a small image file that when compressed will fit into your device's DRAM, a simple {{{gzip -k ${FILE} }}} will suffice. 72 '''2. Install the U-Boot script 73 On the target machine, source the [http://trac.gateworks.com/raw-attachment/wiki/linux/blockdev/block_image_update.scr block_image_update.scr] U-Boot script attached to this page (or copy its contents with an editor). For example via tftp: 74 {{{#!bash 75 setenv serverip 192.168.1.100 76 tftp block_image_update.scr 77 source $loadaddr 78 }}} 79 '''3. Configure your U-Boot environment 80 From the script's usage: 81 {{{ 82 The following environment variables need to be set in order for this script to run: 83 84 imagefile - full path file name of gzipped (.gz) file 85 devtype - interface type of target block device 86 devnum - device number of target 87 partoffset - start of target partition in bytes (0 for full disk image) 88 89 Optional environment variable arguments: 90 91 splitsuffix - the suffix of the split gzipped file (eg .part for file.img.gz.part00) 92 imageload - the command, up to the file name, that will load your imagefile to 93 loadaddr (defaults to [tftp 0x12000000]) 94 }}} 95 An example configuration: 96 {{{#!bash 97 setenv imagefile openwrt_1602_8gb.img.gz # image file from example above 98 setenv devtype mmc # mmc interface for eMMC or uSD 99 setenv devnum 0 # first device 100 setenv partoffset 100000 # target the 1st partition at 1M offset 101 setenv splitsuffix ".part" # suffix from example above 102 setenv imageload "tftp ${loadaddr}" # command to load the image file (can be ext4load) 103 }}} 104 105 '''4. Run the added script with {{{run block_image_update}}} 106 53 107 == Disk Images == 54 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.108 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. 55 109 56 110 Compared to distributing filesystem images or archives of filesystem contents this has method has some pro's and con's: 57 * pro's:111 * Pro's: 58 112 * 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 onto61 * 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-sized113 * Con's: 114 * Images are created from a fixed-size block device and require the same or larger size block device to be installed onto 115 * 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 116 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 {{{ 117 * 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. 118 {{{#!bash 65 119 sudo dd if=/dev/sdc bs=4M count=941 conv=notrunc,noerror | gzip -c > disk-img.gz 66 120 }}} 67 121 68 122 === Creating a disk image === 69 Procedure to create disk image from /dev/sdc:70 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):71 {{{ 123 Procedure to create disk image from {{{/dev/sdc}}}: 124 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): 125 {{{#!bash 72 126 sudo dd if=/dev/zero of=/dev/sdc bs=4M 73 127 }}} 74 128 75 2. copy and gzip to a file:76 {{{ 129 2. Copy and gzip to a file: 130 {{{#!bash 77 131 sudo dd if=/dev/sdc bs=4M conv=sync,noerror | gzip -c > disk-img.gz 78 132 }}} 79 * you can use another compressor other than gzip if you wish but make sure those needing to use these images can support that compression80 * the conv=sync,noerror tells ddto not fail on error and pad any partial or failed blocks with zeros133 * You can use another compressor other than gzip if you wish but make sure those needing to use these images can support that compression 134 * The {{{conv=sync,noerror}}} tells {{{dd}}} to not fail on error and pad any partial or failed blocks with zeros 81 135 82 136 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 83 137 138 84 139 === Using a disk image === 85 Procedure to write a disk image back to /dev/sdc:140 Procedure to write a disk image back to {{{/dev/sdc}}}: 86 141 * Linux: 87 {{{ 142 {{{#!bash 88 143 zcat disk-img.gz | sudo dd of=/dev/sdc bs=4M 89 144 }}} 90 145 * Windows: 91 - see [http://www.alexpage.de/usb-image-tool/ USB Image Tool]146 - See [http://www.alexpage.de/usb-image-tool/ USB Image Tool] 92 147 93 148 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 94 149 150 Once booted you can use OS specific commands to resize partitions to take advantage any unused space on the device (for example, if the image was created using a 2GB microSD and you placed it onto a 16GB you can expand partitions to use the extra 14GB) 151 152 You can do this in Linux for an ext2/3/4 filesystem with {{{resize2fs}}}. Because {{{resize2fs}}} will not alter the partition table you need to do that first. 153 154 The following example assumes you have a single ext filesystem and will resize it using {{{resize2fs}}}, {{{partprobe}}}, and {{{sfdisk}}}: 155 1. Install necessary tools (assuming Ubuntu/debian): 156 {{{#!bash 157 apt-get install parted sfdisk e2fsprogs 158 }}} 159 2. Use {{{sfdisk}}} to repartition: 160 {{{#!bash 161 DEVICE=/dev/mmcblk0 # IMX6 microSD host (use /dev/sda for a USB or SATA disk) 162 df -h . # show current size/used/avail 163 sfdisk --list ${DEVICE} # show current 164 printf "2048,,L,,\n" | sfdisk --force --no-reread -uS ${DEVICE} # re-create partition 165 sfdisk --list ${DEVICE} # show new 166 }}} 167 3. Use {{{partprobe}}} (or reboot system) to force kernel to re-read the partition table of device with mounted filesystems: 168 {{{#!bash 169 partprobe # re-read partition table 170 }}} 171 4. Use {{{resize2fs}}} to resize a partition to take up newly allocated space: 172 {{{#!bash 173 resize2fs ${DEVICE}*1 # resize first partition 174 }}}