0

Everything in Linux-based systems is a file. Everytime I install Ubuntu, I choose a root partition. Now, I want to create a root filesystem, but in a file, for example ~/ubuntu.img. I've got another Linux distro (based on Ubuntu) as the main OS. Is there a way to install (s)ubiquity and select ~/ubuntu.img as the root filesystem? As far as I know, the installer mounts the root filesystem at /target and uses chroot. Maybe I can mount my root filesystem there and install manually?

I want to do this because:

  1. I don't want to install more than 1 OS and dual boot.
  2. My main OS doesn't support some packages or has some dependency issues when I want to compile some projects.

If I'm able to install Ubuntu this way, I'll use chroot to install packages inside the root filesystem image.

adazem009
  • 1,032
  • 1
    Sure, just mount it wherever and run debootstrap on it. But I don't see the point - you could pretty much get the same result with Docker or LXC containers with less hassle. – muru Mar 15 '21 at 07:34
  • @muru Thanks for the response, I'll try to mount it. I want to use chroot because I'm used to it... I also need to access USB devices from the chroot environment and I thought it's the easiest way. – adazem009 Mar 15 '21 at 07:36

2 Answers2

0

I've managed to mount the image from this answer. You can use this link to download it.

For some reason I wasn't able to read the image directly using tools like parted, cfdisk or fdisk. I had to flash the image into a 16GB (or more) USB drive, and then copy the root partition using dd, or the GNOME Disk utility.

So, I copied the root partition to ~/ubuntu.img and set up the chroot environment:

cd # go to the directory where the root filesystem image is located
mkdir ubuntu
sudo mount -o loop ubuntu.img ubuntu
sudo mount -t proc /proc ./ubuntu/proc
sudo mount -t sysfs /sys ./ubuntu/sys
sudo mount -o bind /dev ./ubuntu/dev
sudo chroot ./ubuntu

I can even access USB devices from the chroot environment.

You've probably noticed that the internet connection works only partially - you can ping any server if you specify the IP address, but domains can't be resolved. This command should fix it:

echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf

The downside of this method is that you always have to prepare the chroot environment if you need to access USB devices. But, that can be simplified by writing a shell script.

To clean up after you leave the chroot environment (and no processes are running there):

sudo umount ./ubuntu/{proc,sys,dev}
sudo umount ./ubuntu

I think there's a way to manually install Ubuntu on top of a base system. There are tools like debootstrap, but I didn't try that. Feel free to post another answer with that :)

adazem009
  • 1,032
0

What you suggest is essentially the idea behind a virtual machine. Why not simply use VirtualBox and install Ubuntu as a VirtualBox VM?

HuHa
  • 3,385
  • That could work for Android ROM building, but 1. the USB devices don't work the same way there; 2. I'd get less performance in a VM – adazem009 Mar 15 '21 at 13:00
  • Less performance? Really? Did you measure that, or is that just an assumption? – HuHa Mar 15 '21 at 13:03
  • chroot is different than a VM. In a VM, another Linux kernel gets loaded with the full OS. Using chroot will use the kernel from my main OS, and only execute binaries in the chroot environment. This way, I don't need any virtualization, which could slow down things. – adazem009 Mar 15 '21 at 13:05
  • Right, it will use the kernel from that other OS; and this is where the trouble starts. The glibc etc. in what would be the guest system may or may not work with that kernel, depending on how different they are. Happy experimenting; but I think you will end up with something like docker or virtual machines anyway. – HuHa Mar 15 '21 at 13:08
  • As I mentioned before, my main OS is based on Ubuntu (it's Pop!_OS) and I've manually installed one of the Ubuntu mainline kernels (https://kernel.ubuntu.com/~kernel-ppa/mainline/) to get my touchpad working. – adazem009 Mar 15 '21 at 13:09
  • What could be different, is the initrd. But that'd happen only if Pop!_OS had its own kernel modules or something. – adazem009 Mar 15 '21 at 13:11