PJ-Singh-001 / Cubic

The Official Web Site for Cubic (Custom Ubuntu ISO Creator) (https://github.com/PJ-Singh-001/Cubic)
821 stars 47 forks source link

How to remove all snaps from Ubuntu? #338

Closed PJ-Singh-001 closed 2 months ago

PJ-Singh-001 commented 2 months ago

Ubuntu Server 21.10 and Ubuntu Desktop 23.04 use a layered file system for the ISO, and they use the Subiquity snap package for the installer.

Considering these limitations, how to customize Ubuntu in Cubic to remove all snaps?

PJ-Singh-001 commented 2 months ago

How to remove all snaps from Ubuntu?

Requirements:

Background:

You can not use the snap command in Cubic's Terminal environment. Furthermore, you can not simply execute apt autoremove --purge snapd to remove all snap packages, like you could with previous versions of Ubuntu. The new installer, Subiquity, is a snap package itself, so it requires snapd to be present.

Approach:

Therefore we must leave snapd installed to allow the Live ISO to boot up. However, we can leverage cloud-config to automatically remove snapd once the installation is complete.

Limitations:

Although version 2024.09.88 of Cubic better reflects your changes in the Live environment, there are still some limitations. These are due to the snap based Subiquity installer and the layered file system Caonical adopted in Ubuntu Server 21.10 and Ubuntu Desktop 23.04.

Here is a list of known limitations.

  1. The Live Environment of your customized ISO will contain snapd and the standard snap packages. For Ubuntu Desktop, these are:

    • bare
    • core22
    • firefox
    • firmware-updater
    • gnome-42-2204
    • gtk-common-themes
    • snap-store
    • snapd
    • snapd-desktop-integration
    • thunderbird

    If you have installed the deb versions of Firefox or Thunderbird, your customized ISO will retain both the snap and deb versions of these applications while the user is in the Live Environment. After installation, only your deb packages will remain, because we will instruct the installer to remove snapd (along with all snap packages).

  2. You will not be able to edit /etv/default/grub because the file does not exist in Cubic's Terminal environment. If you want to make changes to this file and execute update-grub, use the late-commands section of coud-config, which we discuss below.

  3. I've noticed some App Icons do not render in the Live Environment. But they worked fine after installing the customized OS.

    There may be other issues or challenges that you encounter because certain files are stored in the "Installer Layer" of the ISO (which Cubic can not customize) or because the Live ISO requires snapd. You may be able to work around these by using late-commands, shown below.

Instructions:

  1. Launch Cubic version 2024.09.88 or newer.

  2. Customize your ISO. Do NOT remove snapd.

  3. Click on the Preseed tab, and create a file named autoinstall.yaml in the preseed folder.

    Screenshot from 2024-09-02 19-14-28

  4. Add the following to autoinstall.yaml:

    #cloud-config
    version: 1
    interactive-sections:
      # Prompt the user during installation.
      - locale
      - refresh
      - keyboard
      - network
      - storage
      - identity
      - timezone
      - user-data
    late-commands:
      # Remove snapd.
      - curtin in-target --target=/target -- rm -rf /var/cache/snapd
      - curtin in-target --target=/target -- rm -rf /var/lib/snapd
      - curtin in-target --target=/target -- rm -rf /var/cache/snapd
      - curtin in-target --target=/target -- apt --yes remove --purge snapd
      - curtin in-target --target=/target -- apt --yes install snapd

    Screenshot from 2024-09-02 21-02-35

    This is an autoinstall file which would normally install Ubuntu without user input. (This could be dangerous, especially if it overwrites your partitions). Therefore, under under interactive-sections, we specify that the installer must stop and ask the user for input for the listed sections.

    After the installation is complete, we instruct the installer to execute a series of commands in the newly installed OS. These commands are listed in the late-commands section, and this is where we remove snapd. You should not need to use sudo, but make sure the commands do not require any user interaction; this is why we use the --yes argument to apt. You could add other commands here as well. (Note that executing the snap command does not seem to work here because it requires the snapd service to be running in the target environment. If you try to execute a snap command here, it will actually run on your Live Environment, and this is not what you intended).

  5. Click on the Boot tab.

  6. Select grub.cfg in the left hand pane and add subiquity.autoinstallpath=/cdrom/preseed/autoinstall.yaml as shown. This will instruct the installer to use the autoinstall.yaml file you created in step 4. NOTE: On the Boot tab, you are editing the grub.cfg file used by the live ISO; this is NOT the grub.cfg used by your customized OS.

    set timeout=30
    
    loadfont unicode
    
    set menu_color_normal=white/black
    set menu_color_highlight=black/light-gray
    
    menuentry "Try or Install Ubuntu" {
        set gfxpayload=keep
        linux   /casper/vmlinuz subiquity.autoinstallpath=/cdrom/preseed/autoinstall.yaml --- quiet splash
        initrd  /casper/initrd.gz
    }
    menuentry "Ubuntu (safe graphics)" {
        set gfxpayload=keep
        linux   /casper/vmlinuz nomodeset subiquity.autoinstallpath=/cdrom/preseed/autoinstall.yaml --- quiet splash
        initrd  /casper/initrd.gz
    }
    grub_platform
    if [ "$grub_platform" = "efi" ]; then
    menuentry 'Boot from next volume' {
        exit 1
    }
    menuentry 'UEFI Firmware Settings' {
        fwsetup
    }
    else
    menuentry 'Test memory' {
        linux16 /boot/memtest86+x64.bin
    }
    fi

    Screenshot from 2024-09-02 19-42-42

  7. Select loopback.cfg in the left hand pane and add subiquity.autoinstallpath=/cdrom/preseed/autoinstall.yaml as shown. This will instruct the installer to use the autoinstall.yaml file you created in step 4. NOTE: On the Boot tab, you are editing the loopback.cfg file used by the live ISO; this is NOT the loopback.cfg used by your customized OS.

    menuentry "Try or Install Ubuntu" {
        set gfxpayload=keep
        linux   /casper/vmlinuz subiquity.autoinstallpath=/cdrom/preseed/autoinstall.yaml iso-scan/filename=${iso_path} --- quiet splash
        initrd  /casper/initrd.gz
    }
    menuentry "Ubuntu (safe graphics)" {
        set gfxpayload=keep
        linux   /casper/vmlinuz nomodeset subiquity.autoinstallpath=/cdrom/preseed/autoinstall.yaml iso-scan/filename=${iso_path} --- quiet splash
        initrd  /casper/initrd.gz
    }

    Screenshot from 2024-09-02 20-28-36

  1. Click Next and proceed to generate your Custom ISO.