11#! /bin/bash
22
33# ##
4- # update_image.sh <xxxx.axp> <builtin_files.tar.xz/builtin_files_dir> <delete_first_files.txt> <out_path> [rootfs_size]
4+ # update_image.sh <xxxx.axp> <builtin_files.tar.xz/builtin_files_dir> <delete_first_files.txt> <out_path>
55# ##
66
77set -e
@@ -13,11 +13,6 @@ out_path=$4
1313
1414rootfs_name=ubuntu_rootfs_sparse.ext4
1515bootfs_name=bootfs.fat32
16- rootfs_image_size=AUTO
17- # if rootf_size is not set, use default value
18- if [ -n " $5 " ]; then
19- rootfs_image_size=$5
20- fi
2116
2217# check root permition
2318if [ " $( id -u) " -ne 0 ]; then
@@ -98,15 +93,6 @@ if [ $? -ne 0 ]; then
9893 exit 1
9994fi
10095echo " Converting $axp_file to raw image done"
101- file_size=$( stat -c%s tmp2/rootfs.ext4.raw)
102- rootfs_image_size0=$(( $file_size / 1024 / 1024 )) M
103- resize_image=0
104- if [[ $rootfs_image_size == " AUTO" ]]; then
105- rootfs_image_size=$rootfs_image_size0
106- elif [[ $rootfs_image_size0 != " $rootfs_image_size " ]]; then
107- echo " will create new image with size $rootfs_image_size "
108- resize_image=1
109- fi
11096
11197echo " Mounting raw image to tmp2/rootfs"
11298mkdir -p tmp2/rootfs
@@ -143,32 +129,45 @@ while IFS= read -r line; do
143129 fi
144130done < " $delete_first_files "
145131
132+ # check image size
133+ # get tmp2/rootfs + builtin_files size, if > rootfs_image_size, update rootfs_image_size -> rootfs_image_size + 100M
134+ rootfs_image_size0=$( du -s --block-size=1M tmp2/rootfs | cut -f1)
135+ rootfs_image_size0=$(( $rootfs_image_size0 + $(du - s -- block- size= 1 M $builtin_files | cut - f1 )) )
136+ rootfs_image_size=$(( $rootfs_image_size0 + 300 ))
137+ echo " =============================="
138+ echo " rootfs_dir_size: $rootfs_image_size0 M"
139+ echo " final rootfs_image_size: $rootfs_image_size M"
140+ echo " =============================="
141+
146142# copy builtin_files rootfs
147143echo " Copying files from directory $builtin_files "
148144target_rootfs_path=tmp2/rootfs
149- if [[ " $resize_image " == " 1" ]]; then
150- mkdir tmp2/rootfs_new
151- cp -r tmp2/rootfs/* tmp2/rootfs_new/
152- target_rootfs_path=tmp2/rootfs_new
153- fi
145+ # copy to new dir
146+ rm -rf tmp2/rootfs_new
147+ mkdir tmp2/rootfs_new
148+ cp -rP tmp2/rootfs/* tmp2/rootfs_new/
149+ target_rootfs_path=tmp2/rootfs_new
150+ # copy rootfs files
154151find $builtin_files -mindepth 1 -maxdepth 1 -type d ! -name " boot" | while read -r dir; do
155- cp -r " $dir " " $target_rootfs_path "
152+ cp -rP " $dir " " $target_rootfs_path "
156153done
157154
158155# copy boot files
159156if [[ -e ${builtin_files} /boot ]]; then
160157 echo " Copying boot files from ${builtin_files} /boot..."
161158 mkdir -p tmp2/bootfs
162159 mount -o loop,rw tmp2/axp/$bootfs_name tmp2/bootfs
163- cp -rf ${builtin_files} /boot/* tmp2/bootfs/
160+ cp -rfP ${builtin_files} /boot/* tmp2/bootfs/
161+ sync
164162 umount tmp2/bootfs
165163 echo " Copying boot files done"
166164fi
167165
168166# convert raw image to ext4 sparse image
169167echo " Making sparse rootfs ..."
170168rm -f tmp2/axp/${rootfs_name}
171- ./make_ext4fs/make_ext4fs -s -l $rootfs_image_size tmp2/axp/${rootfs_name} $target_rootfs_path
169+ ./make_ext4fs/make_ext4fs -s -l ${rootfs_image_size} M tmp2/axp/${rootfs_name} $target_rootfs_path
170+ sync
172171echo " Making sparse rootfs done"
173172
174173# unmount rootfs
0 commit comments