github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/docs/debug/aws.md (about)

     1  Debugging UniK Amazon images
     2  
     3  When debugging unikernels, the main tool amazon offers us is "Get System logs". This basically limits you to "printf" debugging style.
     4  
     5  To use a real debugger, you can build your own xen hypervisor, that will run the same unik images that were built for amazon. 
     6  You can then use gdb to debug your code.
     7  
     8  These instructions are for OS X. can be used in Linux with minor modifications.
     9  
    10  # Get Xen
    11  
    12  I used this vagrant box: https://github.com/englishm/vagrant-xen
    13  
    14  In the vagrant config, forward port 9999:
    15  ```
    16  ...
    17  config.vm.network "forwarded_port", guest: 9999, host: 9999
    18  ...
    19  
    20  ```
    21  
    22  This will be important later when we want to connect to this machine with gdb.
    23  
    24  ## PV Grub
    25  Once you got the box running, ssh inside it (```vagrant ssh```). 
    26  AWS uses pv grub too boot images, you will need to build pv grub (as it's not there by default).
    27  
    28  In general, the instructions are [here](http://wiki.xen.org/wiki/PvGrub
    29  ). Before doing "./configure", install these packages as well (otherwise build will fail):
    30  
    31  ```
    32  sudo apt-get install libaio-dev libssl-dev libc6-dev-i386 texinfo git
    33  ```
    34  
    35  ## Add a bridge
    36  
    37  To add a bridge, add the following lines to /etc/network/interfaces:
    38  ```
    39  iface xenbr0 inet dhcp
    40      bridge_ports eth0
    41      bridge_stp off
    42      bridge_maxwait 0
    43      bridge_fd 0
    44  ```
    45  
    46  Then run this (do this again if you restart, not sure why, but it is not automatic):
    47  
    48  ```
    49  sudo ifdown eth0 && sudo ifup xenbr0 && sudo ifup eth0
    50  ```
    51  
    52  Sources:
    53  - http://askubuntu.com/questions/136089/how-to-set-up-bridged-networking-in-xen
    54  - https://help.ubuntu.com/community/Xen
    55  
    56  
    57  
    58  # Fake AWS metadata service
    59  
    60  Create a xen script on this file "/etc/xen/scripts/metadata-fake", with the following. 
    61  Change 10.0.2.15 to your machine's IP
    62  
    63  ```
    64  #!/bin/bash
    65  
    66  dir=$(dirname "$0")
    67  . "$dir/vif-bridge"
    68  case "$command" in
    69      add|online) 
    70              # TODO support -i $dev so this can be used for multiple vms; it's not working from some reason
    71              iptables -t nat -A PREROUTING   -d 169.254.169.254 -j DNAT --to-destination 10.0.2.15 
    72          ;;
    73      remove|offline)
    74              iptables -t nat -D PREROUTING   -d 169.254.169.254 -j DNAT --to-destination 10.0.2.15 || : 
    75          ;;
    76  esac
    77  ```
    78  
    79  And of course
    80  
    81  ```chmod a+x /etc/xen/scripts/metadata-fake```
    82  
    83  ## Run Metadata Server
    84  
    85  Unik expects a string-to-string map of environment variables in the user-data.
    86  We'll just create an empty map:
    87  
    88  ```
    89  mkdir  latest
    90  cat > latest/user-data <<EOF
    91  {}
    92  EOF
    93  ```
    94  
    95  Then start python fake metadata server:
    96  ```
    97  sudo python -m SimpleHTTPServer 80
    98  ```
    99  
   100  # XL Config file
   101  
   102  
   103  ```
   104  # Example PV Linux guest configuration
   105  # =====================================================================
   106  #
   107  # This is a fairly minimal example of what is required for a
   108  # Paravirtualised Linux guest. For a more complete guide see xl.cfg(5)
   109  
   110  # Guest name
   111  name = "aws-test"
   112  
   113  # 128-bit UUID for the domain as a hexadecimal number.
   114  # Use "uuidgen" to generate one if required.
   115  # The default behavior is to generate a new UUID each time the guest is started.
   116  #uuid = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
   117  
   118  kernel = "/home/vagrant/xen/dist/install/usr/local/lib/xen/boot/pv-grub-x86_64.gz"
   119  extra = "(hd0)/boot/grub/menu.lst"
   120  
   121  # Initial memory allocation (MB)
   122  memory = 1024
   123  
   124  # Maximum memory (MB)
   125  # If this is greater than `memory' then the slack will start ballooned
   126  # (this assumes guest kernel support for ballooning)
   127  #maxmem = 512
   128  
   129  # Number of VCPUS
   130  vcpus = 1
   131  
   132  # Network devices
   133  # A list of 'vifspec' entries as described in
   134  # docs/misc/xl-network-configuration.markdown
   135  vif = [ 'bridge=xenbr0,script=metadata-fake,mac=00:16:3e:58:88:57' ]
   136  
   137  # Disk Devices
   138  # A list of `diskspec' entries as described in
   139  # docs/misc/xl-disk-configuration.txt
   140  disk = [ '/home/vagrant/boot-vol.img,raw,sda1,rw' ]
   141  ```
   142  
   143  Save this as aws-test.conf
   144  
   145  Notes:
   146  - memory and vcpus should match the instance you are emulating
   147  - disk should point the image built by unik. use "--no-cleanup" in `unik build` so it would not delete it after it's uploaded to AWS. 
   148  You will see new file and a folder created in the unik tmp folder. For example:
   149  ```
   150  ~/W/g/s/g/e/unik ❯❯❯ ls -tlr  ~/.unik/tmp/
   151  total 319488
   152  ...
   153  drwx------  7 kohavy  720748206       238 Aug  3 16:47 bootable-image-directory.411462683
   154  -rw-r--r--  1 kohavy  720748206  54525952 Aug  3 16:47 boot-creator-result.img.940436670
   155  ```
   156  Copy the file (in our example, `boot-creator-result.img.940436670`) to the vagrant machine. note the folder as you will need it for later.
   157  - kernel is the path to pv-grub built previously.
   158  - leave extra param as it is (it describes how unik layouts the disk image)
   159  
   160  # Run! 
   161  
   162  ```
   163  sudo  xl create -c ./aws-test.conf
   164  ```
   165  
   166  `ctrl+]` to exit console
   167  
   168  You can delete the vm when you are done:
   169  ```
   170  sudo xl destroy aws-test
   171  ```
   172  
   173  # Debug! 
   174  After starting your vm (with `xl create`), to attached with a debugger,  get dom id:
   175  
   176  ```
   177  sudo  xl list
   178  Name                                        ID   Mem VCPUs	State	Time(s)
   179  Domain-0                                     0   837     1     r-----      12.3
   180  aws-test                                     3  1024     1     --p---       0.0
   181  ```
   182  
   183  Here the ID is 3. replace 3 with your dom id.
   184  
   185  Start gdb stub on the vagrant machine
   186  ```
   187  sudo /usr/lib/xen-4.4/bin/gdbsx -a 3 64 9999
   188  ```
   189  OR just do this:
   190  ```
   191  sudo /usr/lib/xen-4.4/bin/gdbsx -a $(sudo xl list|tail -1 | awk '{print $2}') 64 9999
   192  ```
   193  
   194  
   195  Start our gdb container (your container tab might differ, check containers/versions.json):
   196  ```
   197  docker run --net host --rm -t -i -v /Users/kohavy/.unik/tmp/bootable-image-directory.411462683/:/opt/code:ro  projectunik/rump-debugger-xen:7fa273029766
   198  /opt/gdb-7.11/gdb/gdb -ex 'target remote 192.168.1.109:9999' /opt/code/program.bin
   199  ```
   200  
   201  Debug your problems away!
   202  
   203  Note: Bootable-image-directory.411462683 is the directory that the image was formed from. unik will keep it intact if you use "--no-cleanup".
   204  This directory and the image in the XL config file *MUST* match for source level debugging to work!
   205  
   206  
   207  If you connected with GDB in an early stage, grub might have not loaded ethe kernel yet.
   208  I just place a breakpoint on ```_minios_hypercall_page``` and continued running a few times until the kernel was loaded.