跳转到主要内容

用于创建可引导虚拟机镜像的命令行工具

项目描述

Build Status

目录

关于

欢迎来到 image-bootstrap(及其小兄弟 directory-bootstrap)的家园。

image-bootstrap 是一个命令行工具,用于生成可引导虚拟机镜像并将它们写入指定的 块设备
目前 image-bootstrap 支持的Linux发行版包括:Arch、Debian、Gentoo、Ubuntu。
当传递 --openstack 参数时,镜像将 准备用于与OpenStack一起使用

directory-bootstrap 是一个命令行工具,用于将非Debian Linux发行版安装到指定的 目录(类似于 debootstrap 用于Debian/Ubuntu)。
directory-bootstrap 支持的发行版目前包括:Alpine Linux、Arch Linux、CentOS(最高到 7.x)、Gentoo 和 Void Linux。

历史

image-bootstrap 是基于 grml-debootstrap 的重写。主要用于创建可以启动为虚拟机的 Debian/Ubuntu 或 Arch 图像。

与 grml-debootstrap 相比,目前的 image-bootstrap

  • 只安装到块设备上,

  • 支持安装 GRUB 2.x、extlinux 或完全不安装引导加载程序的多种方法,

  • 不会将主机的计算机名泄露到生成的图像中,

  • 支持通过命令行传递根密码(以及其他用户和 shell 历史记录的眼睛),

  • 支持使用自定义的 /etc/resolv.conf 文件(例如,不要将您的家用路由器型号名称从 /etc/resolv.conf 泄露到图像中),

  • 支持安装 Arch Linux、Gentoo 和 Ubuntu(除了 Debian),

  • 能够创建 OpenStack 图像

  • 是用 Python 而不是 Bash/mksh 编写的,并且

  • 有更友好的终端输出。

directory-bootstrap 伴随着对 Arch Linux 的支持而出现。随后是对 Gentoo 的支持。在 2017 年 12 月 34c3 期间,对 Void Linux 和 CentOS chroots 的支持得以实现。2018 年 3 月,对 Alpine Linux chroots 的支持也出现了。

示例运行

以下是将 Debian jessie 安装到 LVM 卷 /dev/vg/lv 并使用 KVM 启动生成的图像的完整演示。

# ${EDITOR} root_password.txt

# sudo image-bootstrap --hostname jessie debian \
                                  --password-file root_password.txt /dev/vg/lv
     _                          __             __      __
    (_)_ _  ___ ____ ____  ___ / /  ___  ___  / /____ / /________ ____
   / /  ' \/ _ `/ _ `/ -_)/__// _ \/ _ \/ _ \/ __(_-</ __/ __/ _ `/ _ \
  /_/_/_/_/\_,_/\_, /\__/    /_.__/\___/\___/\__/___/\__/_/  \_,_/ .__/
               /___/                      v0.9.1 :: 2015-07-11  /_/

Software libre licensed under AGPL v3 or later.
Brought to you by Sebastian Pipping <sebastian@pipping.org>.
Please report bugs at https://github.com/hartwork/image-bootstrap.  Thank you!

Selected approach "chroot-grub2-drive" for bootloader installation.
Checking for blkid... /sbin/blkid
Checking for chmod... /bin/chmod
Checking for chroot... /usr/sbin/chroot
Checking for cp... /bin/cp
Checking for debootstrap... /usr/sbin/debootstrap
Checking for find... /usr/bin/find
Checking for kpartx... /sbin/kpartx
Checking for mkdir... /bin/mkdir
Checking for mkfs.ext4... /sbin/mkfs.ext4
Checking for mount... /bin/mount
Checking for parted... /sbin/parted
Checking for partprobe... /sbin/partprobe
Checking for rm... /bin/rm
Checking for rmdir... /bin/rmdir
Checking for sed... /bin/sed
Checking for tune2fs... /sbin/tune2fs
Checking for umount... /bin/umount
Checking for uname... /bin/uname
Checking for unshare... /usr/bin/unshare

Checking for known unsupported architecture/machine combination...
Checking if "/dev/vg/lv" is a block device...
Reading root password from file "/home/user1/root_password.txt"...
Unsharing Linux namespaces (mount, UTS/hostname)...
Partitioning "/dev/vg/lv"...
Activating partition devices...
Creating file system on "/dev/mapper/vg-lvp1"...
Creating directory "/mnt/tmpFczeFl"...
Mounting partitions...
Creating directory "/mnt/tmpFczeFl/etc"...
Writing file "/mnt/tmpFczeFl/etc/hostname"...
Writing file "/mnt/tmpFczeFl/etc/resolv.conf" (based on file "/etc/resolv.conf")...
Bootstrapping Debian "jessie" into "/mnt/tmpFczeFl"...
Writing file "/mnt/tmpFczeFl/etc/hostname"...
Writing file "/mnt/tmpFczeFl/etc/resolv.conf" (based on file "/etc/resolv.conf")...
Writing file "/mnt/tmpFczeFl/etc/fstab"...
Writing file "/mnt/tmpFczeFl/etc/network/interfaces"...
Running pre-chroot scripts...
Mounting non-disk file systems...
Setting root password...
Writing device map to "/mnt/tmpFczeFl/boot/grub/device.map" (mapping "(hd9999)" to "/dev/dm-8")...
Installing bootloader to device "/dev/vg/lv" (actually "/dev/dm-8", approach "chroot-grub2-drive")...
Generating GRUB configuration...
Post-processing GRUB config...
Generating initramfs...
Unmounting non-disk file systems...
Cleaning chroot apt cache...
Running post-chroot scripts...
Unmounting partitions...
Removing directory "/mnt/tmpFczeFl"...
Deactivating partition devices...
Done.

# sudo kvm -hda /dev/vg/lv

如果不使用 --color never,上面的输出实际上是彩色的。

加快速度

使用RAM而不是HDD/SSD

如果您反复运行 image-bootstrap 并且有足够的 RAM,您可能希望将图像创建在 RAM 存储而不是磁盘上。我使用了一个带有

  • 循环设备(以拥有块设备)

  • 在稀疏文件(以节省空间)

  • 在 tmpfs 挂载(以使用 RAM)

的设置。

# sudo mount -o remout,size=6g /tmp
# truncate --size 3g /tmp/disk3g
# LOOP_DEVICE="$(losetup --show -f /tmp/disk3g)"

# image-bootstrap .... arch ... "${LOOP_DEVICE}"
# qemu-img convert -p -f raw -O qcow2 "${LOOP_DEVICE}" /var/lib/arch-$(date -I).qcow2

# losetup -d "${LOOP_DEVICE}"
# rm /tmp/disk3g

Apt-Cacher NG -- 专门针对Debian/Ubuntu的缓存

例如(假设您已经将 /tmp 放在 RAM 中)

Polipo -- 一个通用的HTTP缓存

当创建多个图像时,一个本地的 Apt-Cacher NG 实例,并将 --mirror http://localhost:3142/debian 传递给 image-bootstrap 可能会很有用。

# http_proxy=http://127.0.0.1:8123/ image-bootstrap ...

当使用默认端口配置的 Polipo 时,调用 image-bootstrap

haveged -- 一个熵生成器

在图像创建过程中,可能需要生成加密密钥,例如用于 OpenSSH 服务器,至少暂时如此。由于密钥生成依赖于熵的可用性,因此在添加熵池较慢的环境中,图像创建可能需要更长的时间。为了加快速度,在主机系统上运行 haveged 可能 是一个选项,特别是由于所有密钥最终都应从图像中删除。否则,存在多个系统使用相同密钥,从而允许攻击的风险。我不确定 haveged 生成的熵的质量。使用风险自负。

Debian软件包

只要 image-bootstrap 不可用 Debian 中,您就可以轻松地从 Git 中创建自己的 image-bootstrap Debian 软件包,如下所示

# git clone https://github.com/hartwork/image-bootstrap.git
Cloning into 'image-bootstrap'...
[..]

# make -C image-bootstrap/ deb
[..]

# ls *.deb
image-bootstrap_0.9.1_all.deb

# sudo dpkg -i image-bootstrap_0.9.1_all.deb
[..]

用法(《--help》输出)

一般而言,用法是

image-bootstrap [..] DISTRIBUTION [..] DEVICE

image-bootstrap --hostname NAME [DISTRO_AGNOSTIC] DISTRIBUTION [DISTRO_SPECIFIC] DEVICE

更详细一点。

当前 --help 输出的转储将是

# image-bootstrap --help
usage: image-bootstrap [-h] [--version] [--color {never,always,auto}]
                       [--debug] [--quiet] [--verbose] [--arch ARCHITECTURE]
                       [--bootloader {auto,chroot-grub2-device,chroot-grub2-drive,host-extlinux,host-grub2-device,host-grub2-drive,none}]
                       [--bootloader-force] [--hostname NAME] [--openstack]
                       [--password PASSWORD | --password-file FILE]
                       [--resolv-conf FILE] [--disk-id ID]
                       [--first-partition-uuid UUID] [--machine-id ID]
                       [--scripts-pre DIRECTORY] [--scripts-chroot DIRECTORY]
                       [--scripts-post DIRECTORY] [--grub2-install COMMAND]
                       [--cache-dir DIRECTORY]
                       DISTRIBUTION ... DEVICE

Command line tool for creating bootable virtual machine images

positional arguments:
  DEVICE                block device to install to

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit

text output configuration:
  --color {never,always,auto}
                        toggle output color (default: auto)
  --debug               enable debugging
  --quiet               limit output to error messages
  --verbose             increase verbosity

machine configuration:
  --arch ARCHITECTURE   architecture (e.g. amd64)
  --bootloader {auto,chroot-grub2-device,chroot-grub2-drive,host-extlinux,host-grub2-device,host-grub2-drive,none}
                        approach to take during bootloader installation
                        (default: auto)
  --bootloader-force    apply more force when installing bootloader (default:
                        disabled)
  --hostname NAME       hostname to set (default: "machine")
  --openstack           prepare for use with OpenStack (default: disabled)
  --password PASSWORD   root password to set (default: password log-in
                        disabled)
  --password-file FILE  file to read root password from (default: password
                        log-in disabled)
  --resolv-conf FILE    file to copy nameserver entries from (default:
                        /etc/resolv.conf)
  --disk-id ID          specific disk identifier to apply, e.g. 0x12345678
  --first-partition-uuid UUID
                        specific UUID to apply to first partition, e.g.
                        c1b9d5a2-f162-11cf-9ece-0020afc76f16
  --machine-id ID       specific machine identifier to apply, e.g.
                        c1b9d5a2f16211cf9ece0020afc76f16

script integration:
  --scripts-pre DIRECTORY
                        scripts to run prior to chrooting phase, in
                        alphabetical order
  --scripts-chroot DIRECTORY
                        scripts to run during chrooting phase, in alphabetical
                        order
  --scripts-post DIRECTORY
                        scripts to run after chrooting phase, in alphabetical
                        order

command names:
  --grub2-install COMMAND
                        override grub2-install command

general configuration:
  --cache-dir DIRECTORY
                        directory to use for downloads (default:
                        /var/cache/directory-bootstrap/)

subcommands (choice of distribution):
  Run "image-bootstrap DISTRIBUTION --help" for details on options specific to that distribution.

  DISTRIBUTION          choice of distribution, pick from:
    arch                Arch Linux
    debian              Debian GNU/Linux
    gentoo              Gentoo
    ubuntu              Ubuntu

     _                          __             __      __
    (_)_ _  ___ ____ ____  ___ / /  ___  ___  / /____ / /________ ____
   / /  ' \/ _ `/ _ `/ -_)/__// _ \/ _ \/ _ \/ __(_-</ __/ __/ _ `/ _ \
  /_/_/_/_/\_,_/\_, /\__/    /_.__/\___/\___/\__/___/\__/_/  \_,_/ .__/
               /___/                      v2.0.0 :: 2020-02-28  /_/

Software libre licensed under AGPL v3 or later.
Brought to you by Sebastian Pipping <sebastian@pipping.org>.
Please report bugs at https://github.com/hartwork/image-bootstrap.  Thank you!

要显示 Debian 特定的选项,请运行 ..

# image-bootstrap debian --help
usage: image-bootstrap debian [-h] [--debootstrap COMMAND] [--release RELEASE]
                              [--mirror URL] [--debootstrap-opt OPTION]

optional arguments:
  -h, --help            show this help message and exit
  --release RELEASE     specify Debian release (default: jessie)
  --mirror URL          specify Debian mirror to use (e.g.
                        http://localhost:3142/debian for a local instance of
                        apt-cacher-ng; default:
                        http://httpredir.debian.org/debian)
  --debootstrap-opt OPTION
                        option to pass to debootstrap, in addition; can be
                        passed several times; use with --debootstrap-opt=...
                        syntax, i.e. with "="

command names:
  --debootstrap COMMAND
                        override debootstrap command

在管道中使用image-bootstrap的提示

如果您想将 image-bootstrap 运行在管道中,以便将其输出捕获到单个日志文件中的 stdout/stderr,请确保以非缓冲模式运行 image-bootstrap,例如

python -u image-bootstrap [OPTIONS] 2>&1 | tee my.log

Python setuptools 生成的默认 shebang 不使用 -u。也不容易让它传递 -u,因为较新版本的 env 命令确实允许传递命令参数。因此,stdout/stderr 不会同步,并且在日志文件中看到的错误输出将不会与 stdout 上的先前/后续输出完全对应。(有关更多详细信息,请参阅 问题 #71。)

此外,如果您正在使用 Bash 并且需要跟踪 image-bootstrap 的退出码,请在调用 image-bootstrap 之前确保运行 set -o pipefail。(有关更多详细信息,请参阅管道部分的GNU Bash 参考手册。)

已知限制

将 Debian wheezy 安装到循环设备

GRUB 1.99 在安装到循环设备方面存在问题。因此,使用 image-bootstrap 将例如 Debian wheezy 安装到循环设备需要

。引导加载程序方法 host-grub2-devicehost-grub2-drive,以及

。在运行 image-bootstrap 的系统上安装较新版本的 GRUB 2.x。

安装到分区块设备

Linux 不太喜欢分区中的分区。尽管如此,可以使用设备映射器来欺骗它。

以下是使用另一个分区作为临时目标来安装到分区的示例。临时目标必须

  • 至少有 2 GiB 的空间(以存放整个发行版)并且

  • 小于或等于实际目标(以便后续复制能够正常工作)。

# dmsetup create dm-linear-vda4 --table "0 $(blockdev --getsz /dev/vda4) linear /dev/vda4 0"
# image-bootstrap --openstack arch /dev/mapper/dm-linear-vda4
# partprobe /dev/mapper/dm-linear-vda4
# pv /dev/mapper/dm-linear-vda4p1 > /dev/vda2
# dmsetup remove dm-linear-vda4p1
# dmsetup remove dm-linear-vda4

/dev/vda2 是实际目标,/dev/vda4 是临时目标。)

还有其他方法可以实现相同的效果。

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪一个,请了解更多关于安装包的信息。

源代码分发

image-bootstrap-2.0.5.tar.gz (271.3 kB 查看哈希值)

上传时间: 源代码

构建版本

image_bootstrap-2.0.5-py3-none-any.whl (284.2 kB 查看哈希值)

上传时间: Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面