如何使用parted编写自动任务脚本(更新gpt表)

问题描述 投票:0回答:3

我想编写 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
linux bash scripting parted
3个回答
5
投票
printf "fix\n" | parted ---pretend-input-tty /dev/vda print

4
投票

我找到了一个解决方案,使用

sgdisk
代替
parted
,看起来编写脚本方便多了。就我而言,
sgdisk /dev/vda -e
成功了


0
投票

我用了这个脚本

        rm -f /tmp/parted_info
        parted --script /dev/$block_device p 2>&1 | tee -a /tmp/parted_info
        # gpt warning
        if grep -Eq 'fix the GPT' /tmp/parted_info;then
            echo -e "OK\nFix\n" | parted ---pretend-input-tty /dev/$block_device print 1>/dev/null
            rm -f /tmp/parted_info
        fi
© www.soinside.com 2019 - 2024. All rights reserved.