【问题标题】:How to script automatic task with parted (update gpt table)如何使用 parted 编写自动任务脚本(更新 gpt 表)
【发布时间】:2019-08-14 14:16:22
【问题描述】:

我想在我的 Debian 9.9 上编写现有分区的扩展脚本。

我可以通过 parted 的交互模式简单地做到这一点,但我想自动化它。在下面的示例中,我只需要手动编写“修复”,但我想编写脚本。

root@localhost:~# parted -l
Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/backup: 38.9GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0.00B  38.9GB  38.9GB  xfs


Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/data: 38.9GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0.00B  38.9GB  38.9GB  xfs


Warning: Not all of the space available to /dev/vda appears to be used, you can
fix the GPT to use all of the space (an extra 20971520 blocks) or continue with
the current setting? 
Fix/Ignore?                    

我想知道当我输入“Fix”时执行了哪个命令。据我了解,这可能只是 GPT 表的重新加载,所以我尝试执行命令 partprobe:

root@localhost:~# partprobe 
Warning: Not all of the space available to /dev/vda appears to be used, you can fix the GPT to use all of the space (an extra 20971520 blocks) or continue with the current setting? 
root@localhost:~# 

但这里没有提出任何选项。我查看了 parted 的帮助,有一个“脚本模式”,我尝试了以下方法,但没有成功:

root@localhost:~# parted -s /dev/vda print Fix
Warning: Not all of the space available to /dev/vda appears to be used, you can fix the GPT to use all of the space (an extra 20971520 blocks) or continue with the current setting? 
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 118GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      17.4kB  1018kB  1000kB                     bios_grub
 2      1018kB  451MB   450MB   ext3
 3      451MB   10.5GB  10.0GB  xfs
 8      29.6GB  107GB   77.8GB

Usage: parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...]
Apply COMMANDs with PARAMETERS to DEVICE.  If no COMMAND(s) are given, run in
interactive mode.

OPTIONs:
  -h, --help                      displays this help message
  -l, --list                      lists partition layout on all block devices
  -m, --machine                   displays machine parseable output
  -s, --script                    never prompts for user intervention
  -v, --version                   displays the version
  -a, --align=[none|cyl|min|opt]  alignment for new partitions

COMMANDs:
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
[...]

我也试过这个 bash 脚本:

#!/bin/bash
(echo Fix; echo print list; echo quit) | parted /dev/vda print free

但它没有用,我也试过这个但它没有帮助:

**root@localhost:~# cat /tmp/2.sh** 

select /dev/vda
print 
Fix

然后通过管道将其导入parted:

parted < /tmp/2.sh

【问题讨论】:

  • @Cyrus 抱歉,完成了

标签: linux bash scripting parted


【解决方案1】:

我找到了一个解决方案,使用'sgdisk'而不是parted,它看起来更方便编写脚本。就我而言,“sgdisk /dev/vda -e”成功了

【讨论】:

  • 哦!男人! THNQ!
【解决方案2】:
printf "fix\n" | parted ---pretend-input-tty /dev/vda print

【讨论】:

  • 嗨丽莎,欢迎来到 stackoverflow。即使您的答案有效,您是否愿意详细说明它在做什么以及为什么会有所帮助?
  • 欢迎来到 Stack Overflow!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的答案以添加解释并说明适用的限制和假设。 From Review
  • 必须喜欢无证选项。记录在案的--script 使parted 向所有人回答no。这使您可以真正覆盖安全检查,就像您对脚本所期望的那样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 2011-08-14
相关资源
最近更新 更多