在raspbian上扩展文件系统的命令是什么?

时间:2014-07-08 12:59:51

标签: filesystems raspberry-pi

我有很多覆盆子PI连接到分布在大面积上的各种网络,所以这必须是一个远程过程。我需要扩展文件系统,以便填充完整的8Gb(目前为2Gb)。我们使用Puppet来分发更新,但我不确定命令的过程是什么。

我知道这可以使用raspi-config在本地实现,但我需要创建一个脚本或发送命令来通过网络完成此操作。

1 个答案:

答案 0 :(得分:1)

raspi-config是一个shell脚本。有关内存扩展的部分如下所示。以下是基本步骤:

  1. 验证所需的扩展是在SD卡上,而不是在外部设备上,并且不超过两个普通分区。

  2. 确定确切的分区和所需的分区大小。 (分开)

  3. 更改分区表中分区的大小。 (这通常需要重启才能生效。)(fdisk)

  4. 将文件系统扩展到分区的完整大小(在上面的步骤3中调整了大小)。这被设置为在重新启动后运行的shell脚本。 (resize2fs)

  5. 由于SD卡的尺寸存在细微差别,即使是同一制造商的不同型号,也很难提供更具体的命令。

    #!/bin/sh
    # Part of raspi-config http://github.com/asb/raspi-config
    # ...
    
    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
    
    ROOT_PART=$(readlink /dev/root)
    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 rae probably using NOOBS, in which case your root filesystem is already expanded anyway." 20 60 2
      return 0
    fi
    
    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/mmdblk0 <<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
    ### 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