1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# DisplayName: Jolla f8331/@ARCH@ (release) 1
# KickstartType: release
# DeviceModel: f8331
# DeviceVariant: sony-tone
# SuggestedFeatures: customer-jolla, sailfish-xt9-feature, sailfish-eas, aliendalvik-feature
# SuggestedImageType: loop
# SuggestedArchitecture: armv7hl

lang en_US.UTF-8
timezone --utc UTC
keyboard us
user --name nemo --groups audio,input,video --password nemo

### Commands from /tmp/sandbox/usr/share/ssu/kickstart/part/f8331
part / --fstype="ext4" --size=1800 --label=root
part /home --fstype="ext4" --size=800 --label=home
#part /fimage --fstype="ext4" --size=10 --label=fimage

## No suitable configuration found in /tmp/sandbox/usr/share/ssu/kickstart/bootloader

repo --name=adaptation-community-f8331-@RELEASE@ --baseurl=file:///home/me/dev/android/omni/droid-local-repo/kagura
repo --name=adaptation-community-common-f8331-@RELEASE@ --baseurl=http://repo.merproject.org/obs/nemo:/devel:/hw:/common/sailfish_latest_@ARCH@/
repo --name=apps-@RELEASE@ --baseurl=https://releases.jolla.com/jolla-apps/@RELEASE@/@ARCH@/
repo --name=customer-jolla-@RELEASE@ --baseurl=https://releases.jolla.com/features/@RELEASE@/customers/jolla/@ARCH@/
repo --name=hotfixes-@RELEASE@ --baseurl=https://releases.jolla.com/releases/@RELEASE@/hotfixes/@ARCH@/
repo --name=jolla-@RELEASE@ --baseurl=https://releases.jolla.com/releases/@RELEASE@/jolla/@ARCH@/

# @Jolla Configuration f8331
# jolla-configuration-f8331
%packages
@jolla-configuration-f8331
%end

%attachment
### Commands from /tmp/sandbox/usr/share/ssu/kickstart/attachment/f8331
/boot/hybris-boot.img
/boot/hybris-recovery.img
droid-config-f8331-out-of-image-files
droid-flashing-tools
/etc/hw-release
%end

%pre
export SSU_RELEASE_TYPE=release
### begin 01_init
touch $INSTALL_ROOT/.bootstrap
### end 01_init
%end

%post
export SSU_RELEASE_TYPE=release
### begin 01_arch-hack
if [ "@ARCH@" == armv7hl ] || [ "@ARCH@" == armv7tnhl ]; then
    # Without this line the rpm does not get the architecture right.
    echo -n "@ARCH@-meego-linux" > /etc/rpm/platform

    # Also libzypp has problems in autodetecting the architecture so we force tha as well.
    # https://bugs.meego.com/show_bug.cgi?id=11484
    echo "arch = @ARCH@" >> /etc/zypp/zypp.conf
fi
### end 01_arch-hack
### begin 01_rpm-rebuilddb
# Rebuild db using target's rpm
echo -n "Rebuilding db using target rpm.."
rm -f /var/lib/rpm/__db*
rpm --rebuilddb
echo "done"
### end 01_rpm-rebuilddb
### begin 50_oneshot
# exit boostrap mode
rm -f /.bootstrap

# export some important variables until there's a better solution
export LANG=en_US.UTF-8
export LC_COLLATE=en_US.UTF-8
export GSETTINGS_BACKEND=gconf

# run the oneshot triggers for root and first user uid
UID_MIN=$(grep "^UID_MIN" /etc/login.defs |  tr -s " " | cut -d " " -f2)
DEVICEUSER=`getent passwd $UID_MIN | sed 's/:.*//'`

if [ -x /usr/bin/oneshot ]; then
   su -c "/usr/bin/oneshot --mic"
   su -c "/usr/bin/oneshot --mic" $DEVICEUSER
fi
### end 50_oneshot
### begin 60_ssu
if [ "$SSU_RELEASE_TYPE" = "rnd" ]; then
    [ -n "@RNDRELEASE@" ] && ssu release -r @RNDRELEASE@
    [ -n "@RNDFLAVOUR@" ] && ssu flavour @RNDFLAVOUR@
    # RELEASE is reused in RND setups with parallel release structures
    # this makes sure that an image created from such a structure updates from there
    [ -n "@RELEASE@" ] && ssu set update-version @RELEASE@
    ssu mode 2
else
    [ -n "@RELEASE@" ] && ssu release @RELEASE@
    ssu mode 4
fi
### end 60_ssu
### begin 70_sdk-domain

export SSU_DOMAIN=@RNDFLAVOUR@

if [ "$SSU_RELEASE_TYPE" = "release" ] && [[ "$SSU_DOMAIN" = "public-sdk" ]];
then
    ssu domain sailfish
fi
### end 70_sdk-domain
%end

%post --nochroot
export SSU_RELEASE_TYPE=release
### begin 50_os-release
(
CUSTOMERS=$(find $INSTALL_ROOT/usr/share/ssu/features.d -name 'customer-*.ini' \
    |xargs --no-run-if-empty sed -n 's/^name[[:space:]]*=[[:space:]]*//p')

cat $INSTALL_ROOT/etc/os-release
echo "SAILFISH_CUSTOMER=\"${CUSTOMERS//$'\n'/ }\""
) > $IMG_OUT_DIR/os-release
### end 50_os-release
%end

#pack --nochroot
%pack
# TODO FIXME: All of this needs to run INSIDE scratchbox!!!!

export SSU_RELEASE_TYPE=release
### begin hybris
pushd $IMG_OUT_DIR

MD5SUMFILE=md5.lst

DEVICE_VERSION_FILE=./hw-release

if [ -n "@EXTRA_NAME@" ] && [ "@EXTRA_NAME@" != @"EXTRA_NAME"@ ]; then
  EXTRA_NAME="@EXTRA_NAME@-"
fi

if [[ -a $DEVICE_VERSION_FILE ]]; then
  source $DEVICE_VERSION_FILE
  DEVICE_ID=-${MER_HA_DEVICE// /_}-$VERSION_ID
fi

source ./os-release
if [ "$SSU_RELEASE_TYPE" = "rnd" ]; then
  RND_FLAVOUR=$SAILFISH_FLAVOUR
fi

RELEASENAME=${NAME// /_}-${SAILFISH_CUSTOMER// /_}${SAILFISH_CUSTOMER:+-}${EXTRA_NAME// /_}$RND_FLAVOUR${RND_FLAVOUR:+-}$VERSION_ID$DEVICE_ID

# FIXME: Fuck it, just re-extract the tar.bz2 image, don't wanna mess around
# with mic now
tar -xjvf sfe-kagura-3.0.1.11-heroic.tar.bz2

# Remove /opt mounting from fstab, systemd will handle it
# TODO: Not needed on tone as we have no fake /vendor partition to spare
#mkdir root
#mount -o loop root.img root
#sed -i '/\/opt/d' root/etc/fstab
#umount root
#rm -rf root

IMGSECTORS=0
IMGBLOCKS=0
IMGSIZE=0
BLOCKSIZE=0

resizeloop() {
  local IMG=$1
  local LOOP=$(/sbin/losetup -f)
  /sbin/losetup $LOOP $IMG
  /sbin/e2fsck -f -y $LOOP

  # Get image blocks and free blocks
  local BLOCKCOUNT=$(/sbin/dumpe2fs -h $LOOP 2>&1 | grep "Block count:" | grep -o -E '[0-9]+')
  local FREEBLOCKS=$(/sbin/dumpe2fs -h $LOOP 2>&1 | grep "Free blocks:" | grep -o -E '[0-9]+')
  BLOCKSIZE=$(/sbin/dumpe2fs -h $LOOP 2>&1 | grep "Block size:" | grep -o -E '[0-9]+')
  echo "$IMG block size: $BLOCKSIZE - total block count: $BLOCKCOUNT - Free blocks: $FREEBLOCKS"

  local IMAGEBLOCKS=$(/usr/bin/expr $BLOCKCOUNT - $FREEBLOCKS)
  local IMAGESECTORS=$(/usr/bin/expr $IMAGEBLOCKS \* $BLOCKSIZE / 512 )

  # Shrink to minimum
  echo "Shrink $IMG to $IMAGESECTORS"
  #/sbin/resize2fs $LOOP ${IMAGESECTORS}s -f
  # Fuck it, just let resize2fs do its magic
  /sbin/resize2fs $LOOP -f

  # Get the size after resize.
  IMGBLOCKS=$(/sbin/dumpe2fs -h $LOOP 2>&1 | grep "Block count:" | grep -o -E '[0-9]+')
  IMGSIZE=$(/usr/bin/expr $IMGBLOCKS \* $BLOCKSIZE)
  IMGSECTORS=$(/usr/bin/expr $IMGBLOCKS \* $BLOCKSIZE / 512)
  echo "$IMG resized block count: $IMGBLOCKS - Block size: $BLOCKSIZE - Sectors: $IMGSECTORS - Total size: $IMGSIZE"

  /sbin/losetup -d $LOOP
}

resizeloop_home() {
  local IMG=$1
  local LOOP=$(/sbin/losetup -f)
  /sbin/losetup $LOOP $IMG
  /sbin/e2fsck -f -y $LOOP

  # Get the size after resize.
  IMGBLOCKS=$(/sbin/dumpe2fs -h $LOOP 2>&1 | grep "Block count:" | grep -o -E '[0-9]+')
  IMGSIZE=$(/usr/bin/expr $IMGBLOCKS \* $BLOCKSIZE)
  IMGSECTORS=$(/usr/bin/expr $IMGBLOCKS \* $BLOCKSIZE / 512)
  echo "$IMG resized block count: $IMGBLOCKS - Block size: $BLOCKSIZE - Sectors: $IMGSECTORS - Total size: $IMGSIZE"

  /sbin/losetup -d $LOOP
}

echo "Resize root and home"

# Resize root and home to minimum
resizeloop root.img

ROOTSIZE=$IMGSIZE
ROOTBLOCKS=$IMGBLOCKS
ROOTSECTORS=$IMGSECTORS

resizeloop_home home.img

HOMESIZE=$IMGSIZE
HOMEBLOCKS=$IMGBLOCKS
HOMESECTORS=$IMGSECTORS

# We will add some (100M) extra to temp image.
TEMPIMGSECTORS=$(/usr/bin/expr $ROOTSECTORS + $HOMESECTORS + 200000 )

dd if=/dev/zero bs=512 count=0 of=temp.img seek=$TEMPIMGSECTORS

LVM_LOOP=$(/sbin/losetup -f)
/sbin/losetup $LVM_LOOP temp.img
/usr/sbin/pvcreate $LVM_LOOP
/usr/sbin/vgcreate sailfish $LVM_LOOP

echo "Create logical volume ROOT size: $ROOTSIZE"
/usr/sbin/lvcreate -L ${ROOTSIZE}B --name root sailfish

echo "Create logical volume HOME size: $HOMESIZE"
/usr/sbin/lvcreate -L ${HOMESIZE}B --name home sailfish

/bin/sync
/usr/sbin/vgchange -a y sailfish

dd if=root.img bs=$BLOCKSIZE count=$ROOTBLOCKS of=/dev/sailfish/root

dd if=home.img bs=$BLOCKSIZE count=$HOMEBLOCKS of=/dev/sailfish/home

/usr/sbin/vgchange -a n sailfish

pigz -7 root.img && md5sum -b root.img.gz > root.img.gz.md5&
pigz -7 home.img && md5sum -b home.img.gz > home.img.gz.md5&
wait

ROOTSIZE=$(/bin/ls -l root.img.gz | cut -d ' ' -f5)
HOMESIZE=$(/bin/ls -l home.img.gz | cut -d ' ' -f5)

# Temporary dir for making factory image backups.
FIMAGE_TEMP=$(mktemp -d -p $(pwd))

# For some reason loop files created by imager don't shrink properly when
# running resize2fs -M on them. Hence manually growing the loop file here
# to make the shrinking work once we have the image populated.
dd if=/dev/zero bs=512 seek=$(/usr/bin/expr \( $ROOTSIZE + $HOMESIZE + 100000000 \) / 512) count=1 of=fimage.img
/sbin/e2fsck -f -y fimage.img
/sbin/resize2fs -f fimage.img

mount -o loop fimage.img $FIMAGE_TEMP
mkdir -p $FIMAGE_TEMP/${RELEASENAME}
mv root.img.gz* $FIMAGE_TEMP/${RELEASENAME}
mv home.img.gz* $FIMAGE_TEMP/${RELEASENAME}
umount $FIMAGE_TEMP
rmdir $FIMAGE_TEMP

/sbin/e2fsck -f -y fimage.img
/sbin/resize2fs -f -M fimage.img

# To make the file magic right lets convert to single file sparse image.
/usr/bin/img2simg fimage.img fimage.img001
#rm fimage.img

/sbin/losetup -d $LVM_LOOP

mv temp.img sailfish.img

/usr/bin/atruncate sailfish.img
# To make the file magic right lets convert to single file sparse image.
/usr/bin/img2simg sailfish.img sailfish.img001
#rm sailfish.img

# To make the file magic right lets convert to single file sparse image.
# TODO: Not needed on tone as /vendor is inside system image
#/usr/bin/img2simg vendor.img vendor.img001
#rm vendor.img

chmod 755 flash.*

FILES="flash* *.img* *.exe *.dll *-release"
FILES_TO_COPY="*.urls"

# Set supported devices in flash scripts
# TODO: This makes no sense... also, no windows for now, so leave it as is
if [ "@DEVICEMODEL@" = "h3113" ]; then
  sed -e "s/\@DEVICES\@/\-e \"H3113\" -e \"H3123\" -e \"H3133\"/g" -i flash.sh
  sed -e "s/\@DEVICES\@/:: H3113\r\n:: H3123\r\n:: H3133/g" -i flash-on-windows.bat
elif [ "@DEVICEMODEL@" = "h3213" ]; then
  sed -e "s/\@DEVICES\@/\-e \"H3213\" -e \"H3223\"/g" -i flash.sh
  sed -e "s/\@DEVICES\@/:: H3213\r\n:: H3223/g" -i flash-on-windows.bat
elif [ "@DEVICEMODEL@" = "h3413" ]; then
  sed -e "s/\@DEVICES\@/\-e \"H3413\"/g" -i flash.sh
  sed -e "s/\@DEVICES\@/:: H3413/g" -i flash-on-windows.bat
elif [ "@DEVICEMODEL@" = "h4113" ]; then
  sed -e "s/\@DEVICES\@/\-e \"H4113\" -e \"H4133\"/g" -i flash.sh
  sed -e "s/\@DEVICES\@/:: H4113\r\n:: H4133/g" -i flash-on-windows.bat
elif [ "@DEVICEMODEL@" = "h4213" ]; then
  sed -e "s/\@DEVICES\@/\-e \"H4213\" -e \"H4233\"/g" -i flash.sh
  sed -e "s/\@DEVICES\@/:: H4213\r\n:: H4233/g" -i flash-on-windows.bat
elif [ "@DEVICEMODEL@" = "h4413" ]; then
  sed -e "s/\@DEVICES\@/\-e \"H4413\" -e \"H4493\"/g" -i flash.sh
  sed -e "s/\@DEVICES\@/:: H4413\r\n:: H4493/g" -i flash-on-windows.bat
fi

mkdir -p ${RELEASENAME}
cp ${FILES_TO_COPY} ${RELEASENAME}/

mv ${FILES} ${RELEASENAME}/

# Calculate md5sums of files included to the archive
cd ${RELEASENAME}
md5sum * > $MD5SUMFILE
cd ..

# Package stuff back to archive
zip -r ${RELEASENAME}.zip $RELEASENAME

# Remove the files from the output directory
rm -r ${RELEASENAME}

popd
### end hybris
%end