1 Star 0 Fork 0

huryer / RaspberryPi

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0022_SD卡自动扩容.md 3.69 KB
一键复制 编辑 原始数据 按行查看 历史
zengjf 提交于 2021-03-21 07:48 . learn

SD卡自动扩容

理解SD卡自动扩容工作原理

补充内容

  • 刚烧录完系统时候,第一次启动时候,init进程执行的是resize的脚本
  • /boot/cmdline.txt
    • console=serial1,115200 console=tty1 root=PARTUUID=1d1c0dec-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet init=/usr/lib/raspi-config/init_resize.sh splash plymouth.ignore-serial-consoles
      • init=/usr/lib/raspi-config/init_resize.sh

steps

  • sudo raspi-config
    • Advanced Options
      • Expand Filesystem
  • /usr/bin/raspi-config
    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" 20 60 2
        return 0
      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" 20 60 2
        return 0
      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 2 ]; 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." 20 60 2
        return 0
      fi
    
      # 再次确认分区表和设备节点提取的数值是否一致
      # 
      # shell script:
      #   root@zengjf:/home/zengjf/hacking# parted /dev/sdb -ms unit s p 
      #   BYT;
      #   /dev/sdb:1953525168s:scsi:512:4096:gpt:ATA ST1000LM035-1RK1:;
      #   1:2048s:943720448s:943718401s:ntfs:Basic data partition:msftdata;
      #   2:943722496s:1953523711s:1009801216s:ntfs:Basic data partition:msftdata;
      LAST_PART_NUM=$(parted /dev/mmcblk0 -ms unit s p | tail -n 1 | cut -f 1 -d:)
      if [ "$LAST_PART_NUM" != "$PART_NUM" ]; then
        whiptail --msgbox "/dev/root is not the last partition. Don't know how to expand" 20 60 2
        return 0
      fi
    
      # Get the starting offset of the root partition
      # 获取文件系统分区起始位置
      PART_START=$(parted /dev/mmcblk0 -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d:)
      [ "$PART_START" ] || return 1
      # 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=1
    
      # 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: 2 3 4 5 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" >&2
        exit 3
        ;;
    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" 20 60 2
      fi
    }
1
https://gitee.com/huryer/RaspberryPi.git
git@gitee.com:huryer/RaspberryPi.git
huryer
RaspberryPi
RaspberryPi
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891