NetBSD

NetBSD is a free, fast, secure, and highly portable Unix-like Open Source operating system. It is available for a wide range of platforms, from large-scale servers and powerful desktop systems to handheld and embedded devices.

https://www.netbsd.org/

https://www.netbsd.org/docs/guide/en/index.html

Install

VirtualBox installation
Start VirtualBox
New
Name NetBSD 7.0.1
Choose type BSD
version NetBSD 64 bit
RAM 512 MB
Create virtual disk dynamic VDI with Fixed size 8 GB
Create
    
Start VM
Choose ISO NetBSD-7.0.1-amd64.iso
Start
    
1 - Install NetBSD 
a - installation messages in english
q - keyboard type portuguese
a - install netbsd to hard disk 
shall we continue? (b) yes 
which disk? a) wd0 
a - this is the correct geometry
b - use the entire disk 
install netbsd bootcode? a) yes 
b - use existing partition sizes 
x - partition sizes ok
name harddisk: VBOX HARDDISK
shall we continue? yes 
a) use bios console
x) exit 
a) full installation
install from: a) cd-rom 
Hit enter to continue 
a) configure net       a) wm0       autoselect autoconfig? yes
hostname: netbsd
domain name: 
they are ok? yes
install in /etc? yes
b) timezone Portugal
c) root shell /bin/sh
d) root password ********
e) enable installation x)
f) fetch and unpack x)
g) enable sshd yes 
h) enable ntpd yes 
i) run ntpdate at boot yes
j) enable mdnsd no 
k) enable xdm no 
l) enable cgd yes 
m) enable lvm no
n) enable raidframe yes 
o) add a user vitor add to group wheel, user shell sh 
x) finished configuring 
Hit enter to continue 
release ISO  
reboot 
login as root 

   1 startx # it starts OK
   2 setxkbmap pt 
   3 cc --version 
   4 pkg_info 
   5 putty vitor@localhost -P 2222
   6 pkg_info
   7 # pkg_install-20160410nb1 Package management and administration tools for pkgsrc
   8 # libarchive-3.2.1nb1 Library to read/create different archive formats
   9 # pkgin-0.9.4nb2      Apt / yum like tool for managing pkgsrc binary packages
  10 # https://ftp.netbsd.org/pub/pkgsrc/current/pkgsrc/README-all.html
  11 pkg_add openjdk8-1.8.102
  12 PKG_PATH="ftp://ftp.netbsd.org/pub/pkgsrc/packages/NetBSD/amd64/7.0.1/All/"
  13 export PKG_PATH
  14 PATH="/usr/pkg/sbin:$PATH"
  15 export PATH 
  16 pkg_add openjdk8-1.8.92nb2
  17 cd /usr/pkg/java/openjdk8/bin
  18 ./java -version
  19 pkg_add python27-2.7.11nb2
  20 
  21 # Execute this command to extract and rehash all CA root certificates
  22 # distributed by the Mozilla Project, so that they can be used by third
  23 # party applications using OpenSSL. It also creates a single file
  24 # certificate bundle in PEM format which can be used by applications using
  25 # GnuTLS.
  26         # mozilla-rootcerts install
  27 # To mark these certificates as trusted for users of gnupg2, do
  28 # the following (assuming default PKG_SYSCONFBASE and a Bourne shell):
  29 #         # mkdir -p /usr/pkg/etc/gnupg
  30 #        # cd /usr/pkg/etc/gnupg
  31 #         # for c in /etc/openssl/certs/*.pem; do
  32 #        > openssl x509 -in $c -noout -fingerprint|sed 's|^.*=\(.*\)|\1 S|'
  33 #        > done > trustlist.txt
  34 python2.7 
  35 openjdk8-java -version
  36 ntpdate pt.pool.ntp.org 

https://www.netbsd.org/docs/guide/en/chap-cons.html#chap-cons-wscons-wskbd-keymaps

http://julio.meroh.net/2004/07/playing-with-netbsd-keymaps.html

etc/profile

   1 # System-wide .profile file for sh(1).
   2 export LANG="pt_PT.UTF-8"
   3 export LC_CTYPE="pt_PT.UTF-8"
   4 export LC_ALL=""

   1 #change shell to have utf-8 support, ksh has
   2 chsh -s /bin/ksh vitor
   3 chsh -s /bin/ksh root

Custom service

Add to /etc/rc.conf

   1 beat=YES

/etc/rc.d/beat

   1 #!/bin/sh
   2 #
   3 # PROVIDE: beat
   4 
   5 $_rc_subr_loaded . /etc/rc.subr
   6 
   7 name="beat"
   8 rcvar=$name
   9 pidfile="/var/run/${name}.pid"
  10 command="/usr/pkg/bin/python2.7"
  11 command_args="/home/vitor/${name}.py &"
  12 
  13 load_rc_config $name
  14 run_rc_command "$1"

/home/vitor/beat.py

   1 #!/usr/bin/python
   2 import threading
   3 import time
   4 import os
   5 import syslog
   6 import datetime
   7 import sys
   8 import signal
   9 
  10 def termHandler(signal,frame):
  11     print('Signal term caught')
  12     sys.exit(0)
  13 
  14 if __name__=="__main__":
  15     f=open('/var/run/beat.pid','wa')
  16     f.write('%d'%(os.getpid()))
  17     f.close()
  18     signal.signal(signal.SIGTERM,termHandler)
  19 
  20     while True:
  21         syslog.syslog(syslog.LOG_INFO, "Beat %s"%(datetime.datetime.now()) )
  22         time.sleep(5)

   1 /etc/rc.d/beat start
   2 tail -f /var/log/messages
   3 /etc/rc.d/beat status
   4 /etc/rc.d/beat stop

In /root/.profile uncomment the following to allow binary packages installations

Change prompt

~/.profile

   1 PS1="$(date +'%Y-%m-%dT%H:%M:%S') $(whoami)@${HOST}:${PWD}\$ "

cbeat service

cbeat.c

   1 // cc cbeat.c -o cbeat
   2 
   3 #include<stdio.h>
   4 #include<syslog.h>
   5 #include<unistd.h>
   6 #include<signal.h>
   7 #include<stdlib.h>
   8 
   9 #define TRUE 1
  10 
  11 void signalHandler(int signal){
  12   if(signal == SIGTERM){
  13     syslog(LOG_INFO , "Caught SIGTERM signal. Going to exit ...");
  14     exit(0);
  15   }
  16 }
  17 
  18 int main(){
  19   int pid = getpid();
  20   FILE* handle =  fopen("/var/run/cbeat.pid","wb");
  21   fprintf(handle, "%d" ,pid);
  22   fclose(handle);
  23 
  24   // callback to signalHandler function
  25   signal(SIGTERM , signalHandler  );
  26 
  27   while(TRUE){
  28     syslog(LOG_INFO,"cbeat test");
  29     sleep(5);
  30   }
  31 
  32   return 0;
  33 }

/etc/rc.d/cbeat

   1 #!/bin/sh
   2 #
   3 # PROVIDE: cbeat
   4 
   5 $_rc_subr_loaded . /etc/rc.subr
   6 
   7 name="cbeat"
   8 rcvar=$name
   9 pidfile="/var/run/${name}.pid"
  10 command="/root/cbeat"
  11 command_args="&"
  12 
  13 load_rc_config $name
  14 run_rc_command "$1"

Add in /etc/rc.conf

   1 cbeat=YES

POSIX threads

POSIX threads in NetBSD with signal handling.

   1 kill -s TERM <PID>
   2 kill -s USR1 <PID>
   3 kill -s USR2 <PID>

   1 /* 
   2 cc pthread.c -o pthread -lpthread ./pthread 
   3 ./pthread
   4 */
   5 #include <stdio.h> 
   6 #include <stdlib.h> 
   7 #include <pthread.h>
   8 #include <string.h>
   9 #include <signal.h>
  10 #include <unistd.h>
  11 
  12 typedef struct data {
  13   char msg[128];
  14   int count;
  15 } threadData;
  16 
  17 
  18 void signalHandler(int signal){
  19   if(signal == SIGTERM){
  20     printf("Caught SIGTERM signal. Going to exit ...\n");
  21     exit(0);
  22   }
  23  
  24   if(signal == SIGUSR1){
  25     printf("USR1 caught \n");
  26   }
  27 
  28   if(signal == SIGUSR2){
  29     printf("USR2 caught \n");
  30   } 
  31 }
  32 
  33 void *threadCallback( void *ptr ) {
  34   char *message  = (char *) ((threadData *) ptr)->msg;
  35   int count =  ((threadData *) ptr)->count;
  36  
  37   while(count<10) {
  38     printf("%s count %d thread id %u \n", message,count , pthread_self() );
  39     sleep(1);
  40     count++;
  41   }
  42 }
  43 
  44 int main() {
  45   struct sigaction signalAction;
  46   memset (&signalAction, '\0', sizeof(signalAction));
  47   signalAction.sa_handler = &signalHandler;
  48 
  49   pthread_t thread1Id;
  50   int thread1Ret;
  51   threadData thread1Data;
  52   strcpy(thread1Data.msg,"Message 1234");
  53   thread1Data.count=5;
  54    
  55   pthread_t thread2Id;
  56   int thread2Ret;
  57   threadData thread2Data;
  58   strcpy(thread2Data.msg,"Message 54321");
  59   thread2Data.count=3;
  60   
  61   printf("Main thread id %u \n" , pthread_self() );
  62 
  63   thread1Ret = pthread_create( &thread1Id, NULL, threadCallback, (void*) &thread1Data);
  64   thread2Ret = pthread_create( &thread2Id, NULL, threadCallback, (void*) &thread2Data);
  65 
  66   if(thread1Ret || thread2Ret) {
  67     fprintf(stderr,"Error creating threads ");
  68     exit(EXIT_FAILURE);
  69   }
  70 
  71   // callback to signalHandler function
  72   sigaction(SIGTERM  , &signalAction, NULL);
  73   sigaction(SIGUSR1  , &signalAction, NULL);
  74   sigaction(SIGUSR2  , &signalAction, NULL);
  75 
  76   pthread_join( thread1Id, NULL);
  77   pthread_join( thread2Id, NULL);
  78   printf("Normal exit\n");
  79   exit(EXIT_SUCCESS);
  80 }

Bash

   1 export PKG_PATH=ftp://ftp.NetBSD.org/pub/pkgsrc/packages/NetBSD/$(uname -m)/7.0/All
   2 pkg_add bash

~/.profile

   1 #normal user
   2 PS1="\D{%Y-%m-%d}T\A \u@\H:\w\r\n\$"
   3 # for root
   4 PS1="\D{%Y-%m-%d}T\A \u@\H:\w\r\n\043"
   5 
   6 #normal user
   7 PS1="\e[0;33m\]\D{%Y-%m-%d}T\A\[\e[m\] \e[0;32m\]\u@\H:\w\r\n\$\[\e[m\] "
   8 #root
   9 PS1="\e[0;33m\]\D{%Y-%m-%d}T\A\[\e[m\] \e[0;32m\]\u@\H:\w\r\n\043\[\e[m\] "

C - read and write text

   1 #include <stdio.h>
   2 #define SIZE 5
   3 #define FILENAME "test.txt"
   4 
   5 void clear(char* buffer){
   6   for(int i=0;i<SIZE;i++) *(buffer)=0x0;
   7 }
   8 
   9 int main(void) {
  10   printf("Hello World\n");  
  11   FILE* f = fopen(FILENAME,"w");
  12   char buffer[SIZE];
  13 
  14   for(int i=0;i<3;i++){
  15      fwrite("aaaa\n",5,1,f);
  16   }
  17 
  18   fclose(f);
  19 
  20   FILE* r = fopen(FILENAME,"r");
  21   clear(buffer);
  22     
  23   /*fread(buffer,SIZE,1,r);
  24   printf("%s" , buffer);
  25   fclose(r);*/
  26 
  27   while( fread(buffer,SIZE,1,r)!= 0){
  28     printf("%s" , buffer);
  29     clear(buffer);
  30   }
  31 
  32   fclose(r);
  33 
  34   return 0;
  35 }

Vagrant box

   1 cd /tmp
   2 mkdir netbsd-test
   3 cd netbsd-test/
   4 vagrant init NetBSD/NetBSD-9.1
   5 vagrant up
   6 # Vagrant is not able to mount VirtualBox shared folders on BSD-based
   7 # guests. BSD-based guests do not support the VirtualBox filesystem at
   8 # this time.
   9 vagrant ssh
  10 sudo bash 
  11 df -h
  12 # Filesystem         Size       Used      Avail %Cap Mounted on
  13 # /dev/wd0a          9.1G       1.5G       7.1G  17% / 
  14 

NetBSD (last edited 2024-01-25 11:38:45 by vitor)