Filesystems

在 chroot 後解除安裝 sys/fs/cgroup/systemd,無需重新啟動

  • May 4, 2017

***背景:***我正在探索如何將普通的 LVM-on-LUKS Debian 9(“Stretch”)安裝從拇指驅動器(“源驅動器”)複製到 ZFS 格式的驅動器(“目標驅動器”),以便實現 ZFS-on-LUKS 安裝。我的過程是基於這個 HOWTO的。*我認為 ZFS 方面與我想要幫助的問題無關,但我提它以防萬一。

作為我的過程的一部分,當 Stretch 從源驅動器執行時,我將目標 ZFS 根 ( /) 文件系統安裝在/mnt. 然後我遞歸綁定:

  • /dev/mnt/dev
  • /proc/mnt/proc
  • /sys/mnt/sys.

然後我 chroot 進入/mnt.

(以後我chroot到時/mnt,我打算執行update-initramfsupdate-grub等,配置/boot分區的內容。)

然後我退出chroot,我的麻煩就開始了。我發現我可以解除安裝/mnt/devand /mnt/proc但不能/mnt/sys。後者拒絕解除安裝,因為它包含/mnt/sys/fs/cgroup/systemd,系統出於某種原因認為它“正在使用中”。重新格式化 ZFS 驅動器並重新啟動可以解決問題,但會大大減慢我的學習和文件過程的迭代速度。

我的問題是:

- 如何/mnt/sys在 chroot 後解除安裝而不重新啟動?

- 失敗(umount: /mnt/sys/fs/cgroup/systemd: target is busy)是預期的嗎?如果不是,我應該針對哪個軟體送出錯誤報告:umountcgroupssystemdLinux kernel還是其他?

這是(我認為)一個最小的工作範例。(如果您難以重現此內容並認為我可能錯過了一步,請告訴我。)首先,樣板:

# Activate the ZFS kernel module
/sbin/modprobe zfs

# Set variables
BOOT_POOL=bpool
ROOT_POOL=rpool
DIRS_TO_COPY=(boot bin etc home lib lib64 opt root sbin srv usr var)
FILES_TO_COPY=(initrd.img initrd.img.old vmlinuz vmlinuz.old)
VIRTUAL_FILESYSTEM_DIRS=(dev proc sys)

## Partition target drive
# 1MB BIOS boot partition
sgdisk -a2048 -n1:2048:4095     -t1:EF02 $1 -c 1:"bios_boot_partition"
wait
# 510MB partition for /boot ZFS filesystem
sgdisk -a2048 -n2:4096:1052671  -t2:BF07 $1 -c 2:"zfs_boot_partition"
wait
# Remaining drive space, except the last 510MiB in case of future need:
# partition to hold the LUKS container and the root ZFS filesystem
sgdisk -a2048 -n3:1052672:-510M -t3:8300 $1 -c 3:"luks_zfs_root_partition"
wait

# Before proceeding, ensure /dev/disk/by-id/ knows of these new partitions
partprobe
wait

# Create the /boot pool
zpool create -o ashift=12            \
            -O atime=off            \
            -O canmount=off         \
        -O compression=lz4      \
        -O normalization=formD  \
            -O mountpoint=/boot     \
            -R /mnt                 \
            $BOOT_POOL "$1"-part2
wait

# Create the LUKS container for the root pool
cryptsetup luksFormat "$1"-part3               \
                     --hash sha512            \
                     --cipher aes-xts-plain64 \
             --key-size 512
wait

# Open LUKS container that will contain the root pool
cryptsetup luksOpen "$1"-part3 "$DRIVE_SHORTNAME"3_crypt
wait

# Create the root pool
zpool create -o ashift=12           \
            -O atime=off           \
            -O canmount=off        \
            -O compression=lz4     \
            -O normalization=formD \
            -O mountpoint=/        \
            -R /mnt                \
            $ROOT_POOL /dev/mapper/"$DRIVE_SHORTNAME"3_crypt
wait

# Create ZFS datasets for the root ("/") and /boot filesystems
zfs create -o canmount=noauto -o mountpoint=/      "$ROOT_POOL"/debian
zfs create -o canmount=noauto -o mountpoint=/boot  "$BOOT_POOL"/debian

# Mount the root ("/") and /boot ZFS datasets
zfs mount "$ROOT_POOL"/debian
zfs mount "$BOOT_POOL"/debian

# Create datasets for subdirectories
zfs create                     -o setuid=off              "$ROOT_POOL"/home
zfs create -o mountpoint=/root                            "$ROOT_POOL"/home/root
zfs create -o canmount=off     -o setuid=off  -o exec=off "$ROOT_POOL"/var
zfs create -o com.sun:auto-snapshot=false                 "$ROOT_POOL"/var/cache
zfs create                                                "$ROOT_POOL"/var/log
zfs create                                                "$ROOT_POOL"/var/mail
zfs create                                                "$ROOT_POOL"/var/spool
zfs create -o com.sun:auto-snapshot=false     -o exec=on  "$ROOT_POOL"/var/tmp
zfs create                                                "$ROOT_POOL"/srv
zfs create -o com.sun:auto-snapshot=false     -o exec=on  "$ROOT_POOL"/tmp

# Set the `bootfs` property. ***TODO: IS THIS CORRECT???***
zpool set bootfs="$ROOT_POOL"/debian "$ROOT_POOL"

# Set correct permission for tmp directories
chmod 1777 /mnt/tmp
chmod 1777 /mnt/var/tmp

這是問題的核心部分:

# Copy Debian install from source drive to target drive
for i in "${DIRS_TO_COPY[@]}"; do 
   rsync --archive --quiet --delete /"$i"/ /mnt/"$i"
done
for i in "${FILES_TO_COPY[@]}"; do
   cp -a /"$i" /mnt/
done
for i in "${VIRTUAL_FILESYSTEM_DIRS[@]}"; do
   # Make mountpoints for virtual filesystems on target drive
   mkdir /mnt/"$i"
   # Recursively bind the virtual filesystems from source environment to the
   # target. N.B. This is using `--rbind`, not `--bind`.
   mount --rbind /"$i"  /mnt/"$i"
done

# `chroot` into the target environment
chroot /mnt /bin/bash --login

# (Manually exit from the chroot)

# Delete copied files
for i in "${DIRS_TO_COPY[@]}" "${FILES_TO_COPY[@]}"; do
   rm -r /mnt/"$i"
done

# Remove recursively bound virtual filesystems from target
for i in "${VIRTUAL_FILESYSTEM_DIRS[@]}"; do
   # First unmount them
   umount --recursive --verbose --force /mnt/"$i" || sleep 0
   wait
   # Then delete their mountpoints
   rmdir /mnt/"$i"
   wait
done

在這最後一步,我得到:

umount: /mnt/sys/fs/cgroup/systemd: target is busy
   (In some cases useful info about processes that
    use the device is found by lsof(8) or fuser(1).)

如果它有幫助:findmnt顯示完整的sys樹安裝了兩次:一次在/sys,同樣在/mnt/sys

  • ZFS 上的 Debian Jessie Root,CC BY-SA 3.0,作者 Richard Laager 和 George Melikov

您需要mount --make-rslave /mnt/"$i"在第一個掛載命令之後添加,以便為這些掛載點設置正確的傳播標誌。

它們保護主機免受 chroot 環境中的更改,並有助於防止像您這樣的阻塞情況。

引用自:https://unix.stackexchange.com/questions/362870