# gpart: start '40' Invalid argument?



## fredvs (Aug 13, 2015)

Hello.

I use that script for formatting a new hard disk=>

```
#!/bin/sh
gpart create -s gpt da0

gpart add -t freebsd-boot -l gpboot -b 40 -s 512K da0

gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 da0

gpart add -t freebsd-ufs -l gprootfs -b 1M -s 2G da0

gpart add -t freebsd-swap -l gpswap -s 512M da0
gpart add -t freebsd-ufs  -l gpvarfs -s 1G da0
gpart add -t freebsd-ufs  -l gptmpfs -s 256M da0
gpart add -t freebsd-ufs  -l gpusrfs -a 1M da0

gpart show -l da0

newfs -U /dev/gpt/gprootfs
newfs -U /dev/gpt/gpvarfs
newfs -U /dev/gpt/gptmpfs
newfs -U /dev/gpt/gpusrfs
```

That code works perfectly on a Virtual system with VMWare.
The disk is formatted with all the partitions and accessible.

Sadly, the same code, with same configuration than VMWare but using VirtualBox gives that error =>


```
gpart: geom 'da0' exists

gpart: start '40'Invalid argument

gpart: start '2048': No space left on device
```

Why, with same config, I get that error with VirtualBox and all works like charm with VMWare ?

Many thanks to help.

Fre;D


----------



## alphaniner (Aug 13, 2015)

```
gpart: geom 'da0' exists
```

This indicates that da0 already has some sort of disklabel. The remaining errors are probably a consequence of that.


----------



## tobik@ (Aug 13, 2015)

I agree. Maybe add `gpart destroy -F da0` to the beginning of the script to (forcibly) destroy the previous disklabel.


----------



## alphaniner (Aug 13, 2015)

tobik said:


> I agree. Maybe add `gpart destroy -F da0` to the beginning of the script to (forcibly) destroy the previous disklabel.



Note that `gpart` will print an error message and exit with error status if no disklabel exists. You could just `2>/dev/null`, but for a script like this I highly recommend adding 'set -e' to have the script exit on any error. If you do that, you'd have to do something like

```
if gpart status da0 >/dev/null 2>&1; then
    gpart destroy da0
fi
```


----------



## fredvs (Aug 13, 2015)

tobik said:


> I agree. Maybe add  gpart destroy -F da0 to the beginning of the script to (forcibly) destroy the previous disklabel.



=> 
`gpart destroy -F da0`
Indeed, it does the trick but...

It does destroy other things too. ;-(

Here the things:

I have a bootabe iso.

With VMWare, I create a new guest, with a disk of 5 gigas, cdrom (the iso bootable) and a usb-memory-stick connected.

OK, it boots and then I did format the disk with the script of first topic + `gpart destroy -F da0` at beginning.

=> Perfect, the disk is formatted like wanted.

Then I do the same with VirtualBox, creating a new FreeBSD guest, with same bootable iso, with a disk of 5 gigas too and also with a usb-memory stick connected.

The boot is ok (much slower), but, when formatting the disk with same code, instead of formatting the disk, it has formatted the usb-memory-stick (with important things, of course)...

After check, with VirtualBox
ada0
is the hard disk and
da0
is the memory stick.


How to know if da0 or ada0 is the hard disk?
Why is it not the same with VirtualBox vs VMWare ?
And what in real life, on a real no virtual system ?

Thanks.


----------



## wblock@ (Aug 14, 2015)

da devices are dynamic.  Use `gpart show` or `gpart list` to make certain the device is really the right one.  `diskinfo` can also be used to identify the capacity and type of a drive.


----------



## kpa (Aug 14, 2015)

The difference is in what kind of driver is attached to the device. The da(4) devices are anything where an SCSI emulation (or they are real SCSI devices, becoming quite rare in consumer level systems) is used, all USB mass media devices fall into this category. The ada(4) driver is for SATA and IDE/ATA devices.


----------



## fredvs (Aug 14, 2015)

Hello and thanks to kpa and wblock@

Hum, does it exist tool to get the disks and removables of the system.
Or I must be creative and use `gpart show` or `gpart list` to create my own program for this ?

Thanks.


----------



## wblock@ (Aug 14, 2015)

`sysctl kern.disks`


----------



## fredvs (Aug 15, 2015)

wblock@ said:


> sysctl kern.disks



Yep, thanks. (and this one does not give too much blabla ;-) )

By the way, here custom script for formatting devices that is working on all systems tested. (advices highly welcome)

```
#!/bin/sh
clear
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo "  Installing system on device..."
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo "  Look at first column of the list and choose the device to use."
echo " "
echo "  It would be something like 'da0', 'ada0' or 'da1,...'"
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo "  Press [Enter] to see the list."
read something

clear

LINE="-------------------------------------------------------------------------------------"

echo "Device  Sectors  Size  Used"
echo ${LINE}

if [ -c "/dev/da0" ]
then
diskinfo da0
echo ${LINE}
fi

if [ -c "/dev/ada0" ]
then
diskinfo ada0
echo ${LINE}
fi

if [ -c "/dev/da1" ]
then
diskinfo da1
echo ${LINE}
fi

if [ -c "/dev/ada1" ]
then
diskinfo ada1
echo ${LINE}
fi

if [ -c "/dev/da2" ]
then
diskinfo da2
echo ${LINE}
fi

if [ -c "/dev/ada2" ]
then
diskinfo ada2
echo ${LINE}
fi

if [ -c "/dev/da3" ]
then
diskinfo da3
echo ${LINE}
fi

if [ -c "/dev/ada3" ]
then
diskinfo ada3
echo ${LINE}
fi

if [ -c "/dev/da4" ]
then
diskinfo da4
echo ${LINE}
fi

if [ -c "/dev/ada4" ]
then
diskinfo ada4
echo ${LINE}
fi

echo " "
echo " "
echo "Write the device to use + [Enter]."
echo "Example: da0"
echo " "
read dev

clear
echo "Create a new GPT partition on ${dev}."
echo "..."
gpart destroy -F ${dev}
gpart create -s gpt ${dev}

echo "Done."
echo "Create a new boot partition."
echo "..."

gpart add -t freebsd-boot -l gpboot -b 40 -s 512K ${dev}

echo "Done."
echo "Install the GPT bootcode."
echo "..."

gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 ${dev}

echo "Done."
echo "Create a partition for /."
echo "..."

gpart add -t freebsd-ufs -l gprootfs -b 1M -s 2G ${dev}

echo "Done"
echo "Create a partition for swap, /var, /tmp, /usr."
echo "..."

gpart add -t freebsd-swap -l gpswap -s 512M ${dev}
gpart add -t freebsd-ufs  -l gpvarfs -s 1G ${dev}
gpart add -t freebsd-ufs  -l gptmpfs -s 256M ${dev}
gpart add -t freebsd-ufs  -l gpusrfs -a 1M ${dev}

echo "Done."
echo "Show partition."
echo "..."

gpart show -l -p ${dev}

echo "Done."
echo "Format the new filesystems."
echo "..."

newfs -U /dev/gpt/gprootfs
newfs -U /dev/gpt/gpvarfs
newfs -U /dev/gpt/gptmpfs
newfs -U /dev/gpt/gpusrfs

echo "All format done."
echo "..."
```


----------

