forked from friendlyarm/sd-fuse_s5p6818
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-rootfs-img.sh
executable file
·117 lines (108 loc) · 3.69 KB
/
build-rootfs-img.sh
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
#!/bin/bash
set -eu
if [ $# -lt 2 ]; then
echo "Usage: $0 <rootfs dir> <img filename> "
echo "example:"
echo " tar xvzf NETDISK/S5P6818/rootfs/rootfs-friendlycore-arm64.tgz"
echo " ./build-rootfs-img.sh friendlycore-arm64/rootfs friendlycore-arm64"
exit 0
fi
ROOTFS_DIR=$1
TARGET_OS=$(echo ${2,,}|sed 's/\///g')
IMG_FILE=$TARGET_OS/rootfs.img
if [ $# -eq 3 ]; then
IMG_SIZE=$3
else
IMG_SIZE=0
fi
# ----------------------------------------------------------
# Get host machine arch
HOST_ARCH=
if uname -mpi | grep aarch64 >/dev/null; then
HOST_ARCH="aarch64/"
fi
TOP=$PWD
export MKE2FS_CONFIG="${TOP}/tools/mke2fs.conf"
if [ ! -f ${MKE2FS_CONFIG} ]; then
echo "error: ${MKE2FS_CONFIG} not found."
exit 1
fi
true ${MKFS:="${TOP}/tools/${HOST_ARCH}mke2fs"}
if [ ! -d ${ROOTFS_DIR} ]; then
echo "path '${ROOTFS_DIR}' not found."
exit 1
fi
# Automatically re-run script under sudo if not root
if [ $(id -u) -ne 0 ]; then
echo "Re-running script under sudo..."
sudo --preserve-env "$0" "$@"
exit
fi
MKFS_OPTS="-E android_sparse -t ext4 -L rootfs -M /root -b 4096"
case ${TARGET_OS} in
friendlywrt* | buildroot*)
# set default uid/gid to 0
MKFS_OPTS="-0 ${MKFS_OPTS}"
;;
*)
;;
esac
clean_rootfs() {
(cd $1 && {
# remove machine-id, the macaddress will be gen via it
[ -f etc/machine-id ] && > etc/machine-id
[ -f var/lib/dbus/machine-id ] && {
rm -f var/lib/dbus/machine-id
ln -s /etc/machine-id var/lib/dbus/machine-id
}
rm -f etc/friendlyelec-release
rm -f root/running-state-file
rm -f etc/firstuse
rm -f var/lib/dpkg/lock
rm -f var/lib/dpkg/lock-frontend
rm -f var/cache/apt/archives/lock
rm -f var/cache/apt/archives/*.deb
[ -f etc/udev/rules.d/70-persistent-net.rules ] && cat /dev/null > etc/udev/rules.d/70-persistent-net.rules
[ -d ./tmp ] && find ./tmp -exec rm -rf {} +
mkdir -p ./tmp
chmod 1777 ./tmp
if [ -d ./var/lib/apt/lists ]; then
PERM=`grep "^_apt" ./etc/passwd | cut -d':' -f3`
if [ -e ./var/lib/apt/lists ]; then
[ -z ${PERM} ] || chown -R ${PERM}.0 ./var/lib/apt/lists
fi
if [ -e ./var/cache/apt/archives/partial ]; then
[ -z ${PERM} ] || chown -R ${PERM}.0 ./var/cache/apt/archives/partial
fi
fi
[ -d var/log ] && find var/log -type f -delete
[ -d var/tmp ] && find var/tmp -type f -delete
find -name .bash_history -type f -exec cp /dev/null {} \;
[ -e var/lib/systemd ] && touch var/lib/systemd/clock
[ -e var/lib/private/systemd/timesync ] && touch var/lib/private/systemd/timesync/clock
if [ -d var/lib/NetworkManager/ ]; then
rm -fr var/lib/NetworkManager/dhclient*
rm -fr var/lib/NetworkManager/secret_key
rm -fr var/lib/NetworkManager/timestamps
fi
(cd dev && find . ! -type d -exec rm {} \;)
})
}
clean_rootfs ${ROOTFS_DIR}
if [ ${IMG_SIZE} -le 0 ]; then
# calc image size
IMG_SIZE=$(((`du -s -B64M ${ROOTFS_DIR} | cut -f1` + 3) * 1024 * 1024 * 64))
IMG_BLK=$((${IMG_SIZE} / 4096))
INODE_SIZE=$((`find ${ROOTFS_DIR} | wc -l` + 128))
# make fs
[ -f ${IMG_FILE} ] && rm -f ${IMG_FILE}
${MKFS} -N ${INODE_SIZE} ${MKFS_OPTS} -d ${ROOTFS_DIR} ${IMG_FILE} ${IMG_BLK}
else
IMG_BLK=$((${IMG_SIZE} / 4096))
INODE_SIZE=$((`find ${ROOTFS_DIR} | wc -l` + 128))
[ -f ${IMG_FILE} ] && rm -f ${IMG_FILE}
${MKFS} -N ${INODE_SIZE} ${MKFS_OPTS} -d ${ROOTFS_DIR} ${IMG_FILE} ${IMG_BLK}
fi
${TOP}/tools/generate-partmap-txt.sh ${IMG_SIZE} ${TARGET_OS}
echo "generating ${IMG_FILE} done."
echo 0