bash 如何为多个分区编写 sfdisk 或 parted 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12150116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to script sfdisk or parted for multiple partitions?
提问by LabRat
For QA purposes I need to be able to partition a drive via a bash script up to 30 or more partitions for both RHEL and SLES.
出于 QA 目的,我需要能够通过 bash 脚本为 RHEL 和 SLES 将驱动器分区最多 30 个或更多分区。
I have attempted to do this in BASH with fdisk via a "here document" which works but as you can guess blows up in various steps. I assume this is because of timing of the input commands occurring at the wrong times and getting out of sync. 1 out of 10 times my script will work correctly.
我曾尝试通过“此处文档”在 BASH 中使用 fdisk 执行此操作,该文档有效,但您可以猜到在各个步骤中都失败了。我认为这是因为输入命令的时间发生在错误的时间并且不同步。我的脚本 10 次中有 1 次可以正常工作。
I have looked at parted and sfdisk and don't really understand how to use these tools.
我看过 parted 和 sfdisk 并不太了解如何使用这些工具。
I have only ever used fdisk.
我只用过 fdisk。
My issue is that with fdisk you can state something like "new partition +1gb" over and over and this works for me because in my script I don't need to keep track of prior partitions or remaining space or do any calculations. Every time I run this function this just makes an additional 1gb partition from any unused space.
我的问题是,使用 fdisk,您可以一遍又一遍地声明诸如“新分区 +1gb”之类的内容,这对我有用,因为在我的脚本中,我不需要跟踪先前的分区或剩余空间或进行任何计算。每次我运行这个函数时,它都会从任何未使用的空间中创建一个额外的 1gb 分区。
Is there a way to use parted or sfdisk (or any other tool that would already be a part of these distros) so that I could script a loop from 1 to x operations without having to do this accounting of remaining space? Does anyone have any examples they could share?
有没有办法使用 parted 或 sfdisk(或任何其他已经成为这些发行版一部分的工具),以便我可以编写从 1 到 x 操作的循环脚本,而不必计算剩余空间?有没有人有任何可以分享的例子?
Update
更新
Here is an example of one of my functions. At the start of the script we ask the user for the number of partitions to create, their size (this is static for all), and type of FS if any. This functions creates partition 1 thru 3 then a different function handles the extended (4th) and another handles 5th to nn.
这是我的功能之一的示例。在脚本开始时,我们询问用户要创建的分区数量、它们的大小(这是所有静态的)以及 FS 的类型(如果有)。此函数创建分区 1 到 3,然后一个不同的函数处理扩展(第 4 个)和另一个处理第 5 个到 nn。
As I said before, this script is fully functional; my problem is that at times the commands being sent to fdisk seem to arrive at the wrong timing, which then breaks the entire script thereby breaking any automation.
正如我之前所说,这个脚本功能齐全;我的问题是有时发送到 fdisk 的命令似乎到达错误的时间,这会破坏整个脚本,从而破坏任何自动化。
So the commands are being sent like this:
所以命令是这样发送的:
n
p
1
+1M
w
I have been reading up on fdisk and have learned it is not suited well for scripting so what I am seeing is that when in script mode, fdisk might be asking for p
my script already thinks it's time to send the 1
.
我一直在阅读 fdisk 并了解到它不适合编写脚本,所以我看到的是,在脚本模式下,fdisk 可能要求p
我的脚本已经认为是时候发送1
.
The thing about fdisk that worked for me is that after you specify the partition number it already calculated the next free sectorso all I have to do at this point is send a blank line for my start and then the +1M for my total size. Parted and sfdisk don't appear to work this way from what I can tell and I am still very new at this to understand how to automate those tools at this time.
关于 fdisk 对我有用的事情是,在您指定分区号后,它已经计算了下一个空闲扇区,所以此时我要做的就是发送一个空行作为我的开始,然后发送 +1M 作为我的总大小。从我所知,parted 和 sfdisk 似乎并没有以这种方式工作,而且我在这方面仍然很陌生,以了解此时如何自动化这些工具。
Create1to3Primary_Func() {
Size=\+$partSize\MB
for i in {1..3}
do
echo " this loop i= $i"
echo "Creating Partition $i on $targetFull as $targetFull$i using Create1to3Primary_Func()"
rm -f /tmp/myScript
echo -e "n" >> /tmp/myScript
echo -e "p" >> /tmp/myScript
echo -e "$i" >> /tmp/myScript
echo -e " " >> /tmp/myScript
echo -e "$Size" >> /tmp/myScript
echo -e "w" >> /tmp/myScript
echo -e "EOF" >> /tmp/myScript
fdisk $targetFull < /tmp/myScript
echo " sleeping Create1to3Primary_Func()"
sleep 4s
if [ "$RawOrFs" == "f" ]; then
mkfsCMD="mkfs.$fsType"
mkfsFullTarget="$targetFull$i"
cmdline="$mkfsCMD $mkfsFullTarget -L 'Partition$i'"
echo "Creating $fsType File System on $mkfsFullTarget"
$cmdline
fi
void="/mnt/mymnt$i"
if [ ! -d $void ] ; then
echo "Creating Mount Point /mnt/mymnt$i"
void="/mnt/mymnt$i"
mkdir $void
fi
echo "Part Probe on $targetFull "
partprobe $targetFull ; sleep 4s
done
}
回答by Stephane Rouberol
Not sure to get what you really want, but you may be interested by the fact that sfdisk
can dump a partition layout and use this layout to partition other disks. For instance:
不确定是否得到您真正想要的,但您可能会对sfdisk
可以转储分区布局并使用此布局对其他磁盘进行分区的事实感兴趣。例如:
sfdisk -d /dev/sda > mydiskpartitionslayout
Then in your script (take care of course) you can specify
然后在您的脚本中(当然要注意)您可以指定
sfdisk /dev/sdx < mydiskpartitionslayout
回答by metakermit
An approach I like (which I saw in this article) is to "script" the fdisk input directly, since it's smarter than sfdisk about creating a partition "until the end of the disk" or "2 GB large". Example:
我喜欢的一种方法(我在本文中看到)是直接对 fdisk 输入“编写脚本”,因为它比 sfdisk 在创建分区“直到磁盘结束”或“2 GB 大”方面更聪明。例子:
echo "d
1
d
2
d
3
n
p
1
+2G
n
p
2
w
" | fdisk /dev/sda
This script deletes up to 3 existing partitions, creates a 2 GB partition (e.g. swap) and then creates a partition that would extend over the remaining disk space.
此脚本最多删除 3 个现有分区,创建一个 2 GB 分区(例如交换分区),然后创建一个可扩展到剩余磁盘空间的分区。
In contrast, if a partition layout was created and used in sfdisk, the script would not cover the whole disk if more space was available.
相反,如果在 sfdisk 中创建并使用了分区布局,则如果有更多可用空间,脚本将不会覆盖整个磁盘。
回答by user128364
Automating repetitive task is a norm in the life of automation and we need a method to automatically provide answers to these programs if we are to include them in our script.
自动化重复性任务是自动化生活中的一种规范,如果我们要将它们包含在我们的脚本中,我们需要一种方法来自动为这些程序提供答案。
This is where a program called “Expect” steps in to automate. For Red Hat based systems, execute the below command to install "Expect"
这是一个名为“Expect”的程序介入以实现自动化的地方。对于基于 Red Hat 的系统,执行以下命令安装“Expect”
yum install expect
yum 安装期望
For Debian based or Ubuntu, execute the below command.
对于基于 Debian 或 Ubuntu,执行以下命令。
apt-get install expect
apt-get 安装期望
below is the expect script to create a partition /dev/sdc
下面是创建分区 /dev/sdc 的期望脚本
!/usr/bin/expect
!/usr/bin/期望
log_file -a "/tmp/expect.log"
log_file -a "/tmp/expect.log"
set timeout 600
设置超时 600
spawn /sbin/fdisk /dev/sdc
生成 /sbin/fdisk /dev/sdc
expect "Command (m for help): " { send "n\r" }
期望“命令(m寻求帮助):”{发送“n\r”}
expect "p primary partition (1-4)"
期望“p 主分区(1-4)”
expect "" { send "p\r" }
期望“”{发送“p\r”}
expect "Partition number (1-4): " { send "1\r" }
期望“分区号(1-4):”{发送“1\r”}
expect "First cylinder (1-133544, default 1): " { send "1\r" }
期望“第一个柱面(1-133544,默认为 1):” { 发送“1\r”}
expect ": " { send "\r" }
期望“:”{发送“\r”}
expect "Command (m for help): " { send "w\r" }
期望“命令(m寻求帮助):”{发送“w\r”}
interact
相互影响