How to use a specific Ubuntu image source for WSL and push the image to your Windows machines

This configuration only applies to Landscape beta at this time.

If you don’t want to download your Ubuntu image from the Microsoft Store, you can instead use a different image source. This guide describes how to get your Ubuntu image from a different source and push that image to your WSL machines using the Landscape REST API.

The example provided here uses curl and assumes your Windows host machine(s) are already registered in Landscape. For more information on registering a host machine, see how to register WSL hosts to Landscape.

Set up your environment

First, set up your environment:

  1. Install prerequisites:

    snap install curl jq
    
  2. Set the environment variables specific to your Landscape installation and user:

    export LANDSCAPE_USER_EMAIL=john@example.com
    export LANDSCAPE_USER_PASSWORD=pwd
    export LANDSCAPE_URL=https://landscape-server.domain.com
    

Install WSL on multiple Windows host machines

If you’re installing WSL on multiple Windows machines, use the following script. Your Windows host machines should already be registered with Landscape before running this script. If you haven’t registered them yet, see how to register WSL hosts to Landscape.

Before running this script, you first need to edit:

  • PARENT_COMPUTER_IDS: Contains the array of IDs of the Windows host machines.
  • ROOTFS_URL: Contains the URL to download the image from.
  • COMPUTER_NAME: Contains the name to use for the created WSL instance on each Windows host.

Once you’ve edited those variables, execute the full script. The script will display the responses from Landscape.

#!/usr/bin/env bash

# array of instance ids to install the WSL image on
PARENT_COMPUTER_IDS=(6 7 8 9)

# URL to download the image from
ROOTFS_URL=https://example.com/ubuntu.img

# Name of the image
COMPUTER_NAME=Custom-Image

# build the login payload: {"email": "admin@example.com", "password": "adminpassword"}
LOGIN_JSON=$( jq -n \
    --arg em "$LANDSCAPE_USER_EMAIL" \
    --arg pwd "$LANDSCAPE_USER_PASSWORD" \
    '{email: $em, password: $pwd}' )

# make the login request
LOGIN_RESPONSE=$( curl -s -X POST "$LANDSCAPE_URL/api/v2/login" \
    --data "$LOGIN_JSON" \
    --header "Content-Type: application/json" \
    --header "Accept: application/json" )

# extract the JWT from the response and trim the double quotes
JWT=$( echo $LOGIN_RESPONSE | jq .token | tr -d '"')

for COMPUTER_ID in "${PARENT_COMPUTER_IDS[@]}"; do
    # build the request payload: {"rootfs_url": "example.com", "computer_name": "Custom-Image"}
    WSL_JSON=$( jq -n \
        --arg rf "$ROOTFS_URL" \
        --arg cn "$COMPUTER_NAME" \
        '{rootfs_url: $rf, computer_name: $cn}' )

    # make the request
    API_RESPONSE=$( curl -s -X POST \
        "$LANDSCAPE_URL/api/v2/computers/$COMPUTER_ID/children" \
        --data "$WSL_JSON" \
        --header "Authorization:Bearer $JWT" \
        --header "Content-Type: application/json" \
        --header "Accept: application/json" )

    # show the response
    echo $API_RESPONSE
    echo
done

Install WSL on a single Windows host machine

If you’re installing WSL on a single Windows machine, use the following script. Your Windows host machine should already be registered with Landscape before running this script. If you haven’t registered it yet, see how to register WSL hosts to Landscape.

Before running this script, you first need to edit:

  • PARENT_COMPUTER_ID: Contains the ID of the Windows host machine.
  • ROOTFS_URL: Contains the URL to download the image from.
  • COMPUTER_NAME: Contains the name to use for the created WSL instance on your Windows host.

Once you’ve edited those variables, execute the full script. The script will display the response from Landscape.

#!/usr/bin/env bash

# id of the instance to install WSL on
PARENT_COMPUTER_ID=6

# URL to download the image from
ROOTFS_URL=https://example.com/ubuntu.img

# Name of the image
COMPUTER_NAME=Custom-Image

# build the login payload: {"email": "admin@example.com", "password": "adminpassword"}
LOGIN_JSON=$( jq -n \
    --arg em "$LANDSCAPE_USER_EMAIL" \
    --arg pwd "$LANDSCAPE_USER_PASSWORD" \
    '{email: $em, password: $pwd}' )

# make the login request
LOGIN_RESPONSE=$( curl -s -X POST "$LANDSCAPE_URL/api/v2/login" \
    --data "$LOGIN_JSON" \
    --header "Content-Type: application/json" \
    --header "Accept: application/json" )

# extract the JWT from the response and trim the double quotes
JWT=$( echo $LOGIN_RESPONSE | jq .token | tr -d '"')

# build the request payload: {"rootfs_url": "example.com", "computer_name": "Custom-Image"}
WSL_JSON=$( jq -n \
    --arg rf "$ROOTFS_URL" \
    --arg cn "$COMPUTER_NAME" \
    '{rootfs_url: $rf, computer_name: $cn}' )

# make the request
API_RESPONSE=$( curl -s -X POST \
    "$LANDSCAPE_URL/api/v2/computers/$PARENT_COMPUTER_ID/children" \
    --data "$WSL_JSON" \
    --header "Authorization:Bearer $JWT" \
    --header "Content-Type: application/json" \
    --header "Accept: application/json" )

# show the response
echo $API_RESPONSE
echo