Set script annotation in stateless linux

Source: Internet
Author: User

Stateless Linux is a fedora
A core project, as its name implies, does not store persistent state information during system operation. Simply put, it does not allow the system to write persistent storage devices (such as hard disks. Generally used
Readonly root technology. In my recent program, stateless Linux is used to share the same disk image with multiple virtual machines.

The stateless Linux setting file is/etc/sysconfig/readonly-root,/etc/rc. sysinit reads this file at system startup, and configures it according to the settings in the file.

The readonly-root setting code extracted from/etc/rc. sysinit adds comments to the Code:
Readonly =
If [-F/etc/sysconfig/readonly-root]; then
./Etc/sysconfig/readonly-Root
Fi

#/Etc/sysconfig/readonly-root set the variables readonly, temporary_state, rw_mount, rw_label, state_label, and state_mount.

# The functions of these variables are described in the readonly-root file in detail. It can also be reflected in the following code.

If strstr "$ response line" readonlyroot; then
Readonly = Yes
[-Z "$ rw_mount"] & rw_mount =/var/lib/stateless/writable
Fi

# The cmdline variable is the parameter passed to the kernel at startup. You can see it in/proc/cmdline, for example: RO root = label =/

If strstr "$ cmdline" noreadonlyroot; then
Readonly = No
Fi

# Set readonly and temporary_state to yes.

If ["$ readonly" = "yes"-o "$ temporary_state" = "yes"]; then

# The mount_empty function copies the file or directory specified by the parameter $1 to the $ rw_mount directory for backup, and then mounts the backup file to the original file in BIND mode.

# In this way, the original file is still visible and readable, and read-only is ensured, because all write operations will apply to the backup file. The-p Parameter of cpio specifies that the target folder is $ rw_mount, and the-D Parameter

# Indicates that if $1 is a file in a multi-level directory, the directory structure will be automatically created in the original format under $ rw_mount. If $1 is a directory, the files in the directory will not be copied.

# Indicates empty in mount_empty.


Mount_empty (){
If [-e "$1"]; then
Echo "$1" | cpio-p-VD "$ rw_mount" &>/dev/null
Mount-n -- bind "$ RW_MOUNT $1" "$1"
Fi
}

# If $1 is a directory, mount_dirs creates the directory structure under $1 as is under $ RW_MOUNT.

Mount_dirs (){
If [-e "$1"]; then
Mkdir-p "$ RW_MOUNT $1"
# Fixme: find is bad
Find "$1"-type d-print0 | cpio-p-0vd "$ RW_MOUNT" &>/dev/null
Mount-n -- bind "$ RW_MOUNT $1" "$1"
Fi
}

Mount_files (){
If [-e "$1"]; then
CP-A -- parents "$1" "$ rw_mount"
Mount-n -- bind "$ rw_mount $1" "$1"
Fi
}

# In short, Mount _ {empty, dirs, files} is for $1. mount_empty only costs $1, regardless of any node under $1, mount_dirs only maintains the directory structure under $1,

# Mount_files: maintain all directories and files under $1.

# Common mount options for Scratch space regardless
# Type of backing store
Mountopts =

# The blkid command is used to index or view the properties of Block devices. The-t parameter is used to specify an attribute (the attribute is represented by name = value, for example, label =/), and the-l parameter indicates

# Search for the block device whose property value matches the value specified by the-t parameter. -O specifies the output format. Optional values: full, value, and device. The following is an output example.

# [Root @ jcwkyl etc] # blkid-T label =/-l-O full

#/Dev/sda3: LABEL = "/" UUID = "8844f9c3-2836-4f29-aaab-3fb6b6d0a1bf" SEC_TYPE = "ext2" TYPE = "ext3"

# [Root @ jcwkyl etc] # blkid-t LABEL =/-l-o value

#/

# 8844f9c3-2836-4f29-aaab-3fb6b6d0a1bf

# Ext2

# Ext3

# [Root @ jcwkyl etc] # blkid-t LABEL =/-l-o device

#/Dev/sda3

# Scan partitions for local scratch storage
Rw_mount_dev = $ (blkid-t LABEL = "$ RW_LABEL"-l-o device)

# The comments in this Code are very clear. First, try to mount them from the settings in/etc/fstab. If/etc/fstab is not set,

# Mount the partition found in the previous step to $ RW_MOUNT. If not, mount the $ RW_MOUNT directory to tmpfs.

# First try to mount scratch storage from/etc/fstab, then any
# Partition with the proper label. If either succeeds, be sure
# To wipe the scratch storage clean. If both fail, then mount
# Scratch storage via tmpfs.
If mount $ mountopts "$ RW_MOUNT">/dev/null 2> & 1; then
Rm-rf "$ RW_MOUNT">/dev/null 2> & 1

Elif [x $ rw_mount_dev! = X] & mount $ rw_mount_dev $ mountopts
"$ RW_MOUNT">/dev/null 2> & 1; then
Rm-rf "$ RW_MOUNT">/dev/null 2> & 1
Else
Mount-n-t tmpfs $ mountopts none "$ RW_MOUNT"
Fi

For file in/etc/rwtab. d/*; do
Is_ignored_file "$ file" & continue
[-F $ file] & cat $ file | while read type path; do
Case "$ type" in
Empty)
Mount_empty $ path
;;
Files)
Mount_files $ path
;;
Dirs)
Mount_dirs $ path
;;
*)
;;
Esac
[-N "$ SELINUX_STATE"-a-e "$ path"] & restorecon-R "$ path"
Done
Done

# In theory there shoshould be no more than one network interface active
# This early in the boot process -- the one we're re booting from.
# Use the network address to set the hostname of the client. This
# Must be done even if we have local storage.
Ipaddr =
If ["$ HOSTNAME" = "localhost"-o "$ HOSTNAME" = "localhost. localdomain"]; then

Ipaddr = $ (ip addr show to 0/0 scope global | awk
'/[[: Space:] inet/{print gensub ("/. *", "", "g", $2 )}')
If [-n "$ ipaddr"]; then
Eval $ (ipcalc-h $ ipaddr 2>/dev/null)
Hostname $ {hostname}
Fi
Fi

# Clients with read-only root filesystems may be provided with
# Place where they can place minimal amounts of persistent
# State. SSH keys or puppet certificates for example.
#
# Ideally we'll use puppet to manage the State directory and
# Create the BIND mounts. However, until that's all ready this
# Is sufficient to build a working system.

# First try to mount persistent data from/etc/fstab, then any
# Partition with the proper label, then fallback to NFS
State_mount_dev = $ (blkid-t LABEL = "$ STATE_LABEL"-l-o device)
If mount $ mountopts $ STATE_OPTIONS "$ STATE_MOUNT">/dev/null 2> & 1; then
/Bin/true

Elif [x $ state_mount_dev! = X] & mount $ state_mount_dev
$ Mountopts "$ STATE_MOUNT">/dev/null 2> & 1; then
/Bin/true
Elif [! -Z "$ CLIENTSTATE"]; then
# No local storage was found. Make a final attempt to find
# State on an NFS server.

Mount-t nfs $ CLIENTSTATE/$ HOSTNAME $ STATE_MOUNT-o rw, nolock
Fi

If [-d $ STATE_MOUNT/etc]; then
# Copy the puppet CA's cert from the r/o image into
# State directory so that we can create a bind mount on
# The ssl directory for storing the client cert. I 'd really
# Rather have a unionfs to deal with this stuff
Cp -- parents-f-p/var/lib/puppet/ssl/certs/ca. pem $ STATE_MOUNT 2>/dev/null

# In the future this will be handled by puppet
For I in $ (grep-V "^ #" $ state_mount/files); do
If [-e $ I]; then
Mount-n-o bind $ state_mount/$ {I }$ {I}
Fi
Done
Fi
Fi

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.