Gentoo Archives: gentoo-user

From: Meino.Cramer@×××.de
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Creating an firmware image from a too large microSDcard...?
Date: Sat, 16 Jan 2016 12:02:24
Message-Id: 20160116120204.GC18628@solfire
In Reply to: Re: [gentoo-user] Creating an firmware image from a too large microSDcard...? by Neil Bothwick
1 Neil Bothwick <neil@××××××××××.uk> [16-01-16 12:44]:
2 > On Sat, 16 Jan 2016 17:53:18 +0800, Bill Kenworthy wrote:
3 >
4 > > It is possible to create a dd image of the whole SD card and mount it in
5 > > a loopback to repartition etc.
6 > >
7 > > Mount it then shrink your existing file system (and probably the
8 > > partition too) to less than the required size, then recreate the dd
9 > > image to a size still less than the new sd card.
10 > >
11 > > On the new card, dd it across until it errors off, then fix/expand the
12 > > partition and then the file system. You dont care if the end is missing
13 > > as long as your data is within the size needed.
14 >
15 > I get the impression that Meino is trying to keep it as simple as
16 > possible for the other user, so fixing after errors is not a good idea.
17 > Otherwise you could do what you suggest with the original card, shrink the
18 > filesystem and partition to fit a smaller card then create a dd image
19 > that will overflow but should work.
20 >
21 >
22 > --
23 > Neil Bothwick
24 >
25 > RISC: Reduced Into Silly Code
26
27
28 Hi,
29
30 Neil is completly right here (Neil, you are completly right here! :)
31
32 Background: There is a cheap (15$) version of the Raspberry PI, which
33 is based on a 4core, 1.2GHz Allwinner H3 CPU -- the OrangePI-PC
34
35 There are available several firmware images available for this board,
36 and each images can do a certain thing better than another (for
37 example hardware accelerated graphic).
38
39 You dd the image on a sdcard, put that one in a reader, copy two files
40 from /boot to /media/boot, put the sdcard into the OrangePI-PC, boot
41 it, log in via ssh and call a script named "fs_resize", the miniPC
42 reboots...and VOILA!
43
44 Now I want to create such an image from parts of another image
45 (kernel, firmware) and a bootable Gentoo minimal setup.
46
47 For that I need to understand the trick which is used to create such
48 images.
49
50 The script mentioned above is this one:
51
52
53 #!/bin/bash
54
55 # ******************************************
56 # Resize Linux ext4 partition to fill sdcard
57 # ******************************************
58
59 if [ "$(id -u)" != "0" ]; then
60 echo "Script must be run as root !"
61 exit 0
62 fi
63
64 _REL=`lsb_release -sc`
65
66 _rootpart=`mount | grep "on / " | awk '{print $1}'`
67 if [ "${_rootpart}" = "/dev/mmcblk0p2" ]; then
68 rootdrv="mmcblk0p2"
69 sdcard="/dev/mmcblk0"
70 elif [ "${_rootpart}" = "/dev/mmcblk1p2" ]; then
71 rootdrv="mmcblk1p2"
72 sdcard="/dev/mmcblk1"
73 else
74 echo "Root fs mount partition not found!"
75 exit 1
76 fi
77 echo ""
78
79 fdisk -l $sdcard | grep $sdcard
80 echo ""
81
82 _btrfs=`mount | grep -o btrfs`
83
84 sdcard_part=`fdisk -l $sdcard | grep $rootdrv | awk '{print $1}'`
85 sdcard_sect=`fdisk -l $sdcard | grep "Disk $sdcard" | awk '{print $7}'`
86 if [ "${sdcard_sect}" = "" ]; then
87 sdcard_sect=`fdisk -l $sdcard | grep total | awk '{print $8}'`
88 fi
89 sdcard_end=$(expr $sdcard_sect - 1024)
90
91 part_start=`fdisk -l $sdcard | grep $rootdrv | awk '{print $2}'`
92 part_end=`fdisk -l $sdcard | grep $rootdrv | awk '{print $3}'`
93
94 echo " Max block: $sdcard_end"
95 echo " Part end: $part_end"
96 echo " Part start: $part_start"
97 if [ ! "${_btrfs}" = "" ]; then
98 echo " btrfs part: yes"
99 _resize="btrfs filesystem resize max /"
100 else
101 _resize="resize2fs ${sdcard_part}"
102 fi
103 echo ""
104 if [ $part_end -ge $sdcard_end ]; then
105 echo "Partition allready maximum size !"
106 rm /usr/local/bin/fs_resize_warning > /dev/null 2>&1
107 exit 0
108 fi
109
110 echo -n "WARNING: Do you want to resize \"$sdcard_part\" (y/N)? "
111 read -n 1 ANSWER
112 if [ ! "${ANSWER}" = "y" ] ; then
113 echo ""
114 echo "Canceled.."
115 exit 0
116 fi
117 echo ""
118
119 # RESIZE PARTITION
120
121 echo -e "p\nd\n2\nn\np\n2\n$part_start\n$sdcard_end\nw" | fdisk ${sdcard} > /dev/null 2>&1
122 #if [ $? -ne 0 ]; then
123 # echo "ERROR resizing partition!"
124 # exit 1
125 #fi
126
127 echo "PARTITION RESIZED."
128
129 mv /etc/rc.local /etc/rc.local.orig
130
131 cat > /etc/rc.local << _EOF_
132 #!/bin/sh -e
133 #
134 # rc.local
135 #
136 # This script is executed at the end of each multiuser runlevel.
137 # Make sure that the script will "exit 0" on success or any other
138 # value on error.
139 #
140 # In order to enable or disable this script just change the execution
141 # bits.
142 #
143 # By default this script does nothing.
144
145 # ** Overclock to 1.728 GHz
146 #echo 1728000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
147
148 echo 0 > /proc/sys/kernel/hung_task_timeout_secs
149
150 dmesg -n 1
151
152 /usr/local/bin/resize_fs &&
153
154 _EOF_
155
156 echo "exit 0" >> /etc/rc.local
157 chmod +x /etc/rc.local > /dev/null 2>&1
158
159 cat > /usr/local/bin/resize_fs << _EOF_
160 #!/bin/bash
161 $_resize
162 if [ \$? -eq 0 ]; then
163 rm /usr/local/bin/fs_resize_warning
164 rm /usr/local/bin/resize_fs
165 sleep 2
166 rm /etc/rc.local
167 mv /etc/rc.local.orig /etc/rc.local
168 fi
169 _EOF_
170
171 chmod +x /usr/local/bin/resize_fs > /dev/null 2>&1
172
173
174 REBOOT=1
175 echo "*********************************************"
176 echo "Rootfs Extended. Please REBOOT to take effect"
177 echo "*********************************************"
178 echo ""
179
180
181
182 Best regards,
183 Meino

Replies

Subject Author
Re: [gentoo-user] Creating an firmware image from a too large microSDcard...? Neil Bothwick <neil@××××××××××.uk>