CenturionStudio.it - Fotolia

Will a 'create Linux partition' script work?

I want to write a shell script that contains commands for Linux partition creation. Can I automate everything so the user only inputs the partition size and details?

You can create Linux partition scripts in Red Hat Enterprise Linux and other distributions, but perhaps not with the first utility that comes to mind.

Partitioning a Linux disk protects file systems, reduces the file system check time and lets you mount partitions as read-only.

You probably tried to create the Linux partitioning script at the fdisk (fixed disk) utility, because it was designed to partition hard disks. However, this utility doesn't work very well with command-line parameters. It was developed to work with an interactive menu interface.

The addpart utility works much better for scripting your Linux partition generator. It informs the Linux kernel of new partitions, and it works with four different arguments: the name of the device you want to use it on, the number of the partition, the sector where the partition should start, and the last sector of the partition. Begin and end sectors are expressed in multiples of 512 bytes.

Let's use an example command to create a partition. The command addpart /dev/sdc 1 2048 2097152 creates a primary partition with approximately 1 GB of capacity on /dev/sdc.

Using the addpart command properly requires accurate information about the current partitioning on the disk. Here is where the fdisk –l command comes in. Pay particular attention to the number of the last sector that is available on the disk. To obtain this number, use the command setup fdisk –l /dev/yourdevice | head –n 2 | tail –n 1 | awk ‘{ print $7 }'.

About the Author:
Sander van Vugt is an independent trainer and consultant based in the Netherlands. He is an expert in Linux high availability, virtualization and performance. He has authored many books on Linux topics, such as, Beginning the Linux Command Line, Beginning Ubuntu LTS Server Administration and Pro Ubuntu Server Administration.

Next Steps

Linux server troubleshooting ranges from finding problems in scripts to protecting against attacks. Knowing the best utilities and performance tools for the job is half the battle.

Have a question for Linux experts about troubleshooting, commands, or another topic? Submit an expert question here.

Dig Deeper on Linux servers