2018年4月20日 星期五

Use grub to boot the target kernel after reboot

The key concept is that grub.cfg is the final product. The grub behavior will be performed according to grub.cfg (and the environment variables used by it). Thus please try to control everything with the tools of grub first. Let the tools control grub.cfg and environment variables for you.

Firstly, enable the default value is mutable: change the default value from 0 to be saved.

$ sudo vi /etc/default/grub
GRUB_DEFAULT=0

to be

GRUB_DEFAULT=0

and then give it a default value (0 here). The number "0" is the grub entry number. It could be 0, 1, 2, ...etc. depends on the order and number of your entries.

$ sudo grub-set-default 0
Update the grub.cfg and make all change effective.

$ sudo update-grub

Now your grub entries are ready to be selected. For example, select the 6th entry of a sub-menu (order number 1)

$ sudo grub-reboot "1>6"

And then reboot

$ sudo reboot
By the way titles should also work according to its GNU manual. https://www.gnu.org/software/grub/manual/grub/html_node/Simple-configuration.html

Previously it was documented the way to use entry title. While this still works it’s not recommended since titles often contain unstable device names and may be translated


2018年2月24日 星期六

Provision Ubuntu automatically with KVM and cloud-init


We usually want to prepare a fresh Ubuntu system to test our application or prepare a development environment. The following shows how to provision a fresh Ubuntu on your local machine by KVM virtualization technology.

Repository

https://github.com/tai271828/ubuntu-setup-automation

Pre-requirement

Ubuntu Xenial desktop

  • libvirt.pc (provided by libvirt-dev)
  • Python.h (provided by libpython3.5-dev)
  • cloud-localds (provided by cloud-image-utils)
  • optional: You may want to have virt-manager or virt-viewer in your system to see the provisioned system.

Setup


sudo apt-get install libvirt-dev
sudo apt-get install libpython3.5-dev
sudo apt-get install cloud-image-utils

virtualenv -p python3 venv

git clone https://github.com/tai271828/ubuntu-setup-automation.git

source venv/bin/activate

If you want to connect the KVM system later over SSH, you need to paste your public ssh key in this session (replace @@my_ssh_public_key@@ ). If you don't have a key or just want to use password to login, remove the whole session in order that cloud-init won't be confused by the invalid string @@my_ssh_public_key@@.

ssh_authorized_keys:
 - @@my_ssh_public_key@@

Run

In venv python virtual environment,

(venv) ubuntu-setup-automation/scripts⟫ cloud-localds /tmp/my-seed.img ../data/user-data && sudo cp /tmp/my-seed.img /var/lib/libvirt/images/ && sudo ./prepare-kvm-deployment && ../bin/create-instance-kvm


  • cloud-localds /tmp/my-seed.img ../data/user-data
    • This command prepares the image to inject user-data used by cloud-init later.
  • sudo cp /tmp/my-seed.img /var/lib/libvirt/images/
    • This command mv the user-data image, a.k.a my-seed.img, to the folder with libvirt accessible permission, so the user-data image could be used by qemu later.
  • sudo ./prepare-kvm-deployment && ../bin/create-instance-kvm
    • This command will
      • Fetch the official Ubuntu iso
      • Patch the iso so the iso has a preseed file to answer all questions prompted in the installation stage.
      • Initialize the fresh installed Ubuntu system by cloud-init.







2017年6月3日 星期六

Check the configuration of your Jekins charm

Because of different working time zone, sometimes I have to take care of the Jenkins node deployed by Juju, which also maintained by another colleague, to support the colleagues in my time zone. What if I could not access the permission of the Jenkins node?

I take care of the MAAS server to make sure I could provide the hardware resource, e.g. physical nodes, so I could have the permission and credential of the MAAS server and its region controller. The Juju service is provided on the same server, so I could access the Juju profile to deploy the nodes in the lab for sure. The question is, what if I want to access the services deployed by the Juju?

Let's back to the Jenkins service, and take it for example. The answer is simple: check the deployment configuration yaml.

In the README  https://github.com/jenkinsci/jenkins-charm , the default user name is "admin" of the Juju-deployed Jenkins, and it is not recommended (and I think it means "not allowed") to change. Thus the rest of necessary information is to search the deployment configuration yaml hosts the password information, and pray for that the guy to deploy the service did not change its password manually by juju set jenkins password=mypassword.

2017年5月7日 星期日

Build SOLVCON development environment

There are several ways to build its development environment. We could use miniconda directly and only. Or, we could follow the travis.yaml to build the isolated development environment.


Everything comes from miniconda:

export PATH="<YOUR_MINICONDA_ROOT>/bin:$PATH"


Then further configuration to be more automatic. This step is/should be optional:

conda config --set always_yes yes --set changeps1 no
conda update -q conda
conda info -a


Change PS1 option is not necessary. You could decide if you refer it or not.

Create a conda isolated python environment <YOUR_SOLVCON_SRC>/build/env/

<YOUR_SOLVCON_SRC>/contrib/devenv/create.sh

Go to this conda isolated python environment and activate the associated virtualenv

source <YOUR_SOLVCON_SRC>/build/env/start
Please note we won't see the prompt change as what we always get via usual conda activate. Check if we have already been in the conda virtual environment by:

which python

And this python should be the one in the conda virtual environment.


Now it is time to install everything in the conda virtual environment!

<YOUR_SOLVCON_SRC>/contrib/conda.sh

If you want to use the latest pybind11 (optional):

pip install -U https://github.com/pybind/pybind11/archive/master.zip

This month I found an issue and YYC identified it is a real issue of pybind11[1].

Now all packages and libraries are ready to build SOVLCON. Let's build by[2]:

python setup.py build_ext --inplace

That is it! Let's test it a bit:


Unit tests with doctest

nosetests --with-doctest -v

Function tests. Your test machine should be a SSH server to test parallel tests locally.

nosetests ftests/parallel/* -v
In the latest step of travis.yaml, there is a release.sh.

<YOUR_SOLVCON_SRC>/contrib/release.sh
This step is optional if you run SOLVCON locally. The release.sh is for ephemeral build/test nodes. The script strips the build files and packs the necessary libraries with the SOLVCON source to distribute to the test/biuld nodes.

Time to "march" something!! : D

[1] https://github.com/pybind/pybind11/pull/836 No one but me noted the issue for one month!
[2] You may not want to perform "python setup.py install" because this will install your SOLVCON code into the conda environment. We want to keep conda and build/env virtual environment separately.

Summary

In this article we separate the conda and env virtual environment. The advantage to separate them is that we could install common packages via <YOUR_SOLVCON_SRC>/contrib/conda.sh and share them over different build/env virtual environments.



2016年11月15日 星期二

extract Debian pakcages

We can use not only ar to extract data from a Debian package, but also the following commands:

dpkg-deb -x <a-deb> <a-folder>
dpkg-deb -e <a-deb> <a-folder>/DEBIAN

then pack it back by

dpkg-deb -b <the-folder> <the-deb>

2016年11月5日 星期六

use docker to provide nfs service.

First, fetch the docker file. e.g. https://github.com/tai271828/dockerized_nfs_server

And then build the docker image by

docker build -t <the container name you want to have> .

Then fix this source code start.sh to rename mynfs to be <the container name you want to have>.

Follow the README to start the container to have the nfs service. Before starting the container, make sure you native host has the kernel which supports NFS service. Take Ubuntu 16.04 as an example, NFS service still needs the support from kernel. Use ps aux | grep nfsd on your native host to see the NFS support from kernel is already there or not. If no, you may insert the nfsd kernel module to have the NFS support from kernel:

sudo modprobe nfsd

Otherwise you may have the following error message when trying to mount the NFS folders provided by the NFS container:

mount.nfs: timeout set for Sun Nov  6 13:54:20 2016
mount.nfs: trying text-based options 'proto=tcp,port=2049,vers=4,addr=172.17.0.2,clientaddr=172.17.0.1'
mount.nfs: mount(2): Connection refused



2016年4月6日 星期三

use python to write a GUI application based on GTK+3 framework

Recently I want to have a little application and I chose this solution:

python - the programming language
GTK+3 - the GUI framework
glade - the IDE to design the layout of the GUI application

glade could dump the layout drawn with glade IDE to a file in XML, which could be understood by GTK3+, so we could speed up our designing process.

The reason I chose GTK+3 is simple. It is because the glib on my OS** requires my glade should be 3.x instead of 2.x.***

** I use Ubuntu 14.04 64 bit
*** the syntax, "import gtk", is used to support GTK+2.x, and you could refer to this stackoverflow link for the details.




Step 1: use glade to draw the layout and dump it to XML.


Firstly launch glade, and then create a window by clicking the button "window" of the tool panel. Please note we should give the window a name, which its default is "window1", when creating it. This name will be also used for python gtk3+ program when the program tries to access the window object.

Save after drawing and the file will be in XML format.


Step 2: Write the python code


The overview flow of the python gtk+3 framework roughly looks like the following:
Use builder to get the window object, register signal and set up configuration etc. Finally executing Gtk main loop and waiting for events.

This is a snippet code of an example:

from gi.repository import Gtk
class HellowWorldGTK(object):
    """This is a Hello World GTK application"""
    def __init__(self):
        # layout generated by glade
        layout_filename = "layout.xml"
        builder = Gtk.Builder()
        builder.add_from_file(layout_filename)
        #builder.connect_signals(self)
        self.window = builder.get_object("window1")
        if self.window:
            self.window.connect("destroy", Gtk.main_quit)
        self.window.show_all()
if __name__ == "__main__":
    hwg = HellowWorldGTK()
    Gtk.main()

layout.xml is the file name saved in step1 with glade and "window1" is the window name mentioned in step1. Please pay attention to this line to quit the GTK main loop as destroying the window.

self.window.connect("destroy", Gtk.main_quit)

Without this line, the program will keep running after launching it and there is no way to use OS signal to terminate the process.

Step 3: executing


Save the snippet in Step2 as go.py. Execute go.py.


Please refer to Python GTK+3 Tutorial for more details.