2011-09-05 21:38:11 +0000 2011-09-05 21:38:11 +0000
67
67
Advertisement

如何使用bash脚本创建和格式化一个分区?

Advertisement

有什么方法可以用bash脚本创建和格式化一个分区?

我想可以用fdisk来完成,但我不知道如何将bash脚本的命令输入fdisk shell,然后退出fdisk shell。

我想在bash中创建一个分区,然后格式化为ntfs。

Advertisement
Advertisement

答案 (10)

72
72
72
2015-10-10 03:03:55 +0000

和之前的建议类似,将命令管道化到fidsk,我发现这种方法很有用,可以为后续的维护者留下细节。sed位在fdisk获取输入之前就把所有的注释剥离出来。

# to create the partitions programatically (rather than manually)
# we're going to simulate the manual input to fdisk
# The sed script strips off all the comments so that we can 
# document what we're doing in-line with the actual commands
# Note that a blank line (commented as "defualt" will send a empty
# line terminated with a newline to take the fdisk default.
sed -e 's/\s*\([0-9a-zA-Z]*\).*//' << EOF | fdisk ${TGTDEV}
  o # clear the in memory partition table
  n # new partition
  p # primary partition
  1 # partition number 1
    # default - start at beginning of disk 
  +100M # 100 MB boot parttion
  n # new partition
  p # primary partition
  2 # partion number 2
    # default, start immediately after preceding partition
    # default, extend partition to end of disk
  a # make a partition bootable
  1 # bootable partition is partition 1 -- /dev/sda1
  p # print the in-memory partition table
  w # write the partition table
  q # and we're done
EOF
68
68
68
2016-10-08 17:30:51 +0000

sfdisk

sfdiskfdisk 的脚本版本 它是util-linux 的一部分,和fdisk一样,所以可用性应该是一样的。

可以用以下方法创建一个有单个分区的分区表,占用整个磁盘。

echo 'type=83' | sudo sfdisk /dev/sdX

更复杂的分区表将在下面解释。

要生成一个示例脚本,请获取一个磁盘的设置。

sudo sfdisk -d /dev/sda > sda.sfdisk

在我的联想T430 Windows 7 / Ubuntu双启动上的输出示例。

label: dos
label-id: 0x7ddcbf7d
device: /dev/sda
unit: sectors

/dev/sda1 : start= 2048, size= 3072000, type=7, bootable
/dev/sda2 : start= 3074048, size= 195430105, type=7
/dev/sda3 : start= 948099072, size= 28672000, type=7
/dev/sda4 : start= 198504446, size= 749594626, type=5
/dev/sda5 : start= 198504448, size= 618891264, type=83
/dev/sda6 : start= 940277760, size= 7821312, type=82
/dev/sda7 : start= 817397760, size= 61437952, type=83
/dev/sda8 : start= 878837760, size= 61437500, type=83

一旦你把脚本保存到文件中,你就可以把它应用到sdX

sudo sfdisk /dev/sdX < sda.sfdisk

对于sfdisk的输入,你可以省略设备名称,使用类型为:

start= 2048, size= 3072000, type=7, bootable

如果存在的话,它们会被忽略,而设备名称则从命令行参数中获取。

一些解释。

fdisk也可以用sfdisk命令读取I脚本,在交互式的fdisk会话中 “源 "它们,允许你在写分区之前进一步定制。

在Ubuntu 16.04、sfdisk 2.27.1上测试。

在没有sudo的情况下,格式化并填充分区的映像文件

这是一个学习使用sfdisk而不破坏硬盘的好方法。 https://stackoverflow.com/questions/10949169/how-to-create-a-multi-partition-sd-disk-image-without-root-privileges/52850819#52850819

56
Advertisement
56
56
2011-09-06 03:34:53 +0000
Advertisement

fdiskstdin读取,所以你只需要给它输入适当的命令。例如,下面的命令是清除分区表(如果有的话),并创建一个新的分区表,其中有一个是整个磁盘的分区。

(
echo o # Create a new empty DOS partition table
echo n # Add a new partition
echo p # Primary partition
echo 1 # Partition number
echo # First sector (Accept default: 1)
echo # Last sector (Accept default: varies)
echo w # Write changes
) | sudo fdisk

我建议你执行你想要的任务,记录你输入的内容,这样你就可以重现它。

37
37
37
2014-04-09 12:53:18 +0000

你只需要几条命令就可以做到,用引子/n代替多个回声。

echo -e "o\nn\np\n1\n\n\nw" | fdisk /dev/sda
5
Advertisement
5
5
2015-02-25 15:44:20 +0000
Advertisement

如同其他人所解释的那样,向fdisk传输命令的工作原理很好,但这种方式更优雅,更易读。

fdisk /dev/sdc <<EOF
n
p
1

w
EOF

从(临时)文件中传输命令也可以。

fdisk /dev/sdc < /tmp/fdisk.cmds
5
5
5
2012-06-08 15:42:12 +0000

你可以用脚本fdisk.

(echo n; echo p; echo 1; echo 1; echo 200; echo w) | fdisk /dev/sdc

这将在/dev/sdc上创建一个200 MB的分区。

5
Advertisement
5
5
2017-10-06 19:03:35 +0000
Advertisement

创建一个新的磁盘标签类型gpt。

sudo /sbin/parted /dev/xvdf mklabel gpt --script

对磁盘进行分区。

sudo /sbin/parted /dev/xvdf mkpart primary 0% 100% --script

1
1
1
2016-03-23 19:29:01 +0000
printf 'o\nn\np\n1\n\n\nw' | fdisk /dev/sda
0
Advertisement
0
0
2017-07-14 12:07:51 +0000
Advertisement

脚本使用fdisk根据别人的答案创建分区。

在脚本中修改以下内容。

NUM_PARTITIONS =5

PARTITION_SIZE =“+10G”

脚本后面给出了使用示例。

#!/bin/bash
if [$# -eq 0]
then
  echo "input the device"
  exit
fi

NUM_PARTITIONS=5
PARTITION_SIZE="+10G"    

SED_STRING="o"
TAIL="p
w
q
"

NEW_LINE="
"
LETTER_n="n"
EXTENDED_PART_NUM=4
TGTDEV=$1

SED_STRING="$SED_STRING$NEW_LINE"
for i in $(seq $NUM_PARTITIONS)
do
  if [$i -lt $EXTENDED_PART_NUM]
  then
    SED_STRING="$SED_STRING$LETTER_n$NEW_LINE$NEW_LINE$NEW_LINE$NEW_LINE$PARTITION_SIZE$NEW_LINE"
  fi
  if [$i -eq $EXTENDED_PART_NUM]
  then
    SED_STRING="$SED_STRING$LETTER_n$NEW_LINE$NEW_LINE$NEW_LINE$NEW_LINE"
    SED_STRING="$SED_STRING$LETTER_n$NEW_LINE$NEW_LINE$PARTITION_SIZE$NEW_LINE"
  fi
  if [$i -gt $EXTENDED_PART_NUM]
  then
    SED_STRING="$SED_STRING$LETTER_n$NEW_LINE$NEW_LINE$PARTITION_SIZE$NEW_LINE"
  fi
done
SED_STRING="$SED_STRING$TAIL"

sed -e 's/\s*\([0-9a-zA-Z]*\).*//' << EOF | fdisk ${TGTDEV}
  $SED_STRING
EOF

运行时。

sudo sh mk_partition.sh /dev/sdxxx

-2
-2
-2
2016-07-15 12:49:49 +0000
#!/bin/sh
hdd="/dev/hda /dev/hdb /dev/hdc"
for i in $hdd;do
echo "n
p
1

w
"|fdisk $i;mkfs.ext3 "$i1";done

你可以使用这个脚本。只要确保你在定义hdd的地方定义了自己的分区。在1w之间有2个空格,它们需要在那里取默认值。

Advertisement