# Curious memstick layout for 9.0



## jem (Jan 5, 2012)

I've been examining the new memstick install image for 9.0 and noticed that its partition and filesystem layout has changed from the 8.2 image.

With 8.2, the memstick image contained a dedicated bsdlabel, with a single 'a' partition defined.  The 'a' partition had the usual offset of 16 blocks to leave space for the boot code.

Here's how file(1) identifies it:

```
FreeBSD-8.2-RELEASE-amd64-memstick.img: x86 boot sector;
  partition 4: ID=0xa5, active, starthead 0, startsector 0,
  50000 sectors, code offset 0x3c, BSD disklabel
```
and here's what bsdlabel(8) makes of it:

```
# FreeBSD-8.2-RELEASE-amd64-memstick.img:
8 partitions:
#        size   offset    fstype   [fsize bsize bps/cpg]
  a:  2124544       16    unused        0     0       
  c:  2124560        0    unused        0     0         # "raw" part, don't edit
```
I have successfully utilised that same layout when creating my own bootable FreeBSD sticks.

With the 9.0 memstick image file(1) and bsdlabel(8) see it as follows:

```
FreeBSD-9.0-RELEASE-amd64-memstick.img:
  Unix Fast File system [v1] (little-endian),
  last mounted on , last written at Tue Jan  3 08:57:34 2012,
  clean flag 1, number of blocks 669632, number of data blocks 667639,
  number of cylinder groups 13, block size 8192, fragment size 1024,
  minimum percentage of free blocks 8, rotational delay 0ms,
  disk rotational speed 60rps, TIME optimization
```


```
# FreeBSD-9.0-RELEASE-amd64-memstick.img:
8 partitions:
#        size   offset    fstype   [fsize bsize bps/cpg]
  a:  1339264        0    4.2BSD        0     0     0 
  c:  1339264        0    unused        0     0         # "raw" part, don't edit
```

So it appears that either:

a) There is a dedicated bsdlabel with a single 'a' partition, but it starts from block 0.
or
b) The image is just of a pure UFS filesystem with no partitioning.

This is borne out by my being able to mount the image as either /dev/md0a or as /dev/md0 and being able to see the same files.  Either way, the filesystem seems to begin at block 0 of the image file, and thus would also begin at block 0 of any device it is dd'd to.

My question is, if the filesystem starts at block 0, where is the bootcode kept?  How might I go about creating my own bootable images with the same layout?


----------



## SirDice (Jan 5, 2012)

If I'm not mistaken the FreeBSD-9 memstick uses a GPT layout, not an MBR layout.


----------



## jem (Jan 5, 2012)

With respect, I think you are mistaken. It looks like a dedicated bsdlabel to gpart:


```
root@beastie:~ # mdconfig -f FreeBSD-9.0-RELEASE-amd64-memstick.img 
md2
root@beastie:~ # gpart show md2
=>      0  1339264  md2  BSD  (654M)
        0  1339264    1  freebsd-ufs  (654M)
```


Upon further investigation it seems that when you format a device with UFS it leaves the first 64KB untouched and does not use it for filesystem data:


```
root@beastie:~ # dd if=/dev/zero of=test.img bs=1m count=1
1+0 records in
1+0 records out
1048576 bytes transferred in 0.001784 secs (587816962 bytes/sec)

root@beastie:~ # mdconfig -f test.img 
md2

root@beastie:~ # newfs /dev/md2
/dev/md2: 1.0MB (2048 sectors) block size 16384, fragment size 2048
        using 2 cylinder groups of 0.50MB, 32 blks, 64 inodes.
super-block backups (for fsck -b #) at:
 160, 1184

root@beastie:~ # od -A d -t a test.img | head -5
[red]0000000  nul nul nul nul nul nul nul nul nul nul nul nul nul nul nul nul
*
0065536[/red]  nul nul nul nul nul nul nul nul   ( nul nul nul   0 nul nul nul
0065552    8 nul nul nul   @ nul nul nul nul nul nul nul nul nul nul nul
0065568  nul nul nul nul nul nul nul nul nul nul nul nul stx nul nul nul
```

I guess that it's safe to write a bsdlabel and bootcode to this area and define a single partition spanning the whole device.

That also suggests that the practice of starting partition 'a' at an offset of 16 is unnecessary.


----------



## SirDice (Jan 5, 2012)

jem said:
			
		

> With respect, I think you are mistaken. It looks like a dedicated bsdlabel to gpart:


http://lists.freebsd.org/pipermail/freebsd-current/2011-October/028145.html


----------



## jem (Jan 5, 2012)

SirDice said:
			
		

> http://lists.freebsd.org/pipermail/freebsd-current/2011-October/028145.html



They must have changed this since 9.0-BETA3, as there is no sign of GPT partitioning on this image.  Later in that thread they discuss problems with booting GPT memsticks and suggest reverting to the previous method.


----------

