You are not logged in.

#1 2016-02-05 10:53:14

timfayz
Member
From: Kazan, Russia
Registered: 2016-01-14
Posts: 59
Website

What is the difference between chroot and systemctl switch-root?

Hello, I'm very new to Arch and to Linux as a whole, so there is a question: what is the difference between "chroot" and "systemctl switch-root" commands?
When I installed Arch there were two options to use arch-chroot script or do the same manually by chroot in order to install some base packages. After I found systemctl's one and I'm confused... I'm just curious and I havn't tried it yet, because I don't know what it does.

Offline

#2 2016-02-05 13:45:37

atomicbeef
Member
Registered: 2015-09-30
Posts: 98

Re: What is the difference between chroot and systemctl switch-root?

From the systemctl man page:

man systemctl wrote:

Switches to a different root directory and executes a new system manager process below it. This is intended for usage in inital RAM disks ("initrd"), and will transition from the initrd's system manager process (a.k.a. "init" process) to the main system manager process.

From the chroot man page:

man chroot wrote:

chroot - run command or interactive shell with special root directory

Chroot and systemctl switch-root are two different things, so don't try to use them interchangeabley.

Offline

#3 2016-02-05 17:59:01

Bruinn
Member
Registered: 2016-01-22
Posts: 4

Re: What is the difference between chroot and systemctl switch-root?

Hello Timfayz,

Here is my take on the differences between the two systems:

chroot is a system call that shifts what a process expects to be the root directory.  In other words, you can mount a new file system to the root directory, like a usb stick, and chroot into the new file system.  Now the system will no longer see the root directory as root.  It will see the new file system as root.  The only problem with this is the new file system will inherit the old system calls, processes and environment variables. 

When using the systemd method of changing root this does not happen.  You are guaranteed to invoke a completely independent environment with localized system calls, processes and environment variables.

I hope that helps.

Offline

#4 2016-02-05 19:13:00

atomicbeef
Member
Registered: 2015-09-30
Posts: 98

Re: What is the difference between chroot and systemctl switch-root?

Bruinn,

Your take is incorrect. Switch-root cannot be used as a chroot replacement. The reason for this is that chroot only changes the root directory of a single process, whereas switch-root changes the entire system's root directory. The man page for switch-root specifically says that it is intended for initial RAM disks. You can also guarantee clean environment variables with chroot by doing something such as this:

env -i TERM=$TERM SHELL=/bin/bash HOME=$HOME $(which chroot) [chroot-dir] /bin/bash

If you are looking for completely independent environments, you would either be using a virtual machine or some form of lightweight virtualization such as LXC.

Offline

#5 2016-02-05 19:53:15

Bruinn
Member
Registered: 2016-01-22
Posts: 4

Re: What is the difference between chroot and systemctl switch-root?

Atomicbeef,

I never stated Switch-root should be used as a chroot replacement.  I was stating my take on the differences between the two.  My understanding of the two systems is from a blog article written by Lennart Poettering.  I will look up the blog post and post it shortly.

Offline

#6 2016-02-05 19:57:32

Bruinn
Member
Registered: 2016-01-22
Posts: 4

Re: What is the difference between chroot and systemctl switch-root?

Here is the link I was referring to.  If I mislead anyone I apologize. 

http://0pointer.de/blog/projects/changing-roots.html

Offline

#7 2016-02-05 20:58:19

atomicbeef
Member
Registered: 2015-09-30
Posts: 98

Re: What is the difference between chroot and systemctl switch-root?

I don't think that blog article you linked to is talking about switch-root. I couldn't find any mention of switch-root and the article mainly talked about using chroot in systemd services. From what I've found in my research, switch-root is only supposed to be used during the early boot process.

Offline

#8 2016-02-06 09:41:55

timfayz
Member
From: Kazan, Russia
Registered: 2016-01-14
Posts: 59
Website

Re: What is the difference between chroot and systemctl switch-root?

Thank you guys anyway! Both of your opinions are worth to me. At least, now I have veryyy superficial difference understanding!

Offline

Board footer

Powered by FluxBB