I am trying to partition my disk into two equal partitions. So far I was doing everything manually using fdisk command and then modifying fstab file with UUID for both the partitions and I have around 70 machines to do this. Is there any automated way I can do this using shell script? I don't want to do everything manually on each of these boxes.

Filesystem      Size  Used Avail Use% Mounted on
/dev/vda         30G  1.4G   27G   5% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev             60G   12K   60G   1% /dev
tmpfs            12G  384K   12G   1% /run
none            5.0M     0  5.0M   0% /run/lock
none             60G     0   60G   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/vdb        2.4T   72M  2.2T   1% /mnt

I want to partition /dev/vdb into two equal partitions /data01 and /data02. What is the best way to do this using everything in one shell script? I have a root access to all these machines and I have Ubuntu 14.04 box.

  • Do all the disks have the same size, or do you want to just split whatever is there in half? What partition scheme do you want to use: native Linux (LVM), backward-compatible with other OSes but limited to 2TB (MBR), or compatible with recent OSes and able to work with >2TB (GPT)? Commented Mar 6, 2016 at 21:39
  • @Gilles Yes I assume all the disks have same size but I would say let's split whatever is there in two equal half just to be on safe side. Now regarding your second question, I have no clue what does that mean and how to figure out as I am very much beginner in to Unix world. I have few other machines in which I already did partition manually earlier. If there is any way to figure this out, I will be glad to tell you.
    – david
    Commented Mar 6, 2016 at 21:46

2 Answers 2


If this is purely for use by Linux (as it seems to be since these are virtual disks in KVM), use LVM. LVM is easier to use and more flexible than native partitions, at the cost of not being understood by OSes other than Linux. Here's the gist of what you need, without any error checking. Create a physical volume spanning exactly the disk, create a volume group containing just that physical volume, and create two logical volumes containing half the space each (I express the size of the last volume as whatever is still free so as not to run into rounding issues).

pvcreate /dev/vdb
vgcreate mydisk /dev/vdb
lvcreate -n data01 -l 50%VG mydisk
lvcreate -n data02 -l 100%FREE mydisk
echo '/dev/mydisk/data01 /data01 auto defaults' >>/etc/fstab
echo '/dev/mydisk/data02 /data02 auto defaults' >>/etc/fstab
  • When I ran the first line pvcreate /dev/vdb I got this message Device /dev/vdb not found (or ignored by filtering). Not sure what does this mean?
    – david
    Commented Mar 7, 2016 at 0:14
  • LVM has a filter of devices that it should consider. This helps, for example, to avoid making it scan network block devices, or disks that are spun down, if you know these devices aren't going to be used for LVM. The default filter accepts every block device, but it seems your system has a default filter. The setting is in /etc/lvm/lvm.conf. Look at what's currently there to figure out how to change it sensibly, maybe add a|^/dev/vdb$| to the list. Commented Mar 7, 2016 at 0:28
  • Ok so here is my lvm.conf file. What I should modify here? Sorry for asking silly question. Since I don't know much about lvm.conf file so wanted to understand what specifically I need to modify to make this work.
    – david
    Commented Mar 7, 2016 at 1:56
  • @david I'm sorry, I don't understand what's going wrong. I see filter = [ "a/.*/" ] and that means “accept everything” (like the comment above says). Are you sure that /dev/vdb does exist? It'll need to exist, no matter what method you use. What's the output of ls -l /dev/vd*? Commented Mar 7, 2016 at 9:11
  • This is the output I get brw-rw---- 1 root disk 253, 0 Mar 3 18:33 /dev/vda brw-rw---- 1 root disk 253, 16 Mar 6 14:15 /dev/vdb brw-rw---- 1 root disk 253, 32 Mar 3 18:33 /dev/vdc
    – david
    Commented Mar 7, 2016 at 14:54

Assuming your disk sector size is 512 bytes, i think this could do the trick (you will need sfdisk)

SIZE=$(( ($(sfdisk -s $DISK) * 2 - $OFFSET) / 2 ))
POS_MAX_P1=$(( $SIZE + $OFFSET - 1 ))
POS_MIN_P2=$(( $POS_MAX_P1 + 1 ))
PART_TYPE=83     // Here you can change the type of the partitions


echo -e $LAYOUT | sfdisk $DISK
partprobe $DISK
echo -e "/dev/sdb1\t/data01\tauto\t0 1\n/dev/sdb2\t/data02\tauto\t0 1\n" >> /etc/fstab

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .