20. April 2021

How to statically link Rust application for Windows

Rust compiler can generate a single binary that contains all dependencies. This is a great feature for creating stand-alone tools or tools for containers.

There is one gotcha for Windows which does not appear on a developer’s machine. When you move the application to a brand new installation of Windows or you try to start the in Windows Docker Container which contains Windows for Datacenters the app won’t start.

Why? The system is missing Microsoft Visual C++ Redistributable 2015-2019 (vc_redist) which you can download from microsoft.com.

It’s quite inconvenient to force the user to install the package in the case of a stand-alone Rust tool. The package installation even requires elevation of privileges.

The alternative approach to distributing vc_redist is to statically link CRT library into the application. It will result in a slightly bigger application around +100KB.

It’s necessary to tell rustc to perform static linking of CRT.

In the project create file .cargo/config.toml:

rustflags = ["-C", "target-feature=+crt-static"]

Rebuild the application and the new binary is statically linked and can run even on Windows 8 without vc_redist.

The interesting part is how to determine the proper target name which is on the first line of the example configuration. Many examples on the internet are referring to target.i686-pc-windows-msvc which does not work. Use the following command to determine parts of the name of the target:

rustc --print cfg

The target name is composed of values on the lines above.

Facebook Comments

19. April 2021

How to do an ICMP ping of a Service in Kubernetes

When moving software to the cloud you may encounter a component that relies on good old ICMP ping.

The problem is that Kubernetes does not support ICMP ping between services. Nigel Poulton described reasons in his post.

But what if the software depends on ICMP ping because it’s hardcoded since days of the pre-cloud era?

One way to solve the problem and make ICMP ping between Service working in Kubernetes is to use the following trick which has two steps:
– open TCP tunnel which forwards a port of service to localhost
– define host alias in /etc/hosts pointing to

The trick is based on the simple idea that you can ping localhost and talk to the port with local address which is then forwarded to the desired address.

Commands to add ICMP ping to service “myservice”:

socat tcp-listen:8000,reuseaddr,fork tcp:myservice:8000
echo " myservice" >>/etc/hosts


ping myservice
nc -z myservice 8000

The solution is definitely not 100% bullet-proof, but it can be a good workaround until the software is fixed.

Facebook Comments

24. March 2021

How to download binary file in Rust by reqwest

reqwest in Rust allows you to download file. The question is how to store the file on the filesystem. There are several examples on the internet where authors are calling .text() which will result in a corrupted file on the filesystem because text() is decoding UTF-8 characters.

Here is an example of how to download a binary file and store content in a file on the local file system.

File download.rs:

use std::io::Cursor;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

async fn fetch_url(url: String, file_name: String) -> Result<()> {
    let response = reqwest::get(url).await?;
    let mut file = std::fs::File::create(file_name)?;
    let mut content =  Cursor::new(response.bytes().await?);
    std::io::copy(&mut content, &mut file)?;

async fn main() {
    fetch_url("https://georgik.rocks/wp-content/uploads/sianim.gif".to_string(), "siriel.gif".to_string()).await.unwrap();

File Cargo.toml:

name = "download"
version = "0.1.0"
authors = ["Georgik.Rocks"]
edition = "2018"

reqwest = "*"
tokio = { version = "1", features = ["full"] }

name = "download"
path = "download.rs"

To build the example type following:

cargo run
Facebook Comments

1. March 2021

Lenovo T14 CPU locked at 0.8 GHz on Windows

Users with brand new Lenovo T14 might face strange behavior on Windows. The computer seems to be slow and the detail of CPU usage displays a constant CPU speed set to 0.8 GHz.

The problem can be solved by upgrading BIOS. Drivers can be found at pcsupport.lenovo.com

Facebook Comments

1. December 2020

How to switch tabs in Windows Terminal

Windows Terminal is very versatile shell for Windows which allows to run CMD, PowerShell or Linux in WSL2. The terminal also supports tabs.

To switch between tabs you can use:

  • CTRL+Tab – switch to next tab
  • CTRL+Shift+Tab – switch to previous tab
  • If you’d like to use own keyboard shortcuts like CTRL+PgUp, CTRL+PgDown, then just open Settings CTRL+, and enter following code into section actions:

    ..."actions":  [
      { "command": "nextTab", "keys": "ctrl+pgdn" },
      { "command": "prevTab", "keys": "ctrl+pgup" }
    Facebook Comments