❏ 站外平台:

如何构建一台网络引导服务器(四)

作者: Gregory Bartholomew 译者: LCTT qhwdw

| 2019-01-23 22:26   分享: 1    

在本系列教程中所构建的网络引导服务器有一个很重要的限制,那就是所提供的操作系统镜像是只读的。一些使用场景或许要求终端用户能够修改操作系统镜像。例如,一些教师或许希望学生能够安装和配置一些像 MariaDB 和 Node.js 这样的包来做为他们课程练习的一部分。

可写镜像的另外的好处是,终端用户“私人定制”的操作系统,在下次不同的工作站上使用时能够“跟着”他们。

修改 Bootmenu 应用程序以使用 HTTPS

为 bootmenu 应用程序创建一个自签名的证书:

  1. $ sudo -i
  2. # MY_NAME=$(</etc/hostname)
  3. # MY_TLSD=/opt/bootmenu/tls
  4. # mkdir $MY_TLSD
  5. # openssl req -newkey rsa:2048 -nodes -keyout $MY_TLSD/$MY_NAME.key -x509 -days 3650 -out $MY_TLSD/$MY_NAME.pem

验证你的证书的值。确保 Subject 行中 CN 的值与你的 iPXE 客户端连接你的网络引导服务器所使用的 DNS 名字是相匹配的:

  1. # openssl x509 -text -noout -in $MY_TLSD/$MY_NAME.pem

接下来,更新 bootmenu 应用程序去监听 HTTPS 端口和新创建的证书及密钥:

  1. # sed -i "s#listen => .*#listen => ['https://$MY_NAME:443?cert=$MY_TLSD/$MY_NAME.pem\&key=$MY_TLSD/$MY_NAME.key\&ciphers=AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA'],#" /opt/bootmenu/bootmenu.conf

注意 iPXE 当前支持的 加密算法是有限制的。

GnuTLS 要求 “CAPDACREAD_SEARCH” 能力,因此将它添加到 bootmenu 应用程序的 systemd 服务:

  1. # sed -i '/^AmbientCapabilities=/ s/$/ CAP_DAC_READ_SEARCH/' /etc/systemd/system/bootmenu.service
  2. # sed -i 's/Serves iPXE Menus over HTTP/Serves iPXE Menus over HTTPS/' /etc/systemd/system/bootmenu.service
  3. # systemctl daemon-reload

现在,在防火墙中为 bootmenu 服务添加一个例外规则并重启动该服务:

  1. # MY_SUBNET=192.0.2.0
  2. # MY_PREFIX=24
  3. # firewall-cmd --add-rich-rule="rule family='ipv4' source address='$MY_SUBNET/$MY_PREFIX' service name='https' accept"
  4. # firewall-cmd --runtime-to-permanent
  5. # systemctl restart bootmenu.service

使用 wget 去验证是否工作正常:

  1. $ MY_NAME=server-01.example.edu
  2. $ MY_TLSD=/opt/bootmenu/tls
  3. $ wget -q --ca-certificate=$MY_TLSD/$MY_NAME.pem -O - https://$MY_NAME/menu

添加 HTTPS 到 iPXE

更新 init.ipxe 去使用 HTTPS。接着使用选项重新编译 ipxe 引导加载器,以便它包含和信任你为 bootmenu 应用程序创建的自签名证书:

  1. $ echo '#define DOWNLOAD_PROTO_HTTPS' >> $HOME/ipxe/src/config/local/general.h
  2. $ sed -i 's/^chain http:/chain https:/' $HOME/ipxe/init.ipxe
  3. $ cp $MY_TLSD/$MY_NAME.pem $HOME/ipxe
  4. $ cd $HOME/ipxe/src
  5. $ make clean
  6. $ make bin-x86_64-efi/ipxe.efi EMBED=../init.ipxe CERT="../$MY_NAME.pem" TRUST="../$MY_NAME.pem"

你现在可以将启用了 HTTPS 的 iPXE 引导加载器复制到你的客户端上,并测试它能否正常工作:

  1. $ cp $HOME/ipxe/src/bin-x86_64-efi/ipxe.efi $HOME/esp/efi/boot/bootx64.efi

添加用户验证到 Mojolicious 中

为 bootmenu 应用程序创建一个 PAM 服务定义:

  1. # dnf install -y pam_krb5
  2. # echo 'auth required pam_krb5.so' > /etc/pam.d/bootmenu

添加一个库到 bootmenu 应用程序中,它使用 Authen-PAM 的 Perl 模块去执行用户验证:

  1. # dnf install -y perl-Authen-PAM;
  2. # MY_MOJO=/opt/bootmenu
  3. # mkdir $MY_MOJO/lib
  4. # cat << 'END' > $MY_MOJO/lib/PAM.pm
  5. package PAM;
  6. use Authen::PAM;
  7. sub auth {
  8. my $success = 0;
  9. my $username = shift;
  10. my $password = shift;
  11. my $callback = sub {
  12. my @res;
  13. while (@_) {
  14. my $code = shift;
  15. my $msg = shift;
  16. my $ans = "";
  17. $ans = $username if ($code == PAM_PROMPT_ECHO_ON());
  18. $ans = $password if ($code == PAM_PROMPT_ECHO_OFF());
  19. push @res, (PAM_SUCCESS(), $ans);
  20. }
  21. push @res, PAM_SUCCESS();
  22. return @res;
  23. };
  24. my $pamh = new Authen::PAM('bootmenu', $username, $callback);
  25. {
  26. last unless ref $pamh;
  27. last unless $pamh->pam_authenticate() == PAM_SUCCESS;
  28. $success = 1;
  29. }
  30. return $success;
  31. }
  32. return 1;
  33. END

以上的代码是一字不差是从 Authen::PAM::FAQ 的 man 页面中复制来的。

重定义 bootmenu 应用程序,以使它仅当提供了有效的用户名和密码之后返回一个网络引导模板:

  1. # cat << 'END' > $MY_MOJO/bootmenu.pl
  2. #!/usr/bin/env perl
  3. use lib 'lib';
  4. use PAM;
  5. use Mojolicious::Lite;
  6. use Mojolicious::Plugins;
  7. use Mojo::Util ('url_unescape');
  8. plugin 'Config';
  9. get '/menu';
  10. get '/boot' => sub {
  11. my $c = shift;
  12. my $instance = $c->param('instance');
  13. my $username = $c->param('username');
  14. my $password = $c->param('password');
  15. my $template = 'menu';
  16. {
  17. last unless $instance =~ /^fc[[:digit:]]{2}$/;
  18. last unless $username =~ /^[[:alnum:]]+$/;
  19. last unless PAM::auth($username, url_unescape($password));
  20. $template = $instance;
  21. }
  22. return $c->render(template => $template);
  23. };
  24. app->start;
  25. END

bootmenu 应用程序现在查找 lib 命令去找到相应的 WorkingDirectory。但是,默认情况下,对于 systemd 单元它的工作目录设置为服务器的 root 目录。因此,你必须更新 systemd 单元去设置 WorkingDirectory 为 bootmenu 应用程序的根目录:

  1. # sed -i "/^RuntimeDirectory=/ a WorkingDirectory=$MY_MOJO" /etc/systemd/system/bootmenu.service
  2. # systemctl daemon-reload

更新模块去使用重定义后的 bootmenu 应用程序:

  1. # cd $MY_MOJO/templates
  2. # MY_BOOTMENU_SERVER=$(</etc/hostname)
  3. # MY_FEDORA_RELEASES="28 29"
  4. # for i in $MY_FEDORA_RELEASES; do echo '#!ipxe' > fc$i.html.ep; grep "^kernel\|initrd" menu.html.ep | grep "fc$i" >> fc$i.html.ep; echo "boot || chain https://$MY_BOOTMENU_SERVER/menu" >> fc$i.html.ep; sed -i "/^:f$i$/,/^boot /c :f$i\nlogin\nchain https://$MY_BOOTMENU_SERVER/boot?instance=fc$i\&username=\${username}\&password=\${password:uristring} || goto failed" menu.html.ep; done

上面的最后的命令将生成类似下面的三个文件:

menu.html.ep

  1. #!ipxe
  2. set timeout 5000
  3. :menu
  4. menu iPXE Boot Menu
  5. item --key 1 lcl 1. Microsoft Windows 10
  6. item --key 2 f29 2. RedHat Fedora 29
  7. item --key 3 f28 3. RedHat Fedora 28
  8. choose --timeout ${timeout} --default lcl selected || goto shell
  9. set timeout 0
  10. goto ${selected}
  11. :failed
  12. echo boot failed, dropping to shell...
  13. goto shell
  14. :shell
  15. echo type 'exit' to get the back to the menu
  16. set timeout 0
  17. shell
  18. goto menu
  19. :lcl
  20. exit
  21. :f29
  22. login
  23. chain https://server-01.example.edu/boot?instance=fc29&username=${username}&password=${password:uristring} || goto failed
  24. :f28
  25. login
  26. chain https://server-01.example.edu/boot?instance=fc28&username=${username}&password=${password:uristring} || goto failed

fc29.html.ep

  1. #!ipxe
  2. kernel --name kernel.efi ${prefix}/vmlinuz-4.19.5-300.fc29.x86_64 initrd=initrd.img ro ip=dhcp rd.peerdns=0 nameserver=192.0.2.91 nameserver=192.0.2.92 root=/dev/disk/by-path/ip-192.0.2.158:3260-iscsi-iqn.edu.example.server-01:fc29-lun-1 netroot=iscsi:192.0.2.158::::iqn.edu.example.server-01:fc29 console=tty0 console=ttyS0,115200n8 audit=0 selinux=0 quiet
  3. initrd --name initrd.img ${prefix}/initramfs-4.19.5-300.fc29.x86_64.img
  4. boot || chain https://server-01.example.edu/menu

fc28.html.ep

  1. #!ipxe
  2. kernel --name kernel.efi ${prefix}/vmlinuz-4.19.3-200.fc28.x86_64 initrd=initrd.img ro ip=dhcp rd.peerdns=0 nameserver=192.0.2.91 nameserver=192.0.2.92 root=/dev/disk/by-path/ip-192.0.2.158:3260-iscsi-iqn.edu.example.server-01:fc28-lun-1 netroot=iscsi:192.0.2.158::::iqn.edu.example.server-01:fc28 console=tty0 console=ttyS0,115200n8 audit=0 selinux=0 quiet
  3. initrd --name initrd.img ${prefix}/initramfs-4.19.3-200.fc28.x86_64.img
  4. boot || chain https://server-01.example.edu/menu

现在,重启动 bootmenu 应用程序,并验证用户认证是否正常工作:

  1. # systemctl restart bootmenu.service

使得 iSCSI Target 可写

现在,用户验证通过 iPXE 可以正常工作,在用户连接时,你可以按需在只读镜像的上面创建每用户可写的overlay叠加层。使用一个 写时复制 的叠加层与简单地为每个用户复制原始镜像相比有三个好处:

  1. 副本创建非常快。这样就可以按需创建。
  2. 副本并不增加服务器上的磁盘使用。除了原始镜像之外,仅存储用户写入个人镜像的内容。
  3. 由于每个副本的扇区大多都是服务器的存储器上的相同扇区,在随后的用户访问这些操作系统的副本时,它们可能已经加载到内存中,这样就提升了服务器的性能,因为对内存的访问速度要比磁盘 I/O 快得多。

使用写时复制的一个潜在隐患是,一旦叠加层创建后,叠加层之下的镜像就不能再改变。如果它们改变,所有它们之上的叠加层将出错。因此,叠加层必须被删除并用新的、空白的进行替换。即便只是简单地以读写模式加载的镜像,也可能因为某些文件系统更新导致叠加层出错。

由于这个隐患,如果原始镜像被修改将导致叠加层出错,因此运行下列的命令,将原始镜像标记为不可改变:

  1. # chattr +i </path/to/file>

你可以使用 lsattr </path/to/file> 去查看不可改变标志,并可以使用 chattr -i </path/to/file> 取消设置不可改变标志。在设置了不可改变标志之后,即便是 root 用户或以 root 运行的系统进程也不修改或删除这个文件。

停止 tgtd.service 之后,你就可以改变镜像文件:

  1. # systemctl stop tgtd.service

当仍有连接打开的时候,运行这个命令一般需要一分钟或更长的时间。

现在,移除只读的 iSCSI 出口。然后更新模板中的 readonly-root 配置文件,以使镜像不再是只读的:

  1. # MY_FC=fc29
  2. # rm -f /etc/tgt/conf.d/$MY_FC.conf
  3. # TEMP_MNT=$(mktemp -d)
  4. # mount /$MY_FC.img $TEMP_MNT
  5. # sed -i 's/^READONLY=yes$/READONLY=no/' $TEMP_MNT/etc/sysconfig/readonly-root
  6. # sed -i 's/^Storage=volatile$/#Storage=auto/' $TEMP_MNT/etc/systemd/journald.conf
  7. # umount $TEMP_MNT

将 journald 日志从发送到内存修改回缺省值(如果 /var/log/journal 存在的话记录到磁盘),因为一个用户报告说,他的客户端由于应用程序生成了大量的系统日志而产生内存溢出错误,导致它的客户端被卡住。而将日志记录到磁盘的负面影响是客户端产生了额外的写入流量,这将在你的网络引导服务器上可能增加一些没有必要的 I/O。你应该去决定到底使用哪个选择 —— 记录到内存还是记录到硬盘 —— 哪个更合适取决于你的环境。

因为你的模板镜像在以后不能做任何的更改,因此在它上面设置不可更改标志,然后重启动 tgtd.service:

  1. # chattr +i /$MY_FC.img
  2. # systemctl start tgtd.service

现在,更新 bootmenu 应用程序:

  1. # cat << 'END' > $MY_MOJO/bootmenu.pl
  2. #!/usr/bin/env perl
  3. use lib 'lib';
  4. use PAM;
  5. use Mojolicious::Lite;
  6. use Mojolicious::Plugins;
  7. use Mojo::Util ('url_unescape');
  8. plugin 'Config';
  9. get '/menu';
  10. get '/boot' => sub {
  11. my $c = shift;
  12. my $instance = $c->param('instance');
  13. my $username = $c->param('username');
  14. my $password = $c->param('password');
  15. my $chapscrt;
  16. my $template = 'menu';
  17. {
  18. last unless $instance =~ /^fc[[:digit:]]{2}$/;
  19. last unless $username =~ /^[[:alnum:]]+$/;
  20. last unless PAM::auth($username, url_unescape($password));
  21. last unless $chapscrt = `sudo scripts/mktgt $instance $username`;
  22. $template = $instance;
  23. }
  24. return $c->render(template => $template, username => $username, chapscrt => $chapscrt);
  25. };
  26. app->start;
  27. END

新版本的 bootmenu 应用程序调用一个定制的 mktgt 脚本,如果成功,它将为每个它自己创建的新的 iSCSI 目标返回一个随机的 CHAP 密码。这个 CHAP 密码可以防止其它用户的 iSCSI 目标以间接方式挂载这个用户的目标。这个应用程序只有在用户密码认证成功之后才返回一个正确的 iSCSI 目标密码。

mktgt 脚本要加 sudo 前缀来运行,因为它需要 root 权限去创建目标。

$username$chapscrt 变量也传递给 render 命令,因此在需要的时候,它们也能够被纳入到模板中返回给用户。

接下来,更新我们的引导模板,以便于它们能够读取用户名和 chapscrt 变量,并传递它们到所属的终端用户。也要更新模板以 rw(读写)模式加载根文件系统:

  1. # cd $MY_MOJO/templates
  2. # sed -i "s/:$MY_FC/:$MY_FC-<%= \$username %>/g" $MY_FC.html.ep
  3. # sed -i "s/ netroot=iscsi:/ netroot=iscsi:<%= \$username %>:<%= \$chapscrt %>@/" $MY_FC.html.ep
  4. # sed -i "s/ ro / rw /" $MY_FC.html.ep

运行上面的命令后,你应该会看到如下的引导模板:

  1. #!ipxe
  2. kernel --name kernel.efi ${prefix}/vmlinuz-4.19.5-300.fc29.x86_64 initrd=initrd.img rw ip=dhcp rd.peerdns=0 nameserver=192.0.2.91 nameserver=192.0.2.92 root=/dev/disk/by-path/ip-192.0.2.158:3260-iscsi-iqn.edu.example.server-01:fc29-<%= $username %>-lun-1 netroot=iscsi:<%= $username %>:<%= $chapscrt %>@192.0.2.158::::iqn.edu.example.server-01:fc29-<%= $username %> console=tty0 console=ttyS0,115200n8 audit=0 selinux=0 quiet
  3. initrd --name initrd.img ${prefix}/initramfs-4.19.5-300.fc29.x86_64.img
  4. boot || chain https://server-01.example.edu/menu

注意:如果在 插入 变量后需要查看引导模板,你可以在 boot 命令之前,在它自己的行中插入 shell 命令。然后在你网络引导你的客户端时,iPXE 将在那里给你提供一个用于交互的 shell,你可以在 shell 中输入 imgstat 去查看传递到内核的参数。如果一切正确,你可以输入 exit 去退出 shell 并继续引导过程。

现在,通过 sudo 允许 bootmenu 用户以 root 权限去运行 mktgt 脚本(仅这个脚本):

  1. # echo "bootmenu ALL = NOPASSWD: $MY_MOJO/scripts/mktgt *" > /etc/sudoers.d/bootmenu

bootmenu 用户不应该写访问 mktgt 脚本或在它的家目录下的任何其它文件。在 /opt/bootmenu 目录下的所有文件的属主应该是 root,并且不应该被其它任何 root 以外的用户可写。

sudo 在使用 systemd 的 DynamicUser 选项下不能正常工作,因此创建一个普通用户帐户,并设置 systemd 服务以那个用户运行:

  1. # useradd -r -c 'iPXE Boot Menu Service' -d /opt/bootmenu -s /sbin/nologin bootmenu
  2. # sed -i 's/^DynamicUser=true$/User=bootmenu/' /etc/systemd/system/bootmenu.service
  3. # systemctl daemon-reload

最后,为写时复制覆盖创建一个目录,并创建管理 iSCSI 目标的 mktgt 脚本和它们的覆盖支持存储:

  1. # mkdir /$MY_FC.cow
  2. # mkdir $MY_MOJO/scripts
  3. # cat << 'END' > $MY_MOJO/scripts/mktgt
  4. #!/usr/bin/env perl
  5. # if another instance of this script is running, wait for it to finish
  6. "$ENV{FLOCKER}" eq 'MKTGT' or exec "env FLOCKER=MKTGT flock /tmp $0 @ARGV";
  7. # use "RETURN" to print to STDOUT; everything else goes to STDERR by default
  8. open(RETURN, '>&', STDOUT);
  9. open(STDOUT, '>&', STDERR);
  10. my $instance = shift or die "instance not provided";
  11. my $username = shift or die "username not provided";
  12. my $img = "/$instance.img";
  13. my $dir = "/$instance.cow";
  14. my $top = "$dir/$username";
  15. -f "$img" or die "'$img' is not a file";
  16. -d "$dir" or die "'$dir' is not a directory";
  17. my $base;
  18. die unless $base = `losetup --show --read-only --nooverlap --find $img`;
  19. chomp $base;
  20. my $size;
  21. die unless $size = `blockdev --getsz $base`;
  22. chomp $size;
  23. # create the per-user sparse file if it does not exist
  24. if (! -e "$top") {
  25. die unless system("dd if=/dev/zero of=$top status=none bs=512 count=0 seek=$size") == 0;
  26. }
  27. # create the copy-on-write overlay if it does not exist
  28. my $cow="$instance-$username";
  29. my $dev="/dev/mapper/$cow";
  30. if (! -e "$dev") {
  31. my $over;
  32. die unless $over = `losetup --show --nooverlap --find $top`;
  33. chomp $over;
  34. die unless system("echo 0 $size snapshot $base $over p 8 | dmsetup create $cow") == 0;
  35. }
  36. my $tgtadm = '/usr/sbin/tgtadm --lld iscsi';
  37. # get textual representations of the iscsi targets
  38. my $text = `$tgtadm --op show --mode target`;
  39. my @targets = $text =~ /(?:^T.*\n)(?:^ .*\n)*/mg;
  40. # convert the textual representations into a hash table
  41. my $targets = {};
  42. foreach (@targets) {
  43. my $tgt;
  44. my $sid;
  45. foreach (split /\n/) {
  46. /^Target (\d+)(?{ $tgt = $targets->{$^N} = [] })/;
  47. /I_T nexus: (\d+)(?{ $sid = $^N })/;
  48. /Connection: (\d+)(?{ push @{$tgt}, [ $sid, $^N ] })/;
  49. }
  50. }
  51. my $hostname;
  52. die unless $hostname = `hostname`;
  53. chomp $hostname;
  54. my $target = 'iqn.' . join('.', reverse split('\.', $hostname)) . ":$cow";
  55. # find the target id corresponding to the provided target name and
  56. # close any existing connections to it
  57. my $tid = 0;
  58. foreach (@targets) {
  59. next unless /^Target (\d+)(?{ $tid = $^N }): $target$/m;
  60. foreach (@{$targets->{$tid}}) {
  61. die unless system("$tgtadm --op delete --mode conn --tid $tid --sid $_->[0] --cid $_->[1]") == 0;
  62. }
  63. }
  64. # create a new target if an existing one was not found
  65. if ($tid == 0) {
  66. # find an available target id
  67. my @ids = (0, sort keys %{$targets});
  68. $tid = 1; while ($ids[$tid]==$tid) { $tid++ }
  69. # create the target
  70. die unless -e "$dev";
  71. die unless system("$tgtadm --op new --mode target --tid $tid --targetname $target") == 0;
  72. die unless system("$tgtadm --op new --mode logicalunit --tid $tid --lun 1 --backing-store $dev") == 0;
  73. die unless system("$tgtadm --op bind --mode target --tid $tid --initiator-address ALL") == 0;
  74. }
  75. # (re)set the provided target's chap password
  76. my $password = join('', map(chr(int(rand(26))+65), 1..8));
  77. my $accounts = `$tgtadm --op show --mode account`;
  78. if ($accounts =~ / $username$/m) {
  79. die unless system("$tgtadm --op delete --mode account --user $username") == 0;
  80. }
  81. die unless system("$tgtadm --op new --mode account --user $username --password $password") == 0;
  82. die unless system("$tgtadm --op bind --mode account --tid $tid --user $username") == 0;
  83. # return the new password to the iscsi target on stdout
  84. print RETURN $password;
  85. END
  86. # chmod +x $MY_MOJO/scripts/mktgt

上面的脚本将做以下五件事情:

  1. 创建 /<instance>.cow/<username> 稀疏文件(如果不存在的话)。
  2. 创建 /dev/mapper/<instance>-<username> 设备节点作为 iSCSI 目标的写时复制支持存储(如果不存在的话)。
  3. 创建 iqn.<reverse-hostname>:<instance>-<username> iSCSI 目标(如果不存在的话)。或者,如果已存在了,它将关闭任何已存在的连接,因为在任何时刻,镜像只能以只读模式从一个地方打开。
  4. 它在 iqn.<reverse-hostname>:<instance>-<username> iSCSI 目标上(重新)设置 chap 密码为一个新的随机值。
  5. (如果前面的所有任务都成功的话)它在 标准输出 上显示新的 chap 密码。

你应该可以在命令行上通过使用有效的测试参数来运行它,以测试 mktgt 脚本能否正常工作。例如:

  1. # echo `$MY_MOJO/scripts/mktgt fc29 jsmith`

当你从命令行上运行时,mktgt 脚本应该会输出 iSCSI 目标的一个随意的八字符随机密码(如果成功的话)或者是出错位置的行号(如果失败的话)。

有时候,你可能需要在不停止整个服务的情况下删除一个 iSCSI 目标。例如,一个用户可能无意中损坏了他的个人镜像,在那种情况下,你可能需要按步骤撤销上面的 mktgt 脚本所做的事情,以便于他下次登入时他将得到一个原始镜像。

下面是用于撤销的 rmtgt 脚本,它以相反的顺序做了上面 mktgt 脚本所做的事情:

  1. # mkdir $HOME/bin
  2. # cat << 'END' > $HOME/bin/rmtgt
  3. #!/usr/bin/env perl
  4. @ARGV >= 2 or die "usage: $0 <instance> <username> [+d|+f]\n";
  5. my $instance = shift;
  6. my $username = shift;
  7. my $rmd = ($ARGV[0] eq '+d'); #remove device node if +d flag is set
  8. my $rmf = ($ARGV[0] eq '+f'); #remove sparse file if +f flag is set
  9. my $cow = "$instance-$username";
  10. my $hostname;
  11. die unless $hostname = `hostname`;
  12. chomp $hostname;
  13. my $tgtadm = '/usr/sbin/tgtadm';
  14. my $target = 'iqn.' . join('.', reverse split('\.', $hostname)) . ":$cow";
  15. my $text = `$tgtadm --op show --mode target`;
  16. my @targets = $text =~ /(?:^T.*\n)(?:^ .*\n)*/mg;
  17. my $targets = {};
  18. foreach (@targets) {
  19. my $tgt;
  20. my $sid;
  21. foreach (split /\n/) {
  22. /^Target (\d+)(?{ $tgt = $targets->{$^N} = [] })/;
  23. /I_T nexus: (\d+)(?{ $sid = $^N })/;
  24. /Connection: (\d+)(?{ push @{$tgt}, [ $sid, $^N ] })/;
  25. }
  26. }
  27. my $tid = 0;
  28. foreach (@targets) {
  29. next unless /^Target (\d+)(?{ $tid = $^N }): $target$/m;
  30. foreach (@{$targets->{$tid}}) {
  31. die unless system("$tgtadm --op delete --mode conn --tid $tid --sid $_->[0] --cid $_->[1]") == 0;
  32. }
  33. die unless system("$tgtadm --op delete --mode target --tid $tid") == 0;
  34. print "target $tid deleted\n";
  35. sleep 1;
  36. }
  37. my $dev = "/dev/mapper/$cow";
  38. if ($rmd or ($rmf and -e $dev)) {
  39. die unless system("dmsetup remove $cow") == 0;
  40. print "device node $dev deleted\n";
  41. }
  42. if ($rmf) {
  43. my $sf = "/$instance.cow/$username";
  44. die "sparse file $sf not found" unless -e "$sf";
  45. die unless system("rm -f $sf") == 0;
  46. die unless not -e "$sf";
  47. print "sparse file $sf deleted\n";
  48. }
  49. END
  50. # chmod +x $HOME/bin/rmtgt

例如,使用上面的脚本去完全删除 fc29-jsmith 目标,包含它的支持存储设备节点和稀疏文件,可以按下列方式运行命令:

  1. # rmtgt fc29 jsmith +f

一旦你验证 mktgt 脚本工作正常,你可以重启动 bootmenu 服务。下次有人从网络引导时,他们应该能够接收到一个他们可以写入的、可”私人定制“的网络引导镜像的副本:

  1. # systemctl restart bootmenu.service

现在,就像下面的截屏示范的那样,用户应该可以修改根文件系统了:


via: https://fedoramagazine.org/how-to-build-a-netboot-server-part-4/

作者:Gregory Bartholomew 选题:lujun9972 译者:qhwdw 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出



最新评论

从 2025.1.15 起,不再提供评论功能
LCTT 译者
qhwdw 💎
共计翻译: 195.5 篇 | 共计贡献: 510
贡献时间:2017-10-31 -> 2019-03-24
访问我的 LCTT 主页 | 在 GitHub 上关注我


返回顶部

分享到微信

打开微信,点击顶部的“╋”,
使用“扫一扫”将网页分享至微信。