之前我们已经将操作系统定制完成,无论是定制的包还是系统的包,都按照我们的ks要求进行了安装,现在遇到的一个问题是想要在操作系统安装完毕后的第一次boot,执行且只执行一次系统初始化脚本。
在网上有各种方法可以实现,但都有相对多的弊端,经过对比甄选,我们选择了使用服务的方式进行系统初始化,简单的逻辑是这样:
- 在ISO安装过程中,定制一个服务,这个服务调用的是我们的初始化脚本
- 在ks中标注这个服务为系统启动自动运行
- 在初始化脚本中的最后,将服务从系统自启动变更为系统启动时默认不运行
经过这样的逻辑,服务就只有在第一次启动的时候运行一次,其他的时间都不会自动运行。
下面举个例子:
首先定义服务,分别为一个os-init.service 的服务和一个os-init.sh的脚本,在通过之前的章节,我们可以自己定制一个rpm包,然后让这个rpm包将os-init.service放置在/etc/systemd/system/目录下,os-init.sh可以放置在自己的目录下,我这里假定放置在/opt/os-init.sh。然后把这个rpm包根据之前的章节进行ISO安装过程的自动化安装。两个文件如下:
# cat /etc/systemd/system/os-init.service [Unit] Description=OS Init Service After=rabbitmq-server.service mariadb.service [Service] User=root Group=root Type=simple ExecStart=/opt/os-init.sh KillMode=process [Install] WantedBy=multi-user.target
其中标红的是比较重要的地方
- After:表示这个service是在哪些服务之后才可以启动,当os -init.sh脚本中有一些指令需要服务支持的时候,就需要将其加入进来,比如我要初始化数据库和消息队列,所以我引入的就是rabbitmq-server和mariadb,这两个启动之后才运行这个服务
- User,Group:这两个表示服务以什么身份运行,非特殊要求,尽量不要用root
- ExecStart:指定可执行程序位置,目前示例中写的是直接执行,所以os-init.sh需要可执行权限,在rpm构建时要正确处理
# cat /opt/os-init.sh #!/bin/bash # init ssh root password login permision echo "PermitRootLogin yes" >> /etc/ssh/sshd_config echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config # init 8021q kmod modprobe 8021q # init rabbitmq-server start post shell rm -rf /usr/lib/systemd/system/rabbitmq-server.service mv /etc/uxcloud/rabbitmq-server.service /usr/lib/systemd/system/ # init mysql mysqladmin password mysqlpasswd mysql -uroot -pzaq12wsx -e "CREATE DATABASE testDB CHARACTER SET utf8 COLLATE utf8_bin" mysql -uroot -pzaq12wsx -e"GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mysqlpasswd' WITH GRANT OPTION;" # disable os-init systemctl disable os-init
这里注意要将最后标红的加上,这样就可以将os-init.sh只在操作系统安装完之后的第一次启动仅运行一次。
I was recommended this web site by means of my cousin. I’m not positive whether
or not this post is written by means of him as no one else understand such distinctive about my problem.
You are wonderful! Thank you!
Way cool! Some very valid points! I appreciate you penning this post and also the rest of the website is extremely good.| Hetty Sax Jenny
Quality articles, I am waiting for your next article. keep working Annabal Gearalt Jarret
Hello, I do think your site could be having internet browser compatibility problems. Whenever I look at your blog in Safari, it looks fine however, if opening in IE, it has some overlapping issues. I just wanted to provide you with a quick heads up! Aside from that, great blog!| Sharleen Fabio Melita
I conceive you have observed some very interesting points, thanks for the post. Margie Reese Oilla
Thank goodness for Peter! It is amazing all the items that need a tutorial now. Enjoy Pearl! Lilllie Nealson Demeyer
Greetings! Very helpful advice in this particular article! It is the little changes that produce the largest changes. Many thanks for sharing! Idelle Lucas Nonah
Hey! This is my first comment here so I just wanted to give a quick shout out and tell you I genuinely enjoy reading through your blog posts. Can you recommend any other blogs/websites/forums that deal with the same topics? Thanks for your time! Bryna Lanny Elora
Please visit the web-sites we comply with, including this a single, as it represents our picks in the web. Dianne Jared Bazar
I think the admin of this website is truly working hard in favor
of his web page, as here every data is quality based data.
Hi there colleagues, pleasant piece of writing and good arguments commented at this place, I am really enjoying by these. Suki Arturo Lodhia
Absolutely indited articles , appreciate it for information . Lanette Carl Mishaan
Thank you, I used Word Press. There are several free themes to choose from. Ray Justino Zerk
Very good blog article. Really thank you! Keep writing. Jobey Virge Monetta
Pretty! This has been an incredibly wonderful article. Ingaborg Rod Waddle
I love it when individuals come together and share thoughts. Nonnah Blane Gaby
I really like reading an article that will make men and women think. Shannon Marlin Icken
You completed several good points there. I did a search on the issue and found mainly people will consent with your blog. Moria Birch Beane
We have all the necessary skills without leaking away your previous data. Bonnee Neddie Foss
You have remarked very interesting points! ps decent internet site. Brandice Colan Rogers
The most typical loans are those accustomed to purchase a home, a mortgage, car finance, and to pay for student tuition. Marja Darin Vudimir
Our company are will rapidly and also properly produce a guarantee High-end improvement manhattan. Eleanore Teador Teagan
Im thankful for the blog article. Much thanks again. Want more. Nonna Corrie Albion
Pretty! This has been a really wonderful post. Thank you for supplying these details. Katey Callean Sup
I am regular reader, how are you everybody? This post posted at this web page is in fact nice. Lisha Levey Druci
Really nice style and design and superb articles , very little else we need : D. Elisabeth Meredith Haile
Very well research post. thanks for sharing with all. Cacilia Alexandros Nedra
Good handcuffs made of stainless steel. New handcuffs quality is primarily due to the high kind steel used for their output. Marillin Stanislaw Hornstein
Hi there, I do think your web site might be having browser compatibility issues. Mel Dorie Romola
I think that is among the most significant information for me. Kacie Victor Thurman
Helpful information. Lucky me I found your site accidentally, and I am stunned why this coincidence did not happened in advance! I bookmarked it. Aileen Harris Megen
I visited multiple sites however the audio quality for audio songs present at this web site is truly excellent. Marjorie Cristian Carbrey
What a data of un-ambiguity and preserveness of precious know-how concerning unpredicted feelings. Theressa Sayres Dietsche
Always a massive fan of linking to bloggers that I love but dont get a lot of link appreciate from. Mabelle Franny Mateusz
Really appreciate you sharing this blog. Really thank you! Great. Ara Nobe Izzy
Sorry for making you review this message which is more than likely to be considered by you as spam. Yes, spamming is a bad thing. Ardelle Jamaal Tippets
You made some clear points there. I looked on the internet for the subject matter and found most individuals will consent with your blog. Ella Dugald Paviour