Sycamore

Phoenix reborns from the ashe

0%

Jetson Nano SD卡扩容

烧写Jetson Nano的Linux系统的时候,往往会出现利用已有的Jetson Nano的系统镜像对新的SD卡进行烧写,来满足不同的Nano有同样的配置和环境。

此方法解决的是低内存卡的系统镜像烧写到高内存卡中出现的硬盘使用不满的情况。

用户自行烧写完成Jetson Nano的镜像后,需进行SD卡扩容,以完全利用SD卡存储空间。 其实Jetson Nano是自带扩容的程序的。脚本路径在/usr/lib/nvidia/resizefs/nvresizefs.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
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
#!/bin/bash

# Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# This is a script to resize partition and filesystem on the root partition
# This will consume all un-allocated sapce on SD card after boot.

set -e

function usage()
{
if [ -n "${1}" ]; then
echo "${1}"
fi

echo "Usage:"
echo "${script_name} [options]"
echo ""
echo "Available options are:"
echo ""
echo " -h | --help"
echo " Show this usage"
echo ""
echo " -c | --check"
echo " Check whether ${script_name} can be used on current platform"
echo ""
echo " -g | --get"
echo " Show APP partition size in MB"
echo ""
echo " -m | --max"
echo " Show APP partition maximum available size in MB"
echo ""
echo " -s <app_size> | --size <app_size>"
echo " Set APP partition size in MB"
echo ""
echo "Without any option means consume all unallocated"
echo "space on SD card."
echo ""
echo "Example:"
echo "${script_name}"
echo "${script_name} -s 16384"
}

function check_pre_req()
{
root_dev="$(sed -ne 's/.*\broot=\([^ ]*\)\b.*/\1/p' < /proc/cmdline)"

if [[ "${root_dev}" == *UUID* ]]; then
root_dev="$(/sbin/findfs ${root_dev})"
fi

if [[ "${root_dev}" == /dev/mmcblk* ]] || [[ "${root_dev}" == /dev/sd* ]]; then
block_dev="$(/bin/lsblk -no pkname ${root_dev})"
root_dev="$(echo $root_dev | sed 's/^.....//g')"
fi

if [ "${block_dev}" != "" ]; then
# Check whether APP partition is located at the end of the disk
root_start_sector="$(cat /sys/block/${block_dev}/${root_dev}/start)"
all_start_sectors="$(cat /sys/block/${block_dev}/*/start)"

is_last="true"
for start_sector in ${all_start_sectors}; do
if [[ "${start_sector}" -gt "${root_start_sector}" ]]; then
is_last="false"
break
fi
done
support_resizefs="${is_last}"
fi
}

function get_app_size()
{
partition_size="$(cat /sys/block/${block_dev}/${root_dev}/size)"
echo "$((partition_size/2/1024))"
}

function max_available_app_size()
{
# Move backup GPT header to end of disk
sgdisk --move-second-header "/dev/${block_dev}" > /dev/null 2>&1
app_start_sector="$(cat /sys/block/${block_dev}/${root_dev}/start)"
last_usable_sector="$(sgdisk -p /dev/${block_dev} | \
grep "last usable sector" | \
awk '{print $10}')"

echo "$(((last_usable_sector - app_start_sector + 1)/2/1024))"
}

function parse_args()
{
while [ -n "${1}" ]; do
case "${1}" in
-h | --help)
usage
exit 0
;;
-c | --check)
echo "${support_resizefs}"
exit 0
;;
-g | --get)
if [ "${support_resizefs}" = "false" ]; then
echo "ERROR: ${script_name} doesn't support this platform."
exit 1
fi
get_app_size
exit 0
;;
-m | --max)
if [ "${support_resizefs}" = "false" ]; then
echo "ERROR: ${script_name} doesn't support this platform."
exit 1
fi
max_available_app_size
exit 0
;;
-s | --size)
[ -n "${2}" ] || usage "Not enough parameters"
size="+${2}M"
shift 2
;;
*)
usage "Unknown option: ${1}"
exit 1
;;
esac
done
}

script_name="$(basename "${0}")"
support_resizefs="false"
size="0"
root_dev=""
block_dev=""

check_pre_req
parse_args "${@}"

if [ "${support_resizefs}" = "false" ]; then
echo "ERROR: ${script_name} doesn't support this platform."
exit 1
fi

# Move backup GPT header to end of disk
sgdisk --move-second-header "/dev/${block_dev}"

# Get partition information for root partition
partition_num="$(cat /sys/block/${block_dev}/${root_dev}/partition)"
partition_name="$(sgdisk -i ${partition_num} /dev/${block_dev} | \
grep "Partition name" | cut -d\' -f2)"

partition_type="$(sgdisk -i ${partition_num} /dev/${block_dev} | \
grep "Partition GUID code:" | cut -d' ' -f4)"

partition_uuid="$(sgdisk -i ${partition_num} /dev/${block_dev} | \
grep "Partition unique GUID:" | cut -d' ' -f4)"

partition_attr="$(sgdisk -i ${partition_num} /dev/${block_dev} | \
grep "Attribute flags:" | cut -d' ' -f3)"

# Get start sector of the root partition
start_sector="$(cat /sys/block/${block_dev}/${root_dev}/start)"

# Delete and re-create the root partition
# This will resize the root partition.
sgdisk -d "${partition_num}" \
-n "${partition_num}":"${start_sector}":"${size}" \
-c "${partition_num}":"${partition_name}" \
-t "${partition_num}":"${partition_type}" \
-u "${partition_num}":"${partition_uuid}" \
-A "${partition_num}":=:"${partition_attr}" \
"/dev/${block_dev}"

# Inform kernel and OS about change in partition table and root
# partition size
partprobe "/dev/${block_dev}"

# Resize filesystem on root partition to consume all un-allocated
# space on disk
resize2fs "/dev/${root_dev}"


给上述脚本增加权限后执行,然后重启即可完成扩容