千家信息网

Raspberry Pi OS禁止自动扩展tf卡与设置根分区大小的示例分析

发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,这篇文章将为大家详细讲解有关Raspberry Pi OS禁止自动扩展tf卡与设置根分区大小的示例分析,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。树
千家信息网最后更新 2025年02月04日Raspberry Pi OS禁止自动扩展tf卡与设置根分区大小的示例分析

这篇文章将为大家详细讲解有关Raspberry Pi OS禁止自动扩展tf卡与设置根分区大小的示例分析,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

树莓派在突然断电的时候很容易导致tf卡损坏,我需要另外创建一个分区用于保存数据,系统分区设置只读用于保护tf卡。

Linux上要消减一个分区大小是很难的,然而扩展一个分区容量比起来简单很多

Raspbian based on Debian Buster 在系统第一次启动的时候会执行脚本去扩展系统分区到整个tf卡的。如果需要另外创建分区,这个功能是需要禁用的。 Raspbian自动扩展tf卡的实现方式:

  • /boot/cmdline.txt 末尾init=/usr/lib/raspi-config/init_resize.sh,这个脚本修改了root分区的大小,使其扩展到真个tf卡

  • /etc/init.d/resize2fs_once 随机启动脚本会resize2fs 会修改文件系统大小, 并删除文件自身。

知道这个就很简单了,在烧录好tf卡之后,不要立即插入树莓派去运行,而应该在电脑上修改tf卡内容。

  1. 删除/boot/cmdline.txt 末尾init=/usr/lib/raspi-config/init_resize.sh 这一串

  2. 删除/etc/init.d/resize2fs_once 脚本中 含resize2fs $ROOT_DEV &&那一行

  3. fdisk 操作tf卡, 重新创建分区增加size 。我的电脑上是sudo fdisk /dev/sdb

sudo fdisk /dev/sdbWelcome to fdisk (util-linux 2.31.1).Changes will remain in memory only, until you decide to write them.Be careful before using the write command.Command (m for help): mHelp:  DOS (MBR)   a   toggle a bootable flag   b   edit nested BSD disklabel   c   toggle the dos compatibility flag  Generic   d   delete a partition   F   list free unpartitioned space   l   list known partition types   n   add a new partition   p   print the partition table   t   change a partition type   v   verify the partition table   i   print information about a partition  Misc   m   print this menu   u   change display/entry units   x   extra functionality (experts only)  Script   I   load disk layout from sfdisk script file   O   dump disk layout to sfdisk script file  Save & Exit   w   write table to disk and exit   q   quit without saving changes  Create a new label   g   create a new empty GPT partition table   G   create a new empty SGI (IRIX) partition table   o   create a new empty DOS partition table   s   create a new empty Sun partition tableCommand (m for help): pDisk /dev/sdb: 14.6 GiB, 15665725440 bytes, 30597120 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x2fed7feeDevice     Boot  Start     End Sectors  Size Id Type/dev/sdb1         8192  532479  524288  256M  c W95 FAT32 (LBA)/dev/sdb2       532480 3620863 3088384  1.5G 83 LinuxCommand (m for help): dPartition number (1,2, default 2): 2Partition 2 has been deleted.Command (m for help): nPartition type   p   primary (1 primary, 0 extended, 3 free)   e   extended (container for logical partitions)Select (default p): pPartition number (2-4, default 2): First sector (2048-30597119, default 2048): 532480Last sector, +sectors or +size{K,M,G,T,P} (532480-30597119, default 30597119): +6GCreated a new partition 2 of type 'Linux' and of size 6 GiB.Partition #2 contains a ext4 signature.Do you want to remove the signature? [Y]es/[N]o: nCommand (m for help): wThe partition table has been altered.Calling ioctl() to re-read partition table.Syncing disks.$ sudo e2fsck -f /dev/sdb2e2fsck 1.44.1 (24-Mar-2018)Pass 1: Checking inodes, blocks, and sizesPass 2: Checking directory structurePass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary informationrootfs: 44456/96576 files (0.2% non-contiguous), 292342/386048 blocks$ sudo resize2fs /dev/sdb2 6Gresize2fs 1.44.1 (24-Mar-2018)Resizing the filesystem on /dev/sdb2 to 1572864 (4k) blocks.The filesystem on /dev/sdb2 is now 1572864 (4k) blocks long.

现在就可以用这个tf卡启动树莓派了,root分区我只设置了6G。 进入raspbian可以再对剩余空间进行分区, cfdisk比较直观一些。

sudo cfdisk /dev/mmcblk0

之后看到df -h和fdisk -l的大小不一样用下面命令修复

sudo resize2fs /dev/mmcblk0p2

关于Raspberry Pi OS禁止自动扩展tf卡与设置根分区大小的示例分析就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

0