Neil Bothwick <n...@digimed.co.uk> [16-01-16 12:44]:
> On Sat, 16 Jan 2016 17:53:18 +0800, Bill Kenworthy wrote:
> 
> > It is possible to create a dd image of the whole SD card and mount it in
> > a loopback to repartition etc.
> > 
> > Mount it then shrink your existing file system (and probably the
> > partition too) to less than the required size, then recreate the dd
> > image to a size still less than the new sd card.
> > 
> > On the new card, dd it across until it errors off, then fix/expand the
> > partition and then the file system.  You dont care if the end is missing
> > as long as your data is within the size needed.
> 
> I get the impression that Meino is trying to keep it as simple as
> possible for the other user, so fixing after errors is not a good idea.
> Otherwise you could do what you suggest with the original card, shrink the
> filesystem and partition to fit a smaller card then create a dd image
> that will overflow but should work.
> 
> 
> -- 
> Neil Bothwick
> 
> RISC: Reduced Into Silly Code


Hi,

Neil is completly right here (Neil, you are completly right here! :)

Background: There is a cheap (15$) version of the Raspberry PI, which 
is based on a 4core, 1.2GHz Allwinner H3 CPU -- the OrangePI-PC

There are available several firmware images available for this board,
and each images can do a certain thing better than another (for
example hardware accelerated graphic). 

You dd the image on a sdcard, put that one in a reader, copy two files
from /boot to /media/boot, put the sdcard into the OrangePI-PC, boot
it, log in via ssh and call a script named "fs_resize", the miniPC
reboots...and VOILA!

Now I want to create such an image from parts of another image
(kernel, firmware) and a bootable Gentoo minimal setup.

For that I need to understand the trick which is used to create such
images.

The script mentioned above is this one:


#!/bin/bash

# ******************************************
# Resize Linux ext4 partition to fill sdcard
# ******************************************

if [ "$(id -u)" != "0" ]; then
        echo "Script must be run as root !"
        exit 0
fi

_REL=`lsb_release -sc`

_rootpart=`mount | grep "on / " | awk '{print $1}'`
if [ "${_rootpart}" = "/dev/mmcblk0p2" ]; then
        rootdrv="mmcblk0p2"
        sdcard="/dev/mmcblk0"
elif [ "${_rootpart}" = "/dev/mmcblk1p2" ]; then
        rootdrv="mmcblk1p2"
        sdcard="/dev/mmcblk1"
else
        echo "Root fs mount partition not found!"
        exit 1
fi
echo ""

fdisk -l $sdcard | grep $sdcard
echo ""

_btrfs=`mount | grep -o btrfs`

sdcard_part=`fdisk -l $sdcard | grep $rootdrv | awk '{print $1}'`
sdcard_sect=`fdisk -l $sdcard | grep "Disk $sdcard" | awk '{print $7}'`
if [ "${sdcard_sect}" = "" ]; then
    sdcard_sect=`fdisk -l $sdcard | grep total | awk '{print $8}'`
fi
sdcard_end=$(expr $sdcard_sect - 1024)

part_start=`fdisk -l $sdcard | grep $rootdrv | awk '{print $2}'`
part_end=`fdisk -l $sdcard | grep $rootdrv | awk '{print $3}'`

echo "  Max block: $sdcard_end"
echo "   Part end: $part_end"
echo " Part start: $part_start"
if [ ! "${_btrfs}" = "" ]; then
    echo " btrfs part: yes"
    _resize="btrfs filesystem resize max /"
else
    _resize="resize2fs ${sdcard_part}"
fi
echo ""
if [ $part_end -ge $sdcard_end ]; then
    echo "Partition allready maximum size !"
    rm /usr/local/bin/fs_resize_warning > /dev/null 2>&1
    exit 0
fi

echo -n "WARNING: Do you want to resize \"$sdcard_part\" (y/N)?  "
read -n 1 ANSWER
if [ ! "${ANSWER}" = "y" ] ; then
    echo ""
    echo "Canceled.."
    exit 0
fi
echo ""

# RESIZE PARTITION

echo -e "p\nd\n2\nn\np\n2\n$part_start\n$sdcard_end\nw" | fdisk ${sdcard} > 
/dev/null 2>&1
#if [ $? -ne 0 ]; then
#       echo "ERROR resizing partition!"
#       exit 1
#fi

echo "PARTITION RESIZED."

mv /etc/rc.local /etc/rc.local.orig

cat > /etc/rc.local << _EOF_
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# ** Overclock to 1.728 GHz
#echo 1728000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq

echo 0 > /proc/sys/kernel/hung_task_timeout_secs

dmesg -n 1

/usr/local/bin/resize_fs &&

_EOF_

echo "exit 0" >> /etc/rc.local
chmod +x /etc/rc.local > /dev/null 2>&1

cat > /usr/local/bin/resize_fs << _EOF_
#!/bin/bash
$_resize
if [ \$? -eq 0 ]; then
    rm /usr/local/bin/fs_resize_warning
    rm /usr/local/bin/resize_fs
    sleep 2
    rm /etc/rc.local
    mv /etc/rc.local.orig /etc/rc.local
fi
_EOF_

chmod +x /usr/local/bin/resize_fs > /dev/null 2>&1


REBOOT=1
echo "*********************************************"
echo "Rootfs Extended. Please REBOOT to take effect"
echo "*********************************************"
echo ""



Best regards,
Meino



Reply via email to