简体   繁体   English

Bash 使用 fdisk 命令编写磁盘分区脚本

[英]Bash Script disk partition using fdisk command

I have done new partition creation and mount the disk with manual interaction one after another, i got worked for me.我已经完成了新的分区创建并通过手动交互一个接一个地挂载磁盘,我为我工作。 But instead of doing manually i am planning to write the bash script to do all the changes for us without manual interaction.但是,我打算编写 bash 脚本来为我们完成所有更改,而不是手动进行,而无需手动交互。

I found the below script from internet, but i am not understand how the default option will behave using below sed command我从互联网上找到了以下脚本,但我不明白默认选项将如何使用下面的 sed 命令

# 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]*\).*/\1/' << 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

sudo fdisk /dev/sdc
p
n
1
p
w
lsblk
sudo mkfs -t ext4 /dev/sdc1

But after entering option p,n,1, i wanna make default for the rest of activity then finally i need write using option w.但是在输入选项 p,n,1 后,我想为活动的 rest 设置默认值,那么最后我需要使用选项 w 进行写入。

Can you please some one help me how to make it work你能请人帮我如何使它工作吗

code for sfdisk sfdisk 的代码

  echo ';' | sfdisk /dev/sdc
  mkfs.ext4 /dev/sdc1
  mount /dev/sdc1 /opt/app/
  uid=`blkid | grep sdc1 | sed -n 's/.*UUID=\"\([^\"]*\)\".*/\1/p'`
  echo "UUID=$uid /opt/app ext4  defaults  0  2" >> /etc/fstab

After executing sfdisk command after rebooting by REHL7 system not coming up back normal mode.在 REHL7 系统重新启动后执行 sfdisk 命令后,没有回到正常模式。

You can do this concept via regular fdisk this way:您可以通过以下方式通过常规 fdisk 执行此概念:

fdisk /dev/sdb << EOF
p
n
1



p
w
EOF

You merely need to know how many default values are needed.您只需要知道需要多少个默认值。 You can have more, without any major consequences (cosmetic ones will occur, however)您可以拥有更多,而不会产生任何重大后果(但是会发生化妆品)

n has a max of 4 options [number, first, last, remove_old_fstype] n 最多有 4 个选项 [number, first, last, remove_old_fstype]

so you'd add ( 4 - len(args) ) newlines, then proceed.所以你要添加( 4 - len(args) )换行符,然后继续。

PS n doesn't need 1 as the default argument ( as 1 is a default argument ), but the method I described above should work PS n 不需要 1 作为默认参数(因为 1 是默认参数),但我上面描述的方法应该可以工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM