0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WSLが開けなくなったので、再インストールしてみた

Posted at

こんにちは、 f-shoyaです。
普段開発などに使用しているWSLが起動しなくなったため、 内部データを維持しつつ、再インストールを行いました。

起きたこと

普段通り、 業務でWSLを起動しようとすると、 下記のようなエラーが表示され、 WSLが起動しませんでした。

コマンドプロンプト
> wsl
ディスク 'C:\Program Files\WSL\system.vhd' を WSL2 にアタッチできませんでした: 指定されたファイルが見つかりません。

エラー コード: Wsl/Service/CreateInstance/CreateVm/MountVhd/HCS/ERROR_FILE_NOT_FOUND

通常、VSCodeを開くと前回触ったWSL上のコードが開かれます。
そのため、 VSCodeも試しに起動しましたが、それでも開かず・・・

VSCodeのターミナル
[2025-02-14 02:41:15.392] Starting VS Code Server inside WSL (wsl2) 
[2025-02-14 02:41:15.392] Windows build: 26100. Multi distro support: available. WSL path support: enabled 
[2025-02-14 02:41:15.392] Scriptless setup: false 
[2025-02-14 02:41:15.393] No shell environment set or found for current distro. 
[2025-02-14 02:41:15.626] WSL daemon log file: 
[2025-02-14 02:41:15.630] Probing if server is already installed: if [ -d ~/.vscode-server/bin/31c37ee8f63491495ac49e43b8544550fbae4533 ]; then printf 'install-found '; fi; if [ -f /etc/alpine-release ]; then printf alpine-; fi; uname -m 
[2025-02-14 02:41:15.630] NodeExecServer run: C:\WINDOWS\System32\wsl.exe -d Ubuntu -e sh -c if [ -d ~/.vscode-server/bin/31c37ee8f63491495ac49e43b8544550fbae4533 ]; then printf 'install-found '; fi; if [ -f /etc/alpine-release ]; then printf alpine-; fi; uname -m 
[2025-02-14 02:41:17.410] Unable to detect if server is already installed: Error: Failed to probe if server is already installed: code: Failed to probe if server is already installed: code: 4294967295, , �0�0�0�0 'C:\Program Files\WSL\system.vhd' �0 WSL2 k0�0�0�0�0g0M0~0[0�0g0W0_0: c�[U0�0_0�0�0�0�0L0��d0K0�0~0[0�00 
[2025-02-14 02:41:17.410] �0�0�0 �0�0�0: Wsl/Service/CreateInstance/CreateVm/MountVhd/HCS/ERROR_FILE_NOT_FOUND 
[2025-02-14 02:41:17.410] 
[2025-02-14 02:41:17.411] NodeExecServer run: C:\WINDOWS\System32\wsl.exe -d Ubuntu sh -c '"$VSCODE_WSL_EXT_LOCATION/scripts/wslServer.sh" 31c37ee8f63491495ac49e43b8544550fbae4533 stable code-server .vscode-server --host=127.0.0.1 --port=0 --connection-token=2486168096-2963956356-1033736603-3854654579 --use-host-proxy --without-browser-env-var --disable-websocket-compression --accept-server-license-terms --telemetry-level=all'

WSLをそれほど利用していなければ、 単純にWSLを再インストールするなどで無理やり復旧できそうです。
しかし、業務で利用しているデータがかなりあるため、 WSL内のデータを残しつつ、この不具合を解消したいです。

解決手順

ext4.vhdxを確認

WSLのデータはext4.vhdxというファイルに保存されているので、まずは場所を確認します。
ext4.vhdxは私の場合、
C:\Users\[ユーザー名]\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_xxx(任意の英数字文字列)\LocalStateに存在しました。
コマンドプロンプトで下記のコマンドを実行することで、ファイルの場所を確認することもできます。

コマンドプロンプト
> WHERE ext4.vhdx /R \ 

ext4.vhdxの退避

WSLをアンインストールした際に、 内部データを消去しないよう、ext4.vhdxを退避します。
下記コマンドでは、分かりやすくC:\に移動しています。

コマンドプロンプト
> MOVE [ext4.vhdxのディレクトリパス]\ext4.vhdx C:\

WSLの再インストール

退避が完了したら、WSLを再インストールします。
一旦、アンインストールし、

コマンドプロンプト
> wsl --uninstall

再度インストールします。

コマンドプロンプト
> wsl --install

退避したext4.vhdxの復元

最後に、退避したext4.vhdxを復元します。
再度インストールしたWSLのext4.vhdxへのパスが変更されている場合があるため、ディレクトリパスを再度確認します。

コマンドプロンプト
> WHERE ext4.vhdx /R \ 

上記コマンドで出力された、新たなext4.vhdxの場所に先ほど退避したext4.vhdxを移動します。
上書きが必要な場合、上書きします。

コマンドプロンプト
> MOVE C:\ext4.vhdx [ext4.vhdxのディレクトリパス]\ext4.vhdx
[ext4.vhdxのディレクトリパス]\ext4.vhdx を上書きしますか? (Yes/No/All): Yes
        1 個のファイルを移動しました。

動作確認

不具合が解消され、以前までのデータをWSL上で扱えるか確認します。

コマンドプロンプト
> wsl

正常にシェルが起動し、以前のデータを扱えるようになったら成功です。

最後に

業務をしていると、たまにこのようなトラブルに遭遇して時間を溶かしてしまいがちですが、本来の作業が進まず生産的ではありません(問題解決能力が付くので、無意味ではありませんが)。
この記事が誰かの参考になれば幸いです。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?