1

I need to create a bootable image like ***.img for a SD card. At the moment I am doing something like this:

dd if=/dev/zero bs=512 count=1 seek=SD_CARD_SECTORS of=boot.img
sfdisk --force --no-reread -uS boot.img <<EOF
8192,8388608,c,*
8388609,,83
EOF
kpartx -a -s boot.img
mkfs -t vfat  /dev/mapper/loop0p1
mkfs -t ext2 /dev/mapper/loop0p2

However this will work but SD card size will be trimmed. So the example above if written to 16 GB SD card, we will end up only using first 8GB or so. How can I create an image so it can be written to any SD card size without losing any space?

So Something similar to Rasbian image.

Mero
  • 298
  • 5
  • 13

4 Answers4

4

How can I create an image so it can be written to any SD card size without losing any space?

You can't, or at least, you can't use a static image. You could write a script to resize it dynamically depending on the size of the target medium.

The other alternative is to resize on first boot, which is what Raspbian does.

goldilocks
  • 60,325
  • 17
  • 117
  • 234
4

The Raspbian Approach is as follows, I would duplicate this approach. raspi-config provides a resize script that uses parted (a command line utility) to resize the filesystem on boot. This script is located in /usr/lib/raspi-config/init_resize.sh

  1. Create image with minimal fs size
  2. Specify in /boot/cmdline.txt the option init=/usr/lib/raspi-config/init_resize.sh
  3. On first boot this script will execute and delete the line from /boot/cmdline.txt

Before: dwc_otg.lpm_enable=0 init=/usr/lib/raspi-config/init_resize.sh console=serial0,115200 console=tty1 root=PARTUUID=23668fa2-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

After: dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=23668fa2-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

You can choose to use the default raspi-config provided resize script by including the package in your base image, or, you may choose to create your own script to package with your image, using the same approach to do whatever other first-boot configuration you wish.


The resizing brains of the script are reproduced below

  if [ "$NOOBS" = "1" ]; then
    if ! parted -m "$ROOT_DEV" u s resizepart "$EXT_PART_NUM" yes "$TARGET_END"; then
      FAIL_REASON="Extended partition resize failed"
      return 1
    fi
  fi

  if ! parted -m "$ROOT_DEV" u s resizepart "$ROOT_PART_NUM" "$TARGET_END"; then
    FAIL_REASON="Root partition resize failed"
    return 1
  fi

Snippet to Resize Root Partition : parted -m "$ROOT_DEV" u s resizepart "$ROOT_PART_NUM" "$TARGET_END"

Rewriting the /boot/cmdline.txt is done with this line

sed -i 's| init=/usr/lib/raspi-config/init_resize.sh||' /boot/cmdline.txt

crasic
  • 3,033
  • 1
  • 11
  • 20
1

Use the package piclone, it's installed by default on the full Raspbian image. Great way to build, tweak, clean up and backup an installation.

A 16GB SD card with Raspbian can be striped to less than 8GB and then written to an 8GB SD card. I've got images down to 4GB and used a 4GB card to receive the backup clone. The card can then be read to an IMG file on a PC using Win32diskimager.

Andy Anderson
  • 649
  • 1
  • 4
  • 11
0

The official documentation recommend will be shown raspi-config on first booting into Raspbian. To open the configuration tool after this, simply run the following from the command line:

$ sudo raspi-config

enter image description here

Choice: Advanced Option(In the print is 8):

ADVANCED OPTIONS

EXPAND FILESYSTEM If you have installed Raspbian using NOOBS, the filesystem will have been expanded automatically. There may be a rare occasion where this is not the case, e.g. if you have copied a smaller SD card onto a larger one. In this case, you should use this option to expand your installation to fill the whole SD card, giving you more space to use for files. You will need to reboot the Raspberry Pi to make this available. Note that there is no confirmation: selecting the option begins the partition expansion immediately.

It has the following options available:

┤ Raspberry Pi Software Configuration Tool (raspi-config) 
├────────────────────┐
│                                                                                                  
│
│        1 Change User Password Change password for the current user                               
│
│        2 Network Options      Configure network settings                                         
│
│        3 Boot Options         Configure options for start-up                                     
│
│        4 Localisation Options Set up language and regional settings 
         to match your location
│
│        5 Interfacing Options  Configure connections to peripherals                               
│
│        6 Overclock            Configure overclocking for your Pi                                 
│
│        7 Advanced Options     Configure advanced settings                                        
│
│        8 Update               Update this tool to the latest version                             
│
│        9 About raspi-config   Information about this configuration 
        tool
│
│                                                                                                  
│
│                                                                                                  
│
│                                                                                                  
│   
│                           <Select>                           
  <Finish>                            │
│                                                                                                  
│