Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script with fdisk

Tags:

linux

bash

I came across this bash script to expand the fs after making the root volume larger on an ami made with Packer. Can someone please explain the meaning of the fdisk options in the heredoc?

#!/bin/bash
fdisk /dev/xvda <<EEOF
d
n
p
1
1

w
EEOF
exit 0

Thank you!

like image 343
Ele Munjeli Avatar asked Oct 19 '22 16:10

Ele Munjeli


2 Answers

To determine what these mean, look at the built-in help from fdisk. Details may differ based on your implementation; for mine, that looks like this:

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

...so:

  • d deletes a partition (presumably your script was developed for a version of fdisk where if there's only one partition, there's no prompt over which to delete).
  • n creates a new partition.
    • p indicates that it's a primary partition being created.
    • 1 indicates that it should be primary partition #1
    • 1 indicates that it should start at sector #1
    • the following blank line accepts the default end sector
  • w writes changes to disk.
like image 137
Charles Duffy Avatar answered Oct 21 '22 05:10

Charles Duffy


Try this and adjust for your conditions:

#!/bin/bash

HEREDOC_VAR_1='p q ' 
echo $HEREDOC_VAR_1

HEREDOC_VAR_2='n q ' 
echo $HEREDOC_VAR_2

echo "$HEREDOC_VAR_1" | fdisk /dev/xvda
echo "$HEREDOC_VAR_2" | fdisk /dev/xvda
like image 35
Jose Carlos Ramos Carmenates Avatar answered Oct 21 '22 07:10

Jose Carlos Ramos Carmenates