Howto setup a disk using fdisk in shell script

Hello there,

For the people who execute an interactive commands like fdisk and it needs alot of user input, and you want to script it. Check this out. Below script is just an example, bit you can think of better ways to use this concept just as kickstart files.

The script will create new primary partition and change the filesystem tag to 83 which represents ext3 , it will save the new partition, and updates partition map

#!/bin/bash

cat <<EOF | fdisk /dev/sda
n
p
1

t
83
w
EOF
partprobe
Posted in Shell Scripting, System Administartion and tagged , , , , , , . Bookmark the permalink. RSS feed for this post. Leave a trackback.

6 Responses to Howto setup a disk using fdisk in shell script

  1. Vasilij Savin says:

    It is better to use cfdisk in this (CLI/shell) case.

  2. Adisorn says:

    I always wendor why you people make locating the root partition so complicated. Using the mount command makes it much easier.Look:admin:RAI$ mount/dev/disk0s1 on / (hfs, local, journaled)devfs on /dev (devfs, local, nobrowse)map -hosts on /net (autofs, nosuid, automounted, nobrowse)map auto_home on /home (autofs, automounted, nobrowse)the first line shows you that /dev/disk0s1 is your target (mounted on /). From there you derive that you have to install boot0 or /dev/rdisk0 and boot1h on /dev/rdisk0s1

    • Tom says:

      No luck with a reboot. Here’s my dukiitsl:/dev/disk00: GUID_partition_scheme 1.5TB disk01: EFI 209.7MB disk0s12: Apple_HFS snowleopard 1.5TB disk0s2(I had to change the name of the Snow Leopard dive to that because terminal wasn’t recognizing it properly with the caps and the space. If I dragged it to terminal, it would list the address as something like /Snow \ Leopard. I tried reformatting, etc. but no change. Maybe meaningless, but thought I’d mention it.)

  3. Alif says:

    In Linux, the above steps are really hefplul to create new partition. One must need to know about these codes and commands. But, in Mac there are so many third party applications to modify Mac partitions.I have used Stellar Partition manager software to add new partition and resize the the existing one.

  4. Imran ali says:

    Thanks Sir,

    The above steps are really helpful to me.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

Swedish Greys - a WordPress theme from Nordic Themepark.