Raspberry Pi Image Too Big For Same Size SD Card

The Experiment: In this experiment were  going to research why Win32 Disk Imager  keep popping not enough room on the card! when trying to write a backup image to a new SD card.

Table of Contents
1. Progress
2. Scripts
3. Notes
4. Conclusion
5. Reference Links

Progress

Well after a lot of searching on Google, Discovered that all SD cards have a slightly different size.  Even writing an image to a same Brand / Size SD card, could cause not enough room on the card error.  Looking at raspi-config source code discovered that the Expand Filesytem selection expands the partition to the max size.

Since I have so many image backed-up which can not be written to the same size SD card.  Searched again for a way to resize the partition within the backup image file. Well there were quit a view suggestion / scripts but the one’s  tried all had error’s with some problem.

So back to  raspi-config looking through code section do_expand_rootfs() the fdisk command expands the partition to the max size.

fdisk /dev/mmcblk0 <span class="pl-s"><span class="pl-k"><<</span><span class="pl-k">EOF</span></span>
p
d
$PART_NUM
n
p
$PART_NUM
$PART_START

p
w
EOF

To make a backup images usable the partition needs to be a little smaller then the SD  card size. Not having used fdisk, I knew what needed to change, but did not know exactly what to do.

Searching Google again I found a little script called rpi-wiggle  look  through the code, looked save,  lauched it and WOW just what was needed,  rpi-wiggle expands the partition to almost max minus 3 512 blocks.

So back to raspi-config,  were to put a partition end. Looking though fdisk doc. The solution was simple just add a partition end right after the $PART_START.

Borrowing some code from rpi-wiggle,  modified raspi-config ,  code and added code sinppet form rpi-wiggle  also added right after $PART_START / $PART_END.  The rest of the raspi-config code remains the same.


LAST_PART_NUM=$(parted /dev/mmcblk0 -ms unit s p | tail -n 1 | cut -f 1 -d:)
if [ "$LAST_PART_NUM" != "$PART_NUM" ]; then
   whiptail --msgbox "/dev/root is not the last partition. Don't know how to expand" 20 60 2
   return 0
fi

#******* Add this Code snipet from rpi-wiggle here *******
DISK_SIZE="$(($(sudo blockdev --getsz /dev/mmcblk0)/2048/925))";
# Get the ending offset of the root partition - minus 3 512 blocks
PART_END="$(((DISK_SIZE*925*2048-1)-1536))"
#*******                End code snippet           *******


# Get the starting offset of the root partition
PART_START=$(parted /dev/mmcblk0 -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d:)
[ "$PART_START" ] || return 1
# Return value will likely be error for fdisk as it fails to reload the
# partition table because the root fs is mounted
fdisk /dev/mmcblk0 <span class="pl-s"><span class="pl-k"><<</span><span class="pl-k">EOF</span></span>
p
d
$PART_NUM
n
p
$PART_NUM
$PART_START
$PART_END <-- add this

p
w
EOF
 ASK_TO_REBOOT=1

Saved  raspi-config, launched it sudo raspi-config. Selected Expand Filesytem and WOW worked exactly like rpi-wiggle the partition was max minus 3 512 blocks.

Note: 2016-raspbian-jessie-lite.img automatically expand the filesystem after booting up. See Notes: Alert for details.
^Top


Scripts:

  1. raspi-config
  2. rpi-wiggle

^Top


Notes:

rasp-config folders
/etc/init.d/raspi-config
/usr/bin/raspi-config
/usr/share/doc/raspi-config

Modified the /usr/bin/raspi-config

Alert:
2016-raspbian-jessie-lite.img automatically expand the filesystem after booting up.
Before you plugin your new SD card, into your Raspberry Pi.
Edit the file: cmdline.txt and remove the following text:
init=/usr/lib/raspi-config/init_resize.sh
^Top


Conclusion:

It took at least two day to figure out why Win32 Disk Imager  keep saying say not enough room on the card!, when the exact Brand / Size SD card was used.

For now won’t expand the Filesystem with raspi-config  will use rpi-wiggle instead.  Would be nice if raspi-config took into consideration the slight differences in SD car size and expaded the partition slightly under the max SD card size. It’s a very simple fix.

Still searching for a working solution to reduce the partition within image file or reduce the size of the SD card monted in a USB SD card adapter.

^Top


Reference Links:

1. raspi-config
2. rpi-wiggle

^Top

Advertisements