Bernhard's Homepage

GNS3 - Inject a startup-config into IOSv

GNS3 - Inject a startup-config into IOSv

The primary disk is accessible within IOSv as flash: (or flash0:), a second disk as flash2:, a third disk as flash1: (no typing error!!!). I haven't tested, but the fourth disk should be flash3:. The disks must have one partition with fat format.

If any flash device / disk contains a file named ios_config.txt and has a valid checksum file named ios_config_checksum then an existing NVRAM is ignored and a new config is created from the ios_config.txt. This config is activated as a running config and also stored in the NVRAM as a startup-config. Afterwards the ios_config.txt is cleared, so it won't be used on the next boot.

The checksum file ios_config_checksum must contain the md5 checksum of ios_config.txt (32 ascii characters) plus an optional linefeed.

One-Time Setup

You need to access the command line of the linux system of the GNS3 VM. While this can be done via the "Shell" function of the console menu it's much more convenient to use putty/ssh to connect to the GNS3 VM. Then you can use cut-and-paste. Furthermore you should setup winscp/scp to save the resulting image back to your PC.

In this receipt I use mtools to work with dos formatted disk images. It can also be done with loop mounting the disk image, but that needs root privileges. That means you have to be very careful with the permissions. The mtools are pure user mode applications and are much easier to work with (IMHO). So lets install them:

sudo apt-get update
sudo apt-get install mtools
echo "mtools_skip_check=1" > ~/.mtoolsrc

Creation of an empty Disk Image

We need an empty dos formatted disk image. While there are many tools to achieve that, I'm using IOSv to create it. That way there will be no issues with disk format compatibility. It's a bit complex, but we need to create it only once. For lazy guys I have included my empty disk image in the sample disk image (see the end of the page).

First create a new IOSv router in a temporary project. Then add an empty disk image to it:

  • right-click on the IOSv router and select "Configure"
  • select the HDD tab and create a new disk image for HDB (Primary Slave) by clicking "Create..."
  • stay with the defaults in "Binary and format" and "Qcow2 options"
  • In "Size and location" set the "File location:" to "dummy.qcow2" and the "Disk size:" to "1 MiB"
  • Finish
  • set the disk interface of HDB to the same as HDA, best is "virtio"
  • apply with "OK"

Now start the IOSv router, if the initial configuration dialog is shown, abort that with "no". Wait until the router is fully booted. The nice thing about IOSv is, that it automatically formats unformatted disks for use as a flash drive. So our just created empty disk image is now DOS formatted and ready to use. Therefore stop the router.

Now we need to determine, where GNS3 has stored our disk image. For that right click on the IOSv router and select "Show in file manager". You will get a popup like this:

Device Directory

Select the directory (as shown in the above image) and copy that to the clipboard.

In the linux CLI of the GNS3 VM we will now copy and convert the HDB disk. Furthermore we delete the temporary dummy disk image:

qemu-img convert -O raw <paste directory>/hdb_disk.qcow2 empty-1M.img
rm /opt/gns3/images/QEMU/dummy.qcow2*

Now we can delete the IOSv router in the GNS3 project, it has done it's job.

Creation of the startup Configuration

Of course, you now need a startup configuration. I use the ios_base_startup-config.txt as a base, only deleting the "hostname %h" line, as the %h substitution won't work here. Save your config as "ios_config.txt".

Now we have finally all needed parts and can create the startup configuration image:

md5sum ios_config.txt | cut -d ' ' -f 1 > ios_config_checksum
cp empty-1M.img IOSv_startup_config.img
mcopy -i IOSv_startup_config.img@@63S ios_config.txt ::
mcopy -i IOSv_startup_config.img@@63S ios_config_checksum ::
cp -p IOSv_startup_config.img /opt/gns3/images/QEMU/

So what's this "@@63S" stuff??? mcopy works with DOS partitions, but our IOSv_startup_config.img is a disk image starting with a MBR. So we have to tell the mtools, that the partition starts with an offset of 63 sectors.


In the IOSv template we now add the IOSv_startup_config.img as HDB (with the same disk interface as HDA!!!). All newly created IOSv router will now start with this config.

IOSv HDD Config

Sample Disk Image

I've created an example disk image (IOSv_startup_config.img) with a startup_config based on the dynamips startup config.

Together with the empty disk image it's included in

  Length     Date   Time    Name
 --------    ----   ----    ----
  1048576  03-27-17 12:51   IOSv_startup_config.img
  1048576  03-25-17 09:35   empty-1M.img
      386  03-25-17 09:54   ios_config.txt
       33  03-25-17 09:55   ios_config_checksum
 --------                   -------
  2097571                   4 files