/**********************************************************************************
* raspi-config Expand root partition to fill SD card 原理
* 说明:
* 想知道树莓派是如何完成SD卡扩展的,记录相关文档。
*
* 2017-2-20 深圳 南山平山村 曾剑锋
********************************************************************************/ 一、参考文档:
. Raspbian: how to resize the root partition to fill SD card
https://coderwall.com/p/mhj8jw/raspbian-how-to-resize-the-root-partition-to-fill-sd-card
. raspi-config
https://github.com/asb/raspi-config/blob/master/raspi-config 二、raspi-config主要源代码有关SD卡扩展函数分析: do_expand_rootfs() {
# -h : FILE exists and is a symbolic link (same as -L)
if ! [ -h /dev/root ]; then
whiptail --msgbox "/dev/root does not exist or is not a symlink. Don't know how to expand"
return
fi # readlink用来找出符号链接所指向的位置。
ROOT_PART=$(readlink /dev/root)
# 从变量$string的开头, 删除最短匹配$substring的子串
PART_NUM=${ROOT_PART#mmcblk0p}
if [ "$PART_NUM" = "$ROOT_PART" ]; then
whiptail --msgbox "/dev/root is not an SD card. Don't know how to expand"
return
fi # NOTE: the NOOBS partition layout confuses parted. For now, let's only
# agree to work with a sufficiently simple partition layout
if [ "$PART_NUM" -ne ]; then
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."
return
fi # 再次确认分区表和设备节点提取的数值是否一致
#
# shell script:
# root@aplex:/home/zengjf/hacking# parted /dev/sdb -ms unit s p
# BYT;
# /dev/sdb:1953525168s:scsi:::gpt:ATA ST1000LM035-1RK1:;
# :2048s:943720448s:943718401s:ntfs:Basic data partition:msftdata;
# :943722496s:1953523711s:1009801216s:ntfs:Basic data partition:msftdata;
LAST_PART_NUM=$(parted /dev/mmcblk0 -ms unit s p | tail -n | cut -f -d:)
if [ "$LAST_PART_NUM" != "$PART_NUM" ]; then
whiptail --msgbox "/dev/root is not the last partition. Don't know how to expand"
return
fi # Get the starting offset of the root partition
# 获取文件系统分区起始位置
PART_START=$(parted /dev/mmcblk0 -ms unit s p | grep "^${PART_NUM}" | cut -f -d:)
[ "$PART_START" ] || return
# Return value will likely be error for fdisk as it fails to reload the
# partition table because the root fs is mounted
fdisk /dev/mmcblk0 <<EOF
p
d
$PART_NUM
n
p
$PART_NUM
$PART_START
p
w
EOF
ASK_TO_REBOOT= # now set up an init.d script,这里的&&是命令运行正常的与,和后面的程序一起组成命令运行链
cat <<\EOF > /etc/init.d/resize2fs_once &&
#!/bin/sh
### BEGIN INIT INFO
# Provides: resize2fs_once
# Required-Start:
# Required-Stop:
# Default-Start: S
# Default-Stop:
# Short-Description: Resize the root filesystem to fill partition
# Description:
### END INIT INFO
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting resize2fs_once" &&
resize2fs /dev/root && # 真正调整大小的地方
rm /etc/init.d/resize2fs_once && # 删除文件,表明该文件只能被运行一次
update-rc.d resize2fs_once remove &&
log_end_msg $?
;;
*)
echo "Usage: $0 start" >&
exit
;;
esac
EOF
chmod +x /etc/init.d/resize2fs_once && # 给出下次运行的权限
update-rc.d resize2fs_once defaults && # 默认运行
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "Root partition has been resized.\nThe filesystem will be enlarged upon the next reboot"
fi
}

raspi-config Expand root partition to fill SD card 原理的更多相关文章

  1. Resolving 'Root Partition Is Filling Up' Issue on Sophos UTM Firewall

    from: https://wandersick.blogspot.com/2016/06/resolving-root-partition-is-filling-up.html This is a ...

  2. OpenRisc-35-基于orpsoc,eCos的sd card controller的测试实验

    引言 之前,曾经在orpsoc的平台上,测试验证过其sd card controller的linux的驱动,但是并不是很完美,经过努力,终于在eCos下完成了其全部功能的验证,包括驱动层验证,文件系统 ...

  3. [OrangePi] Installation on SD Card

    Download any of the available images (xz archive) from Mega or GoogleDrive Download scriptbin_kernel ...

  4. Python Kivy writes / read the file on the SD card

    Path to SD card from jnius import autoclass # SDcard Android # Get path to SD card Android try: Envi ...

  5. [OrangePi] Backup internal EMMC to SD Card

    Boot your Orange PI board from EMMC without SD Card inserted login insert your SD Card Run: sudo ins ...

  6. I.MX6 uSDHC SD card register

    /**************************************************************************** * I.MX6 uSDHC SD card ...

  7. 【转】VirtualBox direct access to SD Card in Windows--不错

    原文网址:http://www.sandyscott.net/2013/08/14/virtualbox-direct-drive-access/ I’ve trying to get my Rasp ...

  8. SD card技术了解并WINCE下SDHC驱动开发(updated)

    Suumary: 简单介绍了一下SD卡的历史和发展,同时结合MX31 ADS上的WINCE 下SDHC驱动更深入的了解该硬件的一些行为特点. 了解SD card SD是Secure Digital C ...

  9. Using Android Phone to recover SD card formatted with DD command under linux

    Using Android Phone to recover SD card formatted with DD command under linux 1. Formatted a sd card ...

随机推荐

  1. ios cocoapods获取不到最新库的原因主要有两个:

    获取不到最新库的原因主要有两个: 1.cocoapods的版本过低 2.还没有更新本地仓库 解决方法: 1.更新pods 在终端执行命令行 sudo gem install cocoapods 执行完 ...

  2. Android hellocharts 柱形图详解

    近日需要做图表结构的项目,目前最火的就是hellocharts  和MPAndroidChart  相对来说hellocharts集成比较简单: 官网地址   https://github.com/l ...

  3. Unity 插件收集(持续更新)

    MGS Machinery Unity绑定机械关节,铰链,机构插件包.    MGS Mechanical Drive 用于绑定场景中的机械驱动器的Unity插件   Unity Wave Propa ...

  4. 我自己曾经经历的CMMI3认证通过关于软件测试的访谈【转载】

    转自<http://blog.csdn.net/jcy58/article/details/51908884> 因为当初我在公司里是负责软件测试工作的,所以CMMI3和测试相关的访谈,就是 ...

  5. Linux 服务器上建立用户并分配权限

    查看用户 whoami #要查看当前登录用户的用户名 who am i #表示打开当前伪终端的用户的用户名 who mom likes who 命令其它常用参数 参数 说明 -a 打印能打印的全部 - ...

  6. spring boot数据库操作汇总

    1 关于orm orm即object relational mapping,对象关系映射,即将数据库中的表映射成对象. 常用的orm有以下: mybatis spring jdbc template ...

  7. [Android]彻底去除Google AdMob广告

    应用中包含广告是能够理解的,但经常造成用户误点,或者广告切换时造成下载流量,就有点让人不舒服了. 以下就以Google AdMob广告为例,看怎样彻底去除他. 先分析一下Google AdMob的工作 ...

  8. SE16传输配置表数据

    SE16标准工具传输配置表数据 (上面说了配置表,所以并不是所有的表都能SE16传输.) 了解到: 1.如果是可维护的表,SE16,执行 然后:表条目->传输条目.选择请求: 2.对于不可维护的 ...

  9. python selenium cookie 登录

    概要: 1.正常登录,使用selenium获取cookie: 2.保存cookie: 3.使用cookie登录. 4.python--2.7,selenium--3.4.1 步骤1 正常登录,使用se ...

  10. MySQL查看和修改字符集的方法

    一.查看字符集 1.查看MYSQL数据库服务器和数据库字符集 方法一:show variables like '%character%';方法二:show variables like 'collat ...