Create USB Stick from Image File


Write Image File to USB Stick

The image file UGW-OSS13.1_x86_64-life-for-Linda.img containing an OpenSuSE 13.1 x86_64 system and the software to use the Linda apparatus has a size of somewhat over 7GB.
You need at least a 8GB USB stick, but 16GB is recommended.
In the examples below image-file.img is used as the image filename.

Write the image file to the USB stick using dd.
Double check whether /dev/sdX is the correct device corresponding to the USB stick to write to!
/dev/sdX gets overwritten without asking for confirmation as soon as you start the dd command.

dd if=image-file.img of=/dev/sdX bs=1M

Growing root Partition on the USB Stick

Growing root fs Using cfdisk and resize2fs

The root file system has a size which is the minimum to hold all files needed. This means that there is no space left for anything else. For the system to be usable you need to grow the root partition.

It is possible to do this even with the partition mounted as root file system, but the start of the new root partition needs to be aligned with the old root partition!

Use cfdisk to list current partition data of the SD card:

# cfdisk -Ps /dev/sdX
Partition Table for /dev/sdX

               First       Last
 # Type       Sector      Sector   Offset    Length   Filesystem Type (ID) Flag
-- ------- ----------- ----------- ------ ----------- -------------------- ----
   Pri/Log           0        2047*     0#       2048*Free Space           None
 1 Primary        2048*   15198207*     0    15196160*Linux (83)           None

After writing the 7GB image to the 16GB SD card, use cfdisk to delete the partition with the root file system and all partitions listed after the one with the root fs, if there are some.

Still in cfdisk, create a new partition using all available space (or less, if you would like to create other partitions, e.g. swap).

If you did the hot repartitioning (i.e. with the resized partition mounted as root filesystem), do a reboot.
As the last step run resize2fs:

resize2fs /dev/sdX1

Shrink the image file to a Minimum

The image file UGW-OSS13.1_x86_64-live-for-Linda.img has been shrunk from it's original size to a minimum to be easier to transport over the network.
The following describes the process used to shrink.

Shrinking an image produced by dd command involves:


losetup -f                             # find a loop device free for use
losetup -P /dev/loop0 image-file.img   # attach image file to /dev/loop0
e2fsck -f /dev/loop0p1                 # check/repair file system for clean state
resize2fs -M /dev/loop0p1              # resize (shrink) file system as much as possible

Output of the resize2fs command:

resize2fs 1.42.8 (20-Jun-2013)
Resizing the filesystem on /dev/loop0p1 to 1575341 (4k) blocks.
The filesystem on /dev/loop0p1 is now 1575341 blocks long.

This tells that the filesystem has a size of 1575341 * 4096 = 6'452'596'736 bytes.

Running the fdisk command reveals the loop0p1 partition starts at block 2048 (i.e. 2048 * 512 = 1'033'216 bytes), this is the usual alignment for partitions nowadays.

fdisk -l /dev/loop0

Disk /dev/loop0: 15.7 GB, 15728640000 bytes, 30720000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x970736f7

      Device Boot      Start         End      Blocks   Id  System
/dev/loop0p1            2048    30719999    15358976   83  Linux

To shrink the partition so that it just has the size of the shrunk filesystem, it should end at block (2048 + 1575341 * 8) = 12604776 (= 6'453'645'312 bytes)

Use your preferred tool to delete the loop0p1 partition and create a new one starting at the same place as before but ending at block 12604776.
Using parted ('s' stands for sectors):

parted /dev/loop0 rm 1                       # delete partition 1
parted /dev/loop0 mkpart primary 2048s 12604776s  # create new primary partition starting at sector 2048 extending to sector 12604776

Truncate the image file to the size needed to contain the complete partitions, i.e. 12604776 plus one sectors, sector size is 512 bytes.

truncate --size=$(( (12604776+1)*512)) image-file.img

Finally detach image file from /dev/loop0:

losetup -d /dev/loop0               # detach image file from /dev/loop0
Using gparted for resizing both, file system and partition

Using the GUI program gparted for partioning tasks the shrinking may be done in one step (filesystem and partition).
But at least my version of gparted looks after a device file named loop0_part1 for the first partition on "disk" (/dev/loop0) but the loop partition device is named loop0p1. Creating a symbolic link:

ln -s /dev/loop0p1 /dev/loop0_part1

made gparted happy.

Also see

Unlike growing, shrinking of filesystem is not possible online. So the filesystem needs to be unmounted to be shrinken.



This site maintained by:
My public PGP key
last updated: 2016-04-06 Valid CSS! Valid XHTML 1.0 Strict