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.

  • That's quite a vague question. Are you cloning a running system? – Andy Anderson Feb 27 '18 at 18:55
  • well the SD card will always have to be at least as big as the .img file you create, and for the most part the .img file will be the size of the sum of partitions on the SD card you are cloning. If you want it as small as posible, you will need to minimize the partition sizes on the host SD card before you DD it off into an image file – Chad G Feb 27 '18 at 19:01
  • @AndyAnderson Yes. basically, boot.img will contain 2 partitions. First is FAT32 boot which will contain all Pi firmware, kernel images and boot loader. The second will contain linux file system. – Mero Feb 27 '18 at 19:02
  • 1
    Raspbian Image expands itself on first boot, at least in default release image. This is done by the script /usr/lib/raspi-config/init_resize.sh . I would simply copy their approach. The magic strep is that init_resize is actually specified as init for the first boot in /boot/cmdline.txt, and then deletes itself from that file and reverts to default (systemd or init depending on version) – crasic Feb 27 '18 at 21:33
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

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.

  • SD card copier can write an image to an 8GB SD card from a 16GB card, assuming the used area is less than 8GB. – Andy Anderson Mar 8 '18 at 18:13
  • Yes, but the point there is "SD card copier can...". The active agency is a program, and yes, programmatically this is not hard (hence "You could write a script..."). But the question as quoted above is, "How can I create an image so it can be written to any SD card size...". You cannot create such an image. The size of an image file is static, just like the size of a text file. If you want to change it, you need software as an intermediary. – goldilocks Mar 8 '18 at 19:34
  • My point was the image could be created on a smaller card than the original, based on the amount of used space. It goes without saying that you couldn't write an 8GB image to a 4GB card. If it just making sure the image takes up the whole of the card at first boot then --expand-rootfs should do it. – Andy Anderson Mar 9 '18 at 7:54
  • "It goes without saying..." -> I don't assume anything after some of the stuff I've read here, lol. If I am taking the OP too literally, then so be it ;) Executables are files; hence there are self-extracting archives and so on. It is not a big stretch for someone to postulate that a device image file could be made exectuable such that it would install itself to an appropriate size (based on the amount of grief around here involving creating an SD card that works, it would be a good idea). – goldilocks Mar 9 '18 at 13:52
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.

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>                            │
│                                                                                                  
│

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.