banner
Yuzhen

Yuzhen's Blog

🇨🇳 13 y.o. / Student & Developer & OIer / HAM & BGP Player
telegram
tg_channel
github
mastodon
email
zhihu

[Legacy] How to Elegantly Roll Back Windows

How to Elegantly Roll Back from Arch Linux to Windows#

Background: How to Elegantly Migrate to Arch Linux.

Data Backup#

Just copying the previous article.

Use WeiPE and DiskGenius tools for data backup and partition cloning.

Since you may not be able to attend online classes or do exercises in the PE environment, we can first use DiskGenius's partition imaging feature to save the partition image to an external hard drive, then return to the Windows system and use DiskGenius to restore the partition image to the external hard drive (this is because partition imaging is a 4K/sequential read and write operation, while partition restoration is sequential read 4K/sequential write operation, and the small file read and write speed of solid-state drives is significantly greater than that of mechanical hard drives).

  1. Create a WeiPE environment.
  2. Enter the WeiPE environment and use DiskGenius for disk imaging operations.
  3. Restore the hard drive after installing Windows.

Install System#

Install it yourself.

Configure OI Environment#

CP Editor#

Go to the CP Editor official website to download the installation package, select Windows as the platform, choose v6.10.1 (as of August 18, 2022), and select corresponding architecture-setup.exe, as we need to manually install the compiler.

MSYS2#

To use Clang-Format and Clangd, we need to use MSYS2 to install them. It seems that selecting architecture-setup-with-gcc-xxxx-llvm-xxxx.exe is sufficient, but I haven't tested it.

  1. Go to the MSYS2 website, find the first step under Installation, Download the installer, and click the button to download the installer.
  2. Install MSYS2.
  3. Enter the MSYS2 Shell and type pacman -S msys2-keychain to install the keychain.
  4. Type pacman -Syu to update the packages.
  5. Type pacman -S mingw-w64-x86_64-toolchain mingw64/mingw-w64-x86_64-make mingw64/mingw-w64-x86_64-gdb mingw64/mingw-w64-x86_64-clang mingw64/mingw-w64-x86_64-clang-tools-extra to install the compiler.
  6. Set environment variables.
    • Set MINGW_HOME to your MSYS2 path (default is C:\msys2) under mingw64 (for 64-bit computers) or mingw32 (for 32-bit computers), for example, for 64-bit set it to C:\msys2\mingw64.
    • Set C_INCLUDE_PATH to %MINGW_HOME%\include.
    • Set LIBRARY_PATH to %MINGW_HOME%\lib.
    • Add %MINGW_HOME%\bin to PATH.

Configure CP Editor#

  1. Open CP Editor, click on Options -> Settings in the menu bar.

  2. Find Language -> C++ -> C++ Command, configure according to your preference; my configuration is g++ -Wall -Wextra -O2 -lm --std=gnu++14 for CCF compilation parameters.

  3. Find Extensions -> Code Formatting -> Clang Format, set Clang Format Program to clang-format, and configure the style according to your preference. My settings are as follows:

    BasedOnStyle: Google
    IndentWidth: 4
    
  4. Find Extensions -> Language Server -> C++ Server, check to enable it, and set the delay according to your preference; too short a delay may cause it to be unusable. My setting is 1000ms.

PowerShell 7 and oh-my-posh#

Install PowerShell#

Go to the PowerShell GitHub page, find the installer for your architecture in the latest Release, and install the latest PowerShell.

Configure Windows Terminal#

Download MesloLGM NF font and install it.

To use the GUI settings of Windows Terminal, you need to download Windows Terminal Preview from the Microsoft Store.

In Settings -> Profiles -> Defaults -> Additional Settings -> Appearance -> Text -> Font, set the default font to Fira Code.

Install and Configure oh-my-posh#

  1. Search for oh-my-posh in the Microsoft Store and install it.
  2. Open PowerShell, create a new Powershell folder under $Home\[My ]Documents, and create Microsoft.PowerShell_profile.ps1 under it, entering oh-my-posh init pwsh | Invoke-Expression.

WSL2#

Install Arch Linux#

  1. Go to Control Panel -> Programs -> Turn Windows features on or off, check Windows Subsystem for Linux and Virtual Machine Platform, and click OK.

  2. Restart your computer.

  3. Go to this link to download and install the WSL2 upgrade package.

  4. Open PowerShell and type wsl --set-default-version 2 to set WSL to use WSL2 by default.

  5. Download LxRunOffline, extract it and add it to PATH.

  6. Download archlinux-bootstrap-x86_64.tar.gz.

  7. Open a new PowerShell window and type LxRunOffline i -n archlinux -f <bootstrap path> -d <storage location> -r root.x86_64.

  8. Type wsl --set-version archlinux 2. Please ensure you have enabled Virtual Machine Platform, and if your system supports Hyper-V, please enable it.

  9. Type wsl -d archlinux to enter WSL.

  10. In PowerShell, type wsl --shutdown archlinux && wsl -d archlinux to restart and connect to WSL.

  11. In WSL, type cd /etc && explorer.exe . to open /etc in File Explorer.

  12. Open pacman.conf, uncomment the multilib source, and add the archlinuxcn source:

    [archlinuxcn]
    Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch
    
  13. Open pacman.d/mirrorlist and uncomment the domestic mirror sources.

  14. In WSL, type the following commands.

    pacman -Syy
    pacman-key --init
    pacman-key --populate
    pacman -S archlinuxcn-keyring
    pacman -S base base-devel nano git wget
    
  15. In WSL, type passwd to set the root password.

  16. Create a normal user and set a password.

    useradd -m -G wheel -s /bin/bash <username>
    passwd <username>
    
  17. Type EDITOR=nano visudo, uncomment %wheel ALL=(ALL:ALL) NOPASSWD: ALL, press Ctrl+O Enter to save, and Ctrl+X to exit.

  18. Type id -u <username> to query the ID of the newly created user; the first account should be 1000.

  19. Type exit to exit WSL.

  20. In PowerShell, type LxRunOffline su -n archlinux -v <account id>.

  21. Open PowerShell with administrator privileges, type net stop LxssManager && net start LxssManager to restart the WSL service.

  22. Open the Microsoft® Update Catalog, and download the latest Windows Subsystem for Linux Update (as of August 18, 2022, 5.10.102.2).

  23. Install the update, open PowerShell with administrator privileges, and type wsl --shutdown && wsl -d archlinux to restart WSL and enter WSL, then type uname -a to check the kernel version.

Install and Configure zsh and oh-my-zsh#

  1. Enter WSL.
  2. Type pacman -S zsh to install zsh.
  3. Type chsh, enter the password, and switch the shell to /bin/zsh.
  4. Type sh -c "$(curl -fsSL https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh)" to install oh-my-zsh.
  5. Type git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k to install the powerlevel10k theme.
  6. Type git clone https://ghproxy.com/https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions && git clone https://ghproxy.com/https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting to install plugins.
  7. nano ~/.zshrc, set ZSH_THEME="powerlevel10k/powerlevel10k", plugins=(git zsh-autosuggestions zsh-syntax-highlighting).
  8. Exit and restart WSL.

KDE Plasma#

Basic Installation#

sudo pacman -Syu plasma

Basic Usage#

It is recommended to use GWSL to connect to Plasma. Search for GWSL in the Microsoft Store. The GWSL in the national region is paid, but there is no difference between the trial version and the paid version, with no time limit and no payment required, but it is still recommended to support the author.

After downloading, open GWSL, click the icon on the taskbar to bring up the Dashboard. Click GWSL Distro Tools, then enable Display/Audio Auto-Exporting. GWSL supports placing Linux program windows on the Windows desktop, but I prefer to use a separate virtual desktop to place the entire Plasma.

To do this, you need to do the following:

  1. Create a new virtual desktop.
  2. Open GWSL on the newly created virtual desktop.
  3. Right-click the GWSL icon on the taskbar, select the top XServer Profiles, and choose Fullscreen Mode.
  4. A full-screen black window will appear on the virtual desktop; no action is needed, switch to another virtual desktop.
  5. Left-click the GWSL icon on the taskbar, select Shortcut Creator.
  6. In the pop-up window, fill in Shortcut Label as KDE Plasma, and Shortcut Command as dbus-launch startplasma-x11. If you have multiple WSL containers, select Run In as archlinux (i.e., your container name).
  7. Use the search tool to search for KDE Plasma on archlinux, open the folder, and copy the shortcut to the desktop to start Plasma.
  8. Double-click the shortcut.
  9. Return to the virtual desktop where GWSL is in full screen, and you will find that Plasma has started.

Some Tricks#

After this configuration, there will be a problem:

  • Switching virtual desktops will cause the GWSL window to minimize.

First, you need to have a Python 3.x environment installed and use python -m pip install pywin32 pyvda to install the Win32 API and the module for operating virtual desktops.

One More Problem#

At this point, we will find that the GWSL window still minimizes when switching virtual desktops. Thanks to the new features of Windows 11, we can set different wallpapers on multiple virtual desktops. However, the icons on the desktop cannot be separated. My solution is not to place files on the desktop.

Solution#

We can still write a script to set the GWSL window to a non-minimized state using Python's Win32 API and the powerful pyvda library.

from pyvda import VirtualDesktop
import win32gui, win32con
import asyncio

hwnd_title = {}
WINDOW_NAME = 'VcXsrv Server - Display Qin Yuzhen\'s Notebook:0.0' # Please modify this to your window name
GWSL_VD_NUM = 2 # Please modify this to your virtual desktop number
DEBUG = False # Show debug information

def get_all_hwnd(hwnd, mouse) -> dict:
    global hwnd_title
    hwnd_title = {}
    if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
        hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})

async def main():
    while True:
        if VirtualDesktop.current().number != GWSL_VD_NUM:
            await asyncio.sleep(0.1)
            continue
        win32gui.EnumWindows(get_all_hwnd, 0)
        hwnd = win32gui.FindWindow(None, WINDOW_NAME)
        win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL)
        if DEBUG:
            print('Unminimized', hwnd)
        await asyncio.sleep(0.1)
  
if __name__ == '__main__':
    asyncio.run(main())

This code uses AsyncIO to un-minimize the window, with a 0.1s interval between each check.

Reference: Python Control Window on Top, Author: DG Xi.

To make the transition between windows less abrupt, we can change the desktop background to match Plasma.

WSA#

If you have already installed the Windows Subsystem for Android, please uninstall it first.

MagiskOnWSA with GApps#

As the name suggests, this allows you to run Magisk (root access) and GApps (Google Services Framework and Google Play, etc.) on the Windows Subsystem for Android.

Unfortunately, before this article was written, LSPosed/MagiskOnWSA had already been banned. However, we can use the script provided by LSPosed/MagiskOnWSALocal to compile it under Linux.

If you have a GitHub account and know a bit about GitHub operations, it is recommended to use this method, although strictly speaking, this violates the terms of GitHub Actions and abuses the rights to build for free from open-source repositories. If your WSL container can access GitHub, it is recommended to compile manually.

The specific steps are as follows:

  1. Fork LSPosed/MagiskOnWSALocal.

  2. Create a new workflows folder under the .github folder.

  3. Create a .yml file with any name in the workflows folder and paste the Workflow. The Workflow has been placed on Luogu Cloud Clipboard.

  4. Click on the Actions tab on the webpage to enable Actions.

  5. Refer to steps 8-14 below to select the configuration you need, then run the Workflow.

    img

This method requires a "good network environment."

The specific steps are as follows:

  1. Use wsl --install -d Ubuntu to create a new Ubuntu container.

  2. Set the username and password in the new window.

  3. Enter the following script to change to the Tuna source:

    wget https://gitee.com/weizy1983/CNSolutions/raw/master/ubuntu/tunasource/UbuntuTunaSource.sh
    sudo chmod 777 UbuntuTunaSource.sh
    sudo ./UbuntuTunaSource.sh ubuntu20.04
    
  4. Type sudo apt-get install python3-pip winetricks -y to install dependencies.

  5. Open a directory in WSL, such as $Home\Documents.

  6. Clone the Git repository by typing git clone https://ghproxy.com/https://github.com/LSPosed/MagiskOnWSALocal.

  7. Enter the repository directory and run scripts/run.sh, type bash scripts/run.sh.

  8. When the Build arch selection page appears, use the default settings and press Enter to confirm.

  9. When the WSA release type appears, choose as needed; I chose insider slow/Beta Channel, use the up and down keys to move, space to select, and Enter to confirm.

  10. Then for Magisk version, choose as needed; I chose Beta Channel.

  11. Then it will ask Do you want to install gapps? choose Yes, and press Enter to confirm.

  12. Then for Variants of gapps, since OpenGApps does not yet officially support Android 12, you can only choose Pico.

  13. Then choose Do you want to keep amazon appStore?, select No, do not keep the Amazon App Store.

  14. For Root solution, choose Magisk as the root solution.

  15. The terminal will display your solution; just wait for it to complete.

    ARCH=x64
    RELEASE_TYPE=insider slow
    MAGISK_VER=beta
    GAPPS_VARIANT=pico
    REMOVE_AMAZON=remove
    ROOT_SOL=magisk
    

Installation#

  1. Settings -> Privacy and Security -> Developer Options -> Enable Developer Options.
  2. Open your MagiskOnWSALocal directory, if you used GitHub Actions, download the Artifact and extract it to a folder.
  3. Right-click Install.ps1, open it with PowerShell, and wait for it to complete.
  4. Search for and install WSATools from the Microsoft Store.
  5. Install Android SDK Platform Tools.
  6. Install a proxy software you like (if you do not have a "good network environment"), I use Clash for Android, then configure it.
  7. Enter Google Play Store, log in to your account, and install Microsoft Desktop.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.