GNU 在脚本中拆分 resizepart

Xio*_*345 7 partition shell-script parted

我想使用该yes命令,以便 GNU parted 不等待用户输入:

root@195-xxx-xxx-xxx:/proc# parted /dev/sda unit B resizepart 2 1166016512B
 Warning: Shrinking a partition can cause data loss, are you sure you want 
 to continue?
Yes/No? y                                                                 
Information: You may need to update /etc/fstab.
root@195-xxx-xxx-xxx:/proc# echo $?
0
Run Code Online (Sandbox Code Playgroud)

但是使用yes在这里不起作用:

root@195-xxx-xxx-xxx:/proc# yes | parted /dev/sda unit B resizepart 2 166016512B
 Warning: Shrinking a partition can cause data loss, are you sure you 
 want to continue?
root@195-xxx-xxx-xxx:/proc# echo $?
1
Run Code Online (Sandbox Code Playgroud)

编辑:

--script选项也不起作用:

root@195-xxx-xxx-xxx:/proc# parted --script /dev/sda unit B resizepart 2 1166016512B
 Warning: Shrinking a partition can cause data loss, are you sure you 
 want to continue?
root@195-xxx-xxx-xxx:/proc# echo $?
1
Run Code Online (Sandbox Code Playgroud)

小智 6

就我而言,我在无 tty 的 SSH 会话中使用 Parted 3.2。“是”命令 hack 不起作用,因为分离的代码具有以下测试:

    /* script-mode: don't handle the exception */
    if (opt_script_mode || (!isatty (0) && !pretend_input_tty))
            return PED_EXCEPTION_UNHANDLED;
Run Code Online (Sandbox Code Playgroud)

请注意 'isatty' 测试,它将失败。'pretend_input_tty' 是一个未公开的命令行选项,可以通过 ---pretend-input-tty 开启。

因此,如果您想从脚本中使用 parted,我的回答如下:

/sbin/parted -a optimal /dev/loop1 ---pretend-input-tty resizepart 4 Yes 522239s
Run Code Online (Sandbox Code Playgroud)

请注意假输入 tty 前面的三个破折号。我认为这应该把我们吓跑。虽然不确定。


fro*_*utz 5

如果resizepart不起作用,您可能不得不求助于rmmkpart实现相同的目标。

当然,这需要您先解析分区表以确定分区类型和起始偏移量。除非您已经知道必要的值。毕竟,您也必须166016512B从某个地方获取。

parted可以--machine选择生成易于解析的输出。另一方面,实际解析它的例子并不容易找到。;)


小智 5

可以使用以下命令来使用 GNU parted 调整大小:

echo yes | parted /dev/sda ---pretend-input-tty resizepart 2 100GB
Run Code Online (Sandbox Code Playgroud)