3

1. Summarize the problem

How would I create a rootfs.tar of CentOS 7.1905 for use with wsl --import (Windows Subsystem for Linux)

I have not found a rootfs.tar file I can download of CentOS 7.1905 in order to use with wsl --import.

I would like to be able to roll my own rootfs.tar using the dnf command, I have spent the evening searching, trying various things.

If anyone has built a rootfs.tar file for use with WSL, please post.

mkosi (ubuntu utilility) does not work, it cannot be used to build centos images, try it.

Downloading the .raw.tar.gz file does not work with wsl --import, try it.

I am attempting the following command in a Windows .bat file, but cannot find a suitable rootfs.tar file to use, or find a way to build one.

File: create-rootfs.bat

rem <DistributionName> <InstallLocation> <FileName>
wsl --import centos_7.6.1905 c:\wsl\centos\7.6.1905 g:\downloads\cent\centos\rootfs.tar

2. Provide background including what you've already tried.

I have spent the evening reading documentation, trying various incantations of dnf with no luck. There are no examples out there, there are places where dnf and mkosi are recommended, and links to those, but there are no actual, concrete examples that work.


4. Describe expected and actual results including any error messages.

I would like to build a rootfs.tar file of CentOS 7.6.1905, for use with wsl --import.

I am looking for actual examples that work, that have been tried, and proven.

user10664542
  • 1,106
  • 1
  • 23
  • 43

2 Answers2

3

Here we can make CentOS rootfs tarball ourselves. It will be easier if you have a real Linux distribution installed in machine. But here we download a ready-made docker tarball (tar.xz) and convert it to tar.gz because WSL uses bsdtar to extract and install distributions. For this procedure, there should be a pre-installed distribution in WSL. Run that distribution and follow these steps as root user in /root folder to intact file permissions.

  • Open this CentOS docker git repository, go to latest CentOS version branch (not the master), download the centos-7-docker.tar.xz file.

  • Repackage the XZ archive into a GZIP archive: xz -d -c centos-7-docker.tar.xz | gzip -c -2 > centos-7-docker.tar.gz

  • Move that tar.gz folder to Windows drive: mv test.tar.gz /mnt/c/MyFolder

  • Import that tar.gz file as a new distribution: wsl.exe --import CentOS MyFolder test.tar.gz

  • Verify the distribution was installed: wsl.exe --list -v

  • Start the container and log into it: wsl.exe -d CentOS

dnf is not installed in this docker file. So, use rpm or yum to install it, yum install dnf. Make sure Windows Firewall does not block it. For more distributions, you can see my WslInstall repository.

Community
  • 1
  • 1
Biswapriyo
  • 3,491
  • 3
  • 22
  • 42
  • I do not have a machine to install CentOS on, to create a tarball, I'm hoping this method will be the same, I will give it a try. Thank you. – user10664542 Jul 10 '19 at 04:20
  • This solution will not, is not working for me, I'm not seeing 7.6.1905 on any branch, only 7.6.1810 as the latest. I am seeking a way, instructions to build a 7.6.1905 .tar to use with `wsl --import` I 'm not seeing the 1905 release in WslInstall repository. Thank you. – user10664542 Jul 10 '19 at 04:31
  • This started with a .xz file, how was that point reached? Is that the best path to create a rootfs.tar for use with `wsl --import`? Thank You – user10664542 Jul 10 '19 at 04:41
  • For a dry run, I tried release 1810 (ultimately seeking 1905), and got these error messages when trying to create the `tar.gz`. ``` tar: etc/gshadow: Cannot open: Permission denied tar: etc/gshadow-: Cannot open: Permission denied tar: etc/shadow: Cannot open: Permission denied tar: etc/shadow-: Cannot open: Permission denied ``` Is this important? If so, how to avoid when creating the `.tar.gz`? – user10664542 Jul 10 '19 at 04:46
  • I said to do it in `/root` as a root user. And I didn't find that version in [CentOS release notes](https://wiki.centos.org/Manuals/ReleaseNotes). Also you can bootstrap a newer version (if needed) from an older one. – Biswapriyo Jul 10 '19 at 09:37
  • I will give this a try again. – user10664542 Sep 17 '20 at 17:15
  • 2020-Sep-17 I still can not get this to work as described above I followed the instructions exactly and get: error: 'user not found', no 'root' user and I cannot create a 'root' user or any other user with any wsl.exe command From a DOS cmd prompt, using: wsl.exe --user root -d CentOS8 (will not enter a bash shell, get error, then back to cmd prompt) I downloaded and used: https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-Container-8.2.2004-20200611.2.x86_64.tar.xz as the git repo listed in the above was empty – user10664542 Sep 17 '20 at 18:39
  • works perfectly for me, from the first try – Reishin Aug 31 '21 at 13:18
1

It is possible to convert the CentOS cloud QCOW2 images to a rootfs.tar.gz.

Such QCOW2 images are found here: https://cloud.centos.org/centos/

Here is an example script that performs the conversion (requires qemu-utils to be installed):

#!/bin/bash

# Environment variables for the CentOS cloud image
ARCH="x86_64"
OS_VER="7"
ROOTFS_VER="2003"
ROOTFS_FN="CentOS-${OS_VER}-${ARCH}-GenericCloud-${ROOTFS_VER}.qcow2"
ROOTFS_URL="http://cloud.centos.org/centos/${OS_VER}/images/${ROOTFS_FN}"

# Environment variables for Yuk7's wsldl
LNCR_BLD="20040300"
LNCR_ZIP="icons.zip"
LNCR_NAME="CentOS"
LNCR_FN=${LNCR_NAME}.exe
LNCR_ZIPFN=${LNCR_NAME}${OS_VER}.exe
LNCR_URL="https://github.com/yuk7/wsldl/releases/download/${LNCR_BLD}/${LNCR_ZIP}"

# Waits until a file appears or disappears
# - $1   File path to wait for its existence
# - [$2] The string 'a' to wait until the file appears, or 'd' to wait until the file disappears
# - [$3] Timeout in seconds
waitFile() {
  local START=$(cut -d '.' -f 1 /proc/uptime)
  local MODE=${2:-"a"}
  until [[ "${MODE}" = "a" && -e "$1" ]] || [[ "${MODE}" = "d" && ( ! -e "$1" ) ]]; do
    sleep 1s
    if [ -n "$3" ]; then
      local NOW=$(cut -d '.' -f 1 /proc/uptime)
      local ELAPSED=$(( NOW - START ))
      if [ $ELAPSED -ge "$3" ]; then break; fi
    fi
  done
}

# Create a work dir
mkdir wsl
cd wsl

# Download the CentOS cloud image and Yuk7's WSLDL
wget --no-verbose ${ROOTFS_URL} -O ${ROOTFS_FN}
wget --no-verbose ${LNCR_URL} -O ${LNCR_ZIP}

# Extract the CentOS WSL launcher
unzip ${LNCR_ZIP} ${LNCR_FN}

# Clean up
rm ${LNCR_ZIP}

# Mount the qcow2 image
sudo mkdir mntfs
sudo modprobe nbd
sudo qemu-nbd -c /dev/nbd0 --read-only ./${ROOTFS_FN}
waitFile /dev/nbd0p1 "a" 30
sudo mount -o ro /dev/nbd0p1 mntfs

# Clone the qcow2 image contents to a writable directory
sudo cp -a mntfs rootfs

# Unmount the qcow2 image
sudo umount mntfs
sudo qemu-nbd -d /dev/nbd0
waitFile /dev/nbd0p1 "d" 30
sudo rmmod nbd
sudo rmdir mntfs

# Clean up
rm ${ROOTFS_FN}

# Create a tar.gz of the rootfs
sudo tar -zcpf rootfs.tar.gz -C ./rootfs .
sudo chown "$(id -un)" rootfs.tar.gz

# Clean up
sudo rm -rf rootfs

# Create the distribution zip of WSL CentOS
mkdir out
mkdir dist
mv -f ${LNCR_FN} ./out/${LNCR_ZIPFN}
mv -f rootfs.tar.gz ./out/
pushd out
zip -r ../dist/CentOS${OS_VER}.zip ./*
popd

# Clean up
rm -rf out

I also maintain scripts for other CentOS versions, as well as ready-to-import rootfs.tar.gz files here: https://github.com/mishamosher/CentOS-WSL

mishamosher
  • 1,003
  • 1
  • 13
  • 28
  • Ugh! I like very much the approach of getting original files distributed from CentOS, but I don't understand everything in the script. I will give both answers here a try, thank you very much! – user10664542 Sep 17 '20 at 17:14
  • @user10664542 You're welcome (: - In general, the script is quite simplistic. If there is anything specific, I can try my best to clarify it. As a side note, the QCOW2 CentOS images are more complete than the docker ones (they contain a more comprehensive list of preinstalled packages). – mishamosher Sep 19 '20 at 02:36
  • Looking at the script, it is not something that could be run under WSL2 itself (to extract the file system from .qcow2 creating rootfs.tar. Is it possible to convert a .qcow2 to rootfs.tar under WSL2 itself? – user10664542 Sep 21 '20 at 06:22
  • The non-WSL-compatible (yet) commands are related to `modprobe` and `quemu-nbd`. There is no skipping those. Everything else runs pretty much on any Linux distro, DSL and OpenWRT included. You'll be better using Hyper-V or VitualBox with a small Linux box for these tasks, as they don't exhibit the WSL shortcomings. – mishamosher Sep 23 '20 at 02:18