# How to extract and edit the .gz bootloader?



## rickm (Jul 10, 2014)

Hi

This is my first post, I have some experience with Unix, but not FreeBSD itself, it's the first time that I'm playing with FreeBSD and I will be very thankful if you could help.

I have a FreeBSD system that I want to customize, I used Linux tools to mount the FreeBSD file system offline (UFS2) and I found something like this:

```
Ricks-MacBook-2:ufs root# ls -lha /Volumes/FreeBSD/
total 198352
drwxr-xr-x  7 root  wheel      512B Oct 15  2012 .
drwxrwxrwt@ 5 root  wheel      170B Jul  9 14:02 ..
-rw-r--r--  1 root  wheel        0B Oct 15  2012 sps
drwxr-xr-x  3 root  wheel      512B Oct 15  2012 boot
-rwxr-xr-x  1 root  wheel       130M Oct 14  2012 loader.gz
drwxr-xr-x  2 root  wheel      512B Nov  4  2009 usr
drwxr-xr-x  2 root  wheel      512B Nov  4  2009 var
Ricks-MacBook-2:ufs root#
```
Until here all is good, the loader.gz is similar to initrd.gz or vmlinuz.gz for Linux that is decompressed on the fly and in this case contains all files of the file system. I want to edit it, for example, edit default configuration files, add new binaries, and save it to use with my customization.

It's .gz, so I extracted with gzip and all is fine, however I'm unable to mount it or find a way easy way to modify its contents. From the .gz extracted:

```
Ricks-MacBook-2:ufs root#  file loader-no-gz
loader-no-gz: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), dynamically linked (uses shared libs), not stripped
```
How to "mount" this ELF to edit the configuration files and replace binaries and pack it again?

Can you help me please? If you could provide detailed guidance I will be very thankful.

Have a good day.


----------



## SirDice (Jul 10, 2014)

It's a compressed image of a filesystem. You need to mount the image to access the files.


----------



## rickm (Jul 10, 2014)

Hi @SirDice,

Thanks for the reply. I was thinking it should be something like that. My question is: how to mount this image that looks like a ELF? Can you please show an example to me?

What is the filesystem format used to mount this image?

Thanks.


----------



## SirDice (Jul 10, 2014)

rickm said:
			
		

> What is the filesystem format used to mount this image?


It's a plain UFS image.

On FreeBSD you can use mdconfig(8) and mount the newly created md(4) device.


----------



## rickm (Jul 10, 2014)

Hi SirDice

Thanks again for your reply. Serious, I can't believe it's a plain UFS image, see below, please:


```
rick@lnx:~$ file loader 
loader: ELF 64-bit LSB  executable, x86-64, version 1 (FreeBSD), dynamically linked (uses shared libs), not stripped
rick@lnx:~$ sudo modprobe ufs
[sudo] password for rick: 
rick@lnx:~$ 
rick@lnx:~$ lsmod |grep -i ufs
ufs                    74890  0 
rick@lnx:~$ sudo mount -r -t ufs -o loop,ufstype=ufs2 /home/rick/loader /home/
loader
rick@lnx:~$ sudo mount -r -t ufs -o loop,ufstype=ufs2 /home/rick/loader /mnt/pendrive/
mount: wrong fs type, bad option, bad superblock on /dev/loop0,
       missing codepage or helper program, or other error
       In some cases useful info is found in syslog - try
       dmesg | tail  or so

rick@lnx:~$ dmesg
[ 1329.288450] ufs_read_super: bad magic number
```


```
root@lnx:~# mmls -t dos /home/rick/loader 
Invalid magic value (File is not a DOS partition (invalid primary magic) (Sector: 0))
root@lnx:~# 
root@lnx:~# mmls -t bsd /home/rick/loader 
Invalid magic value (BSD partition table (magic #1) (Sector: 1) 0)
root@lnx:~# 
root@lnx:~# mmls -t bsd -o 63 /home/rick/loader 
Invalid magic value (BSD partition table (magic #1) (Sector: 64) d8070000)
root@lnx:~# 
root@lnx:~# fsstat -o 63 /home/rick/loader 
Cannot determine file system type
```

This is unbelievable, neither ufs module for mount is able to mount it on linux or the tools for sleuthkit recognize it properly.

I gave up, I will download a FreeBSD image to try manage this files. I checked the man pages, but it's not very clear to me yet, my file is called loader, can you please give me an practical example demonstrating how to mount "loader" on FreeBSD and how to unmount it properly to save my changes?

I assume that the following is the proper procedure:

```
mdconfig -a -t vnode	-f /tmp/rick/loader -u 4
mount /dev/md4 /mnt/temp
Edit my stuff on "/mnt/temp/"
```

Or should I go with:

```
mdconfig -f /home/rick/loader -u 1
gnop	create -o 512K md1
mount /dev/md1.nop /mnt/temp
Edit my stuff on "/mnt/temp/"
```

And finally, unmount, deatch and gzip it?

```
unmount /mnt/temp/
mdconfig -d -u 4
gzip loader
```

Am I correct?

Thanks.


----------



## wblock@ (Jul 10, 2014)

Where is loader.gz from?  I don't see it on my systems.


----------



## rickm (Jul 10, 2014)

Hi @wblock@

It's from a VMware image using FreeBSD, once I mount the image with UFS I just have the following files:


```
Ricks-MacBook-2:ufs root# ls -lha /Volumes/FreeBSD/
total 198352
drwxr-xr-x  7 root  wheel      512B Oct 15  2012 .
drwxrwxrwt@ 5 root  wheel      170B Jul  9 14:02 ..
-rw-r--r--  1 root  wheel        0B Oct 15  2012 sps
drwxr-xr-x  3 root  wheel      512B Oct 15  2012 boot
-rwxr-xr-x  1 root  wheel       130M Oct 14  2012 loader.gz
drwxr-xr-x  2 root  wheel      512B Nov  4  2009 usr
drwxr-xr-x  2 root  wheel      512B Nov  4  2009 var
Ricks-MacBook-2:ufs root#
```

And the loader.gz is where all stuff is, for example, /etc/, /bin, etc. I'm able to confirm it with `grep`.

Advises?

Thanks


----------



## wblock@ (Jul 10, 2014)

To change that, you'll have to extract it to a directory, make changes, then put it all back into an archive.  I have not used these VM images, but if they are from the official snapshots, the build script would be an example of how to package the file.


----------



## rickm (Jul 10, 2014)

Unfortunately not official, it's a custom image of a unsupported product. I mean, the company that created it do not exist anymore.


----------



## rickm (Jul 10, 2014)

Hi,

I downloaded a FreeBSD image and installed it, hopefully was very easy.

I will try from here since my target system is FreeBSD as well. However, I'm with a problem since I don't know how to mount a VMware .vmdk on FreeBSD, if I try the common methods such as:


```
mdconfig -a -t vnode -f /root/VMware-FreeBSD.vmdk
md0
mkdir /mnt/new/
mount -t ufs /dev/md0 /mnt/new/
mount: md0 invalid argument.
```

I tried with cd9660 and UFS and both fails on the same way, I guess that there is any special trick to mount a .vmdk image on FreeBSD. Can you help me please?

Thanks.


----------



## worldi (Jul 11, 2014)

Interesting. It probably contains an "internal file system" (as mentioned in libstand()). Chances are that you cannot easily modify its content.

Since the file is dynamically linked and not stripped, would you mind providing the outputs of `ldd loader` and `nm -a loader`?


----------



## wblock@ (Jul 11, 2014)

VMDK is a standard VM disk format, it is not a filesystem.  VirtualBox can use it or convert it.  But why are we now dealing with a VMDK rather than just the loader.gz which was already available?


----------



## rickm (Jul 11, 2014)

Hi wblock@,

The loader.gz is inside a VMware imahe. I can copy it out of the VMware image on Linux, but I can't write to it since the UFS module is read-only. on Linux, I'm able to mount the whole .vmdk just using UFS module pointing to the .vmdk file without care about it's how format. How to mount a .vmdk file on FreeBSD and in sequence the internal UFS?

Thanks.


----------

