0

I have a script which partition the directory into two equal partition. I am partitioning /mnt directory into two equal partitions /data01 and /data02.

david@machine:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda         30G  1.2G   27G   5% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev             26G   12K   26G   1% /dev
tmpfs           5.2G  372K  5.2G   1% /run
none            5.0M     0  5.0M   0% /run/lock
none             26G     0   26G   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/vdb        276G   63M  262G   1% /mnt

Below is my script:

#!/bin/bash -x
#
#

# Comment the mnt from fstab
umount /mnt
sed -i '{
s#^/dev/vdb#\#/dev/vdb#
}' /etc/fstab


echo '# partition table of /dev/vdb
unit: sectors

/dev/vdb1 : start=     2048, size=2147481600, Id=83
/dev/vdb2 : start=2147483648, size=2147483647, Id=83
/dev/vdb3 : start=        0, size=        0, Id= 0
/dev/vdb4 : start=        0, size=        0, Id= 0


'  | sfdisk /dev/vdb

# Create the links since it over 2TB disk
partprobe

# make file system
mkfs -t ext4 /dev/vdb1
mkfs -t ext4 /dev/vdb2



# mkdir dst dirs
mkdir /data01 /data02


D1=`blkid /dev/vdb1  | awk '{print $2}'`
D2=`blkid /dev/vdb2  | awk '{print $2}'`

echo "$D1   /data01 ext4    rw,noatime,nodiratime 0 2"  >> /etc/fstab
echo "$D2   /data02 ext4    rw,noatime,nodiratime 0 2"  >> /etc/fstab

mount /data01
mount /data02

When I ran the above script, it unmounted successfully /mnt directory but it failed afterwards with this error message. Same script worked fine on other machine I had so looks like something is different in this box which I am not considering it in my script and because of that it is failing.

Below is the full debug messages:

+ umount /mnt
+ sed -i '{
s#^/dev/vdb#\#/dev/vdb#
}' /etc/fstab
+ echo '# partition table of /dev/vdb
unit: sectors

/dev/vdb1 : start=     2048, size=2147481600, Id=83
/dev/vdb2 : start=2147483648, size=2147483647, Id=83
/dev/vdb3 : start=        0, size=        0, Id= 0
/dev/vdb4 : start=        0, size=        0, Id= 0


+ sfdisk /dev/vdb
'
Checking that no-one is using this disk right now ...
OK

Disk /dev/vdb: 582542 cylinders, 16 heads, 63 sectors/track

sfdisk: ERROR: sector 0 does not have an msdos signature
 /dev/vdb: unrecognized partition table type
Old situation:
No partitions found
Warning: given size (2147481600) exceeds max allowable size (587200512)

sfdisk: bad input
+ partprobe
+ mkfs -t ext4 /dev/vdb1
mke2fs 1.42.9 (4-Feb-2014)
Could not stat /dev/vdb1 --- No such file or directory

The device apparently does not exist; did you specify it correctly?
+ mkfs -t ext4 /dev/vdb2
mke2fs 1.42.9 (4-Feb-2014)
Could not stat /dev/vdb2 --- No such file or directory

The device apparently does not exist; did you specify it correctly?
+ mkdir /data01 /data02
++ blkid /dev/vdb1
++ awk '{print $2}'
+ D1=
++ blkid /dev/vdb2
++ awk '{print $2}'
+ D2=
+ echo '   /data01 ext4    rw,noatime,nodiratime 0 2'
+ echo '   /data02 ext4    rw,noatime,nodiratime 0 2'
+ mount /data01
mount: mount point ext4 does not exist
+ mount /data02
mount: mount point ext4 does not exist

What is wrong I did? How to fix this now? Looks like I messed up something.

david
  • 109
  • 2

1 Answers1

1

You need a partition table in order to make partitions on a disk. Currently, you're using a filesytem directly on the block device. You'll need to first put an MBR or GPT onto this base "disk" before you can put partitions on that table. Your script attempts to do this, but sfdisk fails with invalid input.

Your sfdisk input is out of range, and your first partition is more than a terabyte in size on a disk that's not more than 300 gigs, and sfdisk will balk at that righteously.

The following is an example of a healthy sfdisk output:

# sfdisk -d /dev/sda
label: dos
label-id: 0x8b33f739
device: /dev/sda
unit: sectors

/dev/sda1 : start=        2048, size=    14704640, type=83

This starts at the 2KiB boundary, and ends at roughly 7GiB. There is only one partition. The values in sfdisk are sectors, so you need to be sure if your disk is using a 512B or 4KiB sector size. You can adjust these values manually to your liking.

Spooler
  • 7,016
  • 16
  • 29
  • It did worked on my other box so I started using it as an automation instead of doing it manually. What should I do to fix this? – david Nov 17 '16 at 19:31
  • I think it was hardcoded basis on other box I had earlier. May be we don't need that in this box. How can I figure this thing out for this box? – david Nov 17 '16 at 19:35
  • That's your problem. These boxen aren't equivalent. If you look at your sfdisk values, you're WAY out of range. Using 512 byte sector sizes, the size of your first partition is more than a terabyte. You need to change these values. You also don't want those empty partitions. I'll edit my answer in a minute with a reasonable configuration, and you can tune that as needed. – Spooler Nov 17 '16 at 20:01
  • Got it. Also if I have to revert back the damage my script has done is so I just need to unmount back the "/mnt" directory? That's all. – david Nov 17 '16 at 21:04
  • It shouldn't have actually touched your disk in this case, as sfdisk refused to, so you should be able to just remount the old stuff. – Spooler Nov 17 '16 at 21:13