<!--
.. title: Подготовка chroot
.. slug: chroot-prepare
.. date: 2022-01-11 13:12:31 UTC+03:00
.. tags: linux
.. category: linux
.. link: 
.. description: 
.. type: text
-->

# Подготовка и запуск chroot

```bash
#!/bin/bash

target=/mnt/disk
source=/dev/sda2
#src_boot=/dev/sda1
#dst_boot=$target/boot/efi

echo "** mount disk..."
mkdir -p $target
mount $source $target

if [[ -v "${src_boot}" ]]; then
  echo "** mount boot..."
  mount ${src_boot} ${dst_boot}
fi

mount -t proc /proc $target/proc
mount -t sysfs /sys $target/sys
mount -o bind  /dev $target/dev
mount tmpfs $target/var/log -t tmpfs -o rw,noatime,nodiratime
mount tmpfs $target/tmp     -t tmpfs -o rw,nodev,nosuid,noatime,nodiratime
mount tmpfs $target/run     -t tmpfs -o rw,nodev,nosuid,noatime,nodiratime

echo "** ready to chroot to $target"

read -p "Run chroot (y/n)? " -n 1 -r
echo    # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
    # do dangerous stuff
    chroot $target
fi

```

# Размонтирование после выхода из chroot

```bash
#!/bin/sh

target=/mnt/disk
#dst_boot=$target/boot/efi

umount $target/var/log
umount $target/tmp
umount $target/run

umount $target/dev
umount $target/sys
umount $target/proc

if [[ -v "${src_boot}" ]]; then
  echo "** umount boot..."
  umount -R ${dst_boot}
fi

echo "** umount $target..."
umount -R $target

```
