Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit e6958c7

Browse files
committed
SD expand
1 parent 139be94 commit e6958c7

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
NO.|文件名称|摘要
1414
:--:|:--|:--
15+
0022| [SD卡自动扩容](docs/0022_SD卡自动扩容.md) | 理解SD卡自动扩容工作原理
1516
0021| [wav声音PCM数据分析](docs/0021_wav声音PCM数据分析.md) | 通过wav文件了解PCM数据
1617
0020| [Android_Binder通信](docs/0020_Android_Binder通信.md) | 在树莓派中理解Android Binder通信
1718
0019| [USB_OTG虚拟串口](docs/0019_USB_OTG虚拟串口.md) | USB OTG虚拟串口,可用于学习串口通信

docs/0022_SD卡自动扩容.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# SD卡自动扩容
2+
3+
理解SD卡自动扩容工作原理
4+
5+
## steps
6+
7+
* sudo raspi-config
8+
* Advanced Options
9+
* Expand Filesystem
10+
* /usr/bin/raspi-config
11+
```sh
12+
do_expand_rootfs() {
13+
# -h : FILE exists and is a symbolic link (same as -L)
14+
if ! [ -h /dev/root ]; then
15+
whiptail --msgbox "/dev/root does not exist or is not a symlink. Don't know how to expand" 20 60 2
16+
return 0
17+
fi
18+
19+
# readlink用来找出符号链接所指向的位置。
20+
ROOT_PART=$(readlink /dev/root)
21+
# 从变量$string的开头, 删除最短匹配$substring的子串
22+
PART_NUM=${ROOT_PART#mmcblk0p}
23+
if [ "$PART_NUM" = "$ROOT_PART" ]; then
24+
whiptail --msgbox "/dev/root is not an SD card. Don't know how to expand" 20 60 2
25+
return 0
26+
fi
27+
28+
# NOTE: the NOOBS partition layout confuses parted. For now, let's only
29+
# agree to work with a sufficiently simple partition layout
30+
if [ "$PART_NUM" -ne 2 ]; then
31+
whiptail --msgbox "Your partition layout is not currently supported by this tool. You are probably using NOOBS, in which case your root filesystem is already expanded anyway." 20 60 2
32+
return 0
33+
fi
34+
35+
# 再次确认分区表和设备节点提取的数值是否一致
36+
#
37+
# shell script:
38+
# root@zengjf:/home/zengjf/hacking# parted /dev/sdb -ms unit s p
39+
# BYT;
40+
# /dev/sdb:1953525168s:scsi:512:4096:gpt:ATA ST1000LM035-1RK1:;
41+
# 1:2048s:943720448s:943718401s:ntfs:Basic data partition:msftdata;
42+
# 2:943722496s:1953523711s:1009801216s:ntfs:Basic data partition:msftdata;
43+
LAST_PART_NUM=$(parted /dev/mmcblk0 -ms unit s p | tail -n 1 | cut -f 1 -d:)
44+
if [ "$LAST_PART_NUM" != "$PART_NUM" ]; then
45+
whiptail --msgbox "/dev/root is not the last partition. Don't know how to expand" 20 60 2
46+
return 0
47+
fi
48+
49+
# Get the starting offset of the root partition
50+
# 获取文件系统分区起始位置
51+
PART_START=$(parted /dev/mmcblk0 -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d:)
52+
[ "$PART_START" ] || return 1
53+
# Return value will likely be error for fdisk as it fails to reload the
54+
# partition table because the root fs is mounted
55+
fdisk /dev/mmcblk0 <<EOF
56+
p
57+
d
58+
$PART_NUM
59+
n
60+
p
61+
$PART_NUM
62+
$PART_START
63+
64+
p
65+
w
66+
EOF
67+
ASK_TO_REBOOT=1
68+
69+
# now set up an init.d script
70+
cat <<\EOF > /etc/init.d/resize2fs_once &&
71+
#!/bin/sh
72+
### BEGIN INIT INFO
73+
# Provides: resize2fs_once
74+
# Required-Start:
75+
# Required-Stop:
76+
# Default-Start: 2 3 4 5 S
77+
# Default-Stop:
78+
# Short-Description: Resize the root filesystem to fill partition
79+
# Description:
80+
### END INIT INFO
81+
. /lib/lsb/init-functions
82+
case "$1" in
83+
start)
84+
log_daemon_msg "Starting resize2fs_once" &&
85+
resize2fs /dev/root && # 真正调整大小的地方
86+
rm /etc/init.d/resize2fs_once && # 删除文件,表明该文件只能被运行一次
87+
update-rc.d resize2fs_once remove &&
88+
log_end_msg $?
89+
;;
90+
*)
91+
echo "Usage: $0 start" >&2
92+
exit 3
93+
;;
94+
esac
95+
EOF
96+
chmod +x /etc/init.d/resize2fs_once && # 给出下次运行的权限
97+
update-rc.d resize2fs_once defaults && # 默认运行
98+
if [ "$INTERACTIVE" = True ]; then
99+
whiptail --msgbox "Root partition has been resized.\nThe filesystem will be enlarged upon the next reboot" 20 60 2
100+
fi
101+
}
102+
```

0 commit comments

Comments
 (0)