OpenStack服务开机自启动
来源:互联网 发布:光纤交换机端口 编辑:程序博客网 时间:2024/05/16 19:29
1 浅析 Linux 初始化 init 系统
第1部分sysvinit: https://www.ibm.com/developerworks/cn/linux/1407_liuming_init1/
第2部分UpStart: https://www.ibm.com/developerworks/cn/linux/1407_liuming_init2/
第3部分Systemd: https://www.ibm.com/developerworks/cn/linux/1407_liuming_init3/
1.1 SysVInit
1.2 UpStart
1.3 Systemd
1.3.1 基本
1 System单元的概念
系统初始化需要做的事情非常多。需要启动后台服务,比如启动 SSHD 服务;需要做配置工作,比如挂载文件系统。这个过程中的每一步都被 systemd 抽象为一个配置单元,即 unit。
单元的常见类型:
service unit : 文件扩展名为.service,用于定义系统类服务
target unit : 文件扩展为.target,用于实现模拟”运行级别”
device unit : 文件扩展为.device,用于定义内核识别的设备。
mount unit : 文件扩展为.mount,定义文件系统挂载点,利用logind服务,为用户的会话进程分配CGroup资源
socket unit : 文件扩展为.socket,用于标识进程间通信的socket文件
snapshot unit : 文件扩展为.snapshot,管理系统快照
swap unit : 文件扩展为.swap,用于标识swap设备
automount unit : 文件扩展为.automount,文件系统自动挂载设备
path unit : 文件扩展为.path,用于定义文件系统中的一个文件或目录
timer unit : 文件扩展为.timer,定时器配置单元,用来定时触发用户定义的操作,这类配置单元取代了atd、crond等传统的定时服务
1.3.2 初始化过程
2. MBR从Grub或LILO引导程序读取相关信息并初始化内核。接下来将由Grub或LILO继续引导系统。如果你在grub配置文件里指定了systemd作为引导管理程序,之后的引导过程将由systemd完成。Systemd使用“target”来处理引导和服务管理过程。这些systemd里的“target”文件被用于分组不同的引导单元以及启动同步进程。
root@compute:~# grep -R systemd /etc/grub.d/* /etc/grub.d/10_linux:SUPPORTED_INITS="sysvinit:/lib/sysvinit/init systemd:/lib/systemd/systemd upstart:/sbin/upstart"/etc/grub.d/20_linux_xen:SUPPORTED_INITS="sysvinit:/lib/sysvinit/init systemd:/lib/systemd/systemd upstart:/sbin/upstart"3. systemd执行的第一个目标是graphical.target。
root@compute:~# systemctl get-defaultgraphical.target
查看运行级别:
root@compute:~# runlevelN 5文件graphical.target的实际位置是/lib/systemd/system/graphical.target。
root@compute:~# find / -name "graphical.target"/lib/systemd/system/graphical.target
root@compute:~# cat /lib/systemd/system/graphical.target# This file is part of systemd.## systemd is free software; you can redistribute it and/or modify it# under the terms of the GNU Lesser General Public License as published by# the Free Software Foundation; either version 2.1 of the License, or# (at your option) any later version.[Unit]Description=Graphical InterfaceDocumentation=man:systemd.special(7)Requires=multi-user.targetWants=display-manager.serviceConflicts=rescue.service rescue.targetAfter=multi-user.target rescue.service rescue.target display-manager.serviceAllowIsolate=yes
root@compute:~# cat /lib/systemd/system/multi-user.target[Unit]Description=Multi-User SystemDocumentation=man:systemd.special(7)Requires=basic.targetConflicts=rescue.service rescue.targetAfter=basic.target rescue.service rescue.targetAllowIsolate=yes"multi-user.target"会将控制权交给另一层“basic.target”。
root@compute:~# ls /etc/systemd/system/multi-user.target.wants/atd.service cron.service lxcfs.service neutron-linuxbridge-agent.service openstack.service rsyslog.service snapd.servicecgmanager.service libvirt-bin.service lxd-containers.service neutron-linuxbridge-cleanup.service open-vm-tools.service snapd.autoimport.service ssh.servicecgproxy.service libvirt-guests.service networking.service nova-compute.service remote-fs.target snapd.refresh.timer ufw.service5. "basic.target"单元用于启动普通服务特别是图形管理服务。它通过/etc/systemd/system/basic.target.wants目录来决定哪些服务会被启动,basic.target之后将控制权交给sysinit.target.
root@compute:~# cat /lib/systemd/system/basic.target [Unit]Description=Basic SystemDocumentation=man:systemd.special(7)Requires=sysinit.targetWants=sockets.target timers.target paths.target slices.targetAfter=sysinit.target sockets.target paths.target slices.target tmp.mountWants=tmp.mount6. "sysinit.target"会启动重要的系统服务例如系统挂载,内存交换空间和设备,内核补充选项等等。
root@compute:~# cat /lib/systemd/system/sysinit.target[Unit]Description=System InitializationDocumentation=man:systemd.special(7)Conflicts=emergency.service emergency.targetWants=local-fs.target swap.targetAfter=local-fs.target swap.target emergency.service emergency.target
root@compute:~# cat /lib/systemd/system/local-fs.target[Unit]Description=Local File SystemsDocumentation=man:systemd.special(7)DefaultDependencies=noConflicts=shutdown.targetAfter=local-fs-pre.targetOnFailure=emergency.targetOnFailureJobMode=replace-irreversibly
参考资料:
1 http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html
2 http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html
3 最简明扼要的 Systemd 教程,只需十分钟: https://linux.cn/article-6888-1.html
4 走进Linux之systemd启动过程:https://linux.cn/article-5457-1.html
1.4 systemd & sysvinit
主要差别如下:
(2)启动脚本的位置,以前是/etc/init.d目录,符号链接到不同的 RunLevel 目录 (比如/etc/rc3.d、/etc/rc5.d等),现在则存放在/lib/systemd/system和/etc/systemd/system目录。
(3)配置文件的位置,以前init进程的配置文件是/etc/inittab,各种服务的配置文件存放在/etc/sysconfig目录。现在的配置文件主要存放在/lib/systemd目录,在/etc/systemd目录里面的修改可以覆盖原始设置。
2 命令启动Openstck服务
2.1 servcie命令
使用如下形式手动重启、关闭或启动:
service SCRIPT COMMAND [OPTIONS]The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT, or the name of an upstart job in /etc/init. The existence of an upstart job of the same name as a script in /etc/init.d will cause the upstart job to take precedence over the init.d script. The supported values of COMMAND depend on the invoked script. service passes COMMAND and OPTIONS to the init script unmodified. For upstart jobs, start, stop, status, are passed through to their upstart equivalents. Restart will call the upstart 'stop' for the job, followed immediately by the 'start', and will exit with the return code of the start command. All scripts should support at least the start and stop commands.如:
# service nova-api restart
/etc/init The directory containing upstart jobs./etc/init.d The directory containing System V init scripts./etc/rc?.d/ The directories containing the links used by init and managed by update-rc.d./etc/init.d/skeleton Model for use by writers of init.d scripts./var/lib/sysv-rc/legacy-bootsequence Flag indicating the machine is using legacy mode for boot script ordering.
2.2 systemctl
stop NAME... Stop (deactivate) one or more units
reload NAME... Reload one or more units
restart NAME... Start or restart one or more units
root@compute:~# ps -ef | grep novanova 3489 1 0 May19 ? 00:03:01 /usr/bin/python /usr/bin/nova-compute --config-file=/etc/nova/nova.conf --config-file=/etc/nova/nova-compute.conf --log-file=/var/log/nova/nova-compute.logroot@compute:~# systemctl stop nova-computeroot@compute:~# ps -ef | grep novaroot 9624 1821 0 09:19 pts/0 00:00:00 grep --color=auto novaroot@compute:~# systemctl start nova-computeroot@compute:~# ps -ef | grep novanova 9644 1 99 09:19 ? 00:00:02 /usr/bin/python /usr/bin/nova-compute --config-file=/etc/nova/nova.conf --config-file=/etc/nova/nova-compute.conf --log-file=/var/log/nova/nova-compute.log
3 开机自启动
3.1 sysvinit
root@controller:~# ls /etc/init.d/nova-api /etc/init.d/nova-apiroot@controller:~# cat /etc/init.d/nova-api #!/bin/sh### BEGIN INIT INFO# Provides: nova-api# Required-Start: $network $local_fs $remote_fs $syslog# Required-Stop: $remote_fs# Should-Start: postgresql mysql keystone rabbitmq-server ntp# Should-Stop: postgresql mysql keystone rabbitmq-server ntp# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Short-Description: Nova API server# Description: Frontend Nova API server### END INIT INFO# Author: Julien Danjou <acid@debian.org># PATH should only include /usr/* if it runs after the mountnfs.sh scriptPATH=/sbin:/usr/sbin:/bin:/usr/binDESC="OpenStack Compute API"PROJECT_NAME=novaNAME=${PROJECT_NAME}-api#!/bin/sh# The content after this line comes from openstack-pkg-tools# and has been automatically added to a .init.in script, which# contains only the descriptive part for the daemon. Everything# else is standardized as a single unique script.# Author: Thomas Goirand <zigo@debian.org># PATH should only include /usr/* if it runs after the mountnfs.sh scriptPATH=/sbin:/usr/sbin:/bin:/usr/binif [ -z "${DAEMON}" ] ; thenDAEMON=/usr/bin/${NAME}fiPIDFILE=/var/run/${PROJECT_NAME}/${NAME}.pidif [ -z "${SCRIPTNAME}" ] ; thenSCRIPTNAME=/etc/init.d/${NAME}fiif [ -z "${SYSTEM_USER}" ] ; thenSYSTEM_USER=${PROJECT_NAME}fiif [ -z "${SYSTEM_USER}" ] ; thenSYSTEM_GROUP=${PROJECT_NAME}fiif [ "${SYSTEM_USER}" != "root" ] ; thenSTARTDAEMON_CHUID="--chuid ${SYSTEM_USER}:${SYSTEM_GROUP}"fiif [ -z "${CONFIG_FILE}" ] ; thenCONFIG_FILE=/etc/${PROJECT_NAME}/${PROJECT_NAME}.conffiLOGFILE=/var/log/${PROJECT_NAME}/${NAME}.logif [ -z "${NO_OPENSTACK_CONFIG_FILE_DAEMON_ARG}" ] ; thenDAEMON_ARGS="${DAEMON_ARGS} --config-file=${CONFIG_FILE}"fi# Exit if the package is not installed[ -x $DAEMON ] || exit 0# If ran as root, create /var/lock/X, /var/run/X, /var/lib/X and /var/log/X as neededif [ `whoami` = "root" ] ; thenfor i in lock run log lib ; domkdir -p /var/$i/${PROJECT_NAME}chown ${SYSTEM_USER} /var/$i/${PROJECT_NAME}donefi# This defines init_is_upstart which we use later on (+ more...). /lib/lsb/init-functions# Manage log options: logfile and/or syslog, depending on user's choosing[ -r /etc/default/openstack ] && . /etc/default/openstack[ -r /etc/default/$NAME ] && . /etc/default/$NAME[ "x$USE_SYSLOG" = "xyes" ] && DAEMON_ARGS="$DAEMON_ARGS --use-syslog"[ "x$USE_LOGFILE" != "xno" ] && DAEMON_ARGS="$DAEMON_ARGS --log-file=$LOGFILE"do_start() {start-stop-daemon --start --quiet --background ${STARTDAEMON_CHUID} --make-pidfile --pidfile ${PIDFILE} --chdir /var/lib/${PROJECT_NAME} --startas $DAEMON \--test > /dev/null || return 1start-stop-daemon --start --quiet --background ${STARTDAEMON_CHUID} --make-pidfile --pidfile ${PIDFILE} --chdir /var/lib/${PROJECT_NAME} --startas $DAEMON \-- $DAEMON_ARGS || return 2}do_stop() {start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILERETVAL=$?rm -f $PIDFILEreturn "$RETVAL"}do_systemd_start() {exec $DAEMON $DAEMON_ARGS}case "$1" instart)init_is_upstart > /dev/null 2>&1 && exit 1log_daemon_msg "Starting $DESC" "$NAME"do_startcase $? in0|1) log_end_msg 0 ;;2) log_end_msg 1 ;;esac;;stop)init_is_upstart > /dev/null 2>&1 && exit 0log_daemon_msg "Stopping $DESC" "$NAME"do_stopcase $? in0|1) log_end_msg 0 ;;2) log_end_msg 1 ;;esac;;status)status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?;;systemd-start)do_systemd_start;; restart|force-reload)init_is_upstart > /dev/null 2>&1 && exit 1log_daemon_msg "Restarting $DESC" "$NAME"do_stopcase $? in0|1)do_startcase $? in0) log_end_msg 0 ;;1) log_end_msg 1 ;; # Old process is still running*) log_end_msg 1 ;; # Failed to startesac;;*) log_end_msg 1 ;; # Failed to stopesac;;*)echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload|systemd-start}" >&2exit 3;;esacexit 0
3.2 upstart
将自己的程序加入到系统的服务启动机制中,可以让其开机自启动。只需要在/etc/init下增加xxx.conf配置文件即可,xxx是service的服务名字。
两个概念:
1. init: upstart主进程,是Linux系统中的“应用程序管理器”,是其他所有进程的源头(PID为1),它会读取配置文件,处理各种服务和应用程序的依赖关系,根据事件(信号)来启动这些功能与服务,并动态地进行管理。
2.initctl:upstart事件管理器,可以被应用程序进程用来通知init哪些事件(信号)发生。
root@controller:~# ls /etc/init/nova-api.conf /etc/init/nova-api.confroot@controller:~# root@controller:~# cat /etc/init/nova-api.conf description "OpenStack Compute API"author "Thomas Goirand <zigo@debian.org>"start on runlevel [2345]stop on runlevel [!2345]chdir /var/runrespawnrespawn limit 20 5limit nofile 65535 65535pre-start scriptfor i in lock run log lib ; domkdir -p /var/$i/novachown nova /var/$i/novadoneend scriptscript[ -x "/usr/bin/nova-api" ] || exit 0DAEMON_ARGS=""[ -r /etc/default/openstack ] && . /etc/default/openstack[ -r /etc/default/$UPSTART_JOB ] && . /etc/default/$UPSTART_JOB[ "x$USE_SYSLOG" = "xyes" ] && DAEMON_ARGS="$DAEMON_ARGS --use-syslog"[ "x$USE_LOGFILE" != "xno" ] && DAEMON_ARGS="$DAEMON_ARGS --log-file=/var/log/nova/nova-api.log"exec start-stop-daemon --start --chdir /var/lib/nova \--chuid nova:nova --make-pidfile --pidfile /var/run/nova/nova-api.pid \--exec /usr/bin/nova-api -- --config-file=/etc/nova/nova.conf ${DAEMON_ARGS}end script
3.3 systemd
root@compute:~# ls /etc/systemd/system/multi-user.target.wants/neutron-linuxbridge-agent.service openstack.service rsyslog.service snapd.serviceneutron-linuxbridge-cleanup.service open-vm-tools.service snapd.autoimport.service ssh.servicenova-compute.service remote-fs.target snapd.refresh.timer ufw.service
root@compute:~# cat /etc/systemd/system/multi-user.target.wants/nova-compute.service [Unit]Description=OpenStack ComputeAfter=libvirt-bin.service postgresql.service mysql.service keystone.service rabbitmq-server.service ntp.service neutron-ovs-cleanup.service [Service]User=novaGroup=novaType=simpleWorkingDirectory=/var/lib/novaPermissionsStartOnly=trueExecStartPre=/bin/mkdir -p /var/lock/nova /var/log/nova /var/lib/novaExecStartPre=/bin/chown nova:nova /var/lock/nova /var/lib/novaExecStartPre=/bin/chown nova:adm /var/log/novaExecStart=/etc/init.d/nova-compute systemd-startRestart=on-failureLimitNOFILE=65535TimeoutStopSec=15[Install]WantedBy=multi-user.targetnova-compute.service被调用后,会执行etc/init.d/nova-compute脚本
root@compute:~# ls /etc/init/nova-compute.conf /etc/init/nova-compute.confroot@compute:~# ls /etc/init.d/nova-compute /etc/init.d/nova-compute因为没有使用upstart,所以/etc/init/nova-compute.conf配置是不起作用的。
4 取消开机自启动
4.1 sysvinit
4.2 upstart
4.3 systemd
5 nova-api调用执行
exec start-stop-daemon --start --chdir /var/lib/nova \--chuid nova:nova --make-pidfile --pidfile /var/run/nova/nova-api.pid \--exec /usr/bin/nova-api -- --config-file=/etc/nova/nova.conf ${DAEMON_ARGS}其实就是用start-stop-daemon去调用/usr/bin/nova-api
(2) 查看/usr/bin/nova-api
root@controller:~# cat /usr/bin/nova-api #!/usr/bin/python# PBR Generated from u'console_scripts'import sysfrom nova.cmd.api import mainif __name__ == "__main__": sys.exit(main())其中调用了nova/cmd目录下的api.py文件中的main函数
(3) 查看api.py文件
root@controller:~# pythonPython 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import nova>>> print nova.__path__['/usr/lib/python2.7/dist-packages/nova']到目录/usr/lib/python2.7/dist-packages/nova/cmd下:
root@controller:/usr/lib/python2.7/dist-packages/nova/cmd# ll api*py-rw-r--r-- 1 root root 1514 Nov 22 11:11 api_ec2.py-rw-r--r-- 1 root root 1781 Nov 22 11:11 api_metadata.py-rw-r--r-- 1 root root 1481 Nov 22 11:11 api_os_compute.py-rw-r--r-- 1 root root 1846 Nov 22 11:11 api.py
- OpenStack服务开机自启动
- 开机自启动服务
- 服务开机自启动
- 开机服务自启动配置
- 设定开机自启动服务
- 开机自启动服务Service
- Linux开机自启动服务
- tomcat 服务开机自启动
- Linux服务开机自启动
- linux 开机服务自启动
- 设置服务开机自启动
- 开机广播RECEIVE_BOOT_COMPLETED、开机自启动服务
- Linux开机启动详解--服务自启动
- redhat中vsftpd服务开机自启动
- android实现开机自启动服务
- android实现开机自启动服务
- ubuntu 10.10开机自启动svn服务
- android实现开机自启动服务
- 表单
- ORACLE 建库过程总结
- UE4 导出全景图和全景视频
- iOS 允许HTTP请求
- 1.jQuery 入门
- OpenStack服务开机自启动
- PCM转WAV格式 Wav数据格式的44个字节头部信息汇总
- Oracle select case when
- [leetcode]: 543. Diameter of Binary Tree
- c++ 操作系统实验 进程调度
- nginx在windows下配置反向代理
- lua——赢三张牌型处理相关算法(下)——牌型比较
- POI导出工具类
- [蓝桥杯] 历届试题 剪格子(dfs+回溯)