改动虚拟机镜像的rootpassword

      有时从网上下载的虚拟机镜像。没有rootpassword,必须通过秘钥登录。然后秘钥又须要麻烦的注入到里面去。想用,却无法登录。非常头痛。本文提供一种通过改动虚拟机镜像里面的/etc/shadow文件,来设置镜像的rootpassword,当然也能够改动其他用户的password。

      本文使用python-guestfs类库来操作虚拟机镜像,所以请安装python-guestfs及相关包。ubuntu系统运行:

sudo apt-get install python-guestfs 
      centos系统请运行:

sudo yum install python-libguestfs 

      给指定password生成加密过后的password:

def encrypt_passwd(admin_passwd):                                             
    algos = {'SHA-512': '$6$', 'SHA-256': '$5$', 'MD5': '$1$', 'DES': ''}          
    salt_set = ('abcdefghijklmnopqrstuvwxyz'                                    
                'ABCDEFGHIJKLMNOPQRSTUVWXYZ'                                    
                '0123456789./')                                                 
    salt = 16 * ' '                                                             
    salt = ''.join([random.choice(salt_set) for c in salt]) 
                                                                                                                   
    encrypted_passwd = crypt.crypt(admin_passwd, algos['MD5'] + salt)           
    if len(encrypted_passwd) == 13:                                             
        encrypted_passwd = crypt.crypt(admin_passwd, algos['DES'] + salt)          
                                                                                
    return encrypted_passwd
     使用guestfs将生成后的加密password放到虚拟机镜像里面的/etc/shadow文件里:

image_file = "ubuntu.qcow2"
image_format = "qcow2"
password = "123456"
shadow_path = "/etc/shadow"

#调用encrypt_passwd()使用md5算法对password进行加密,并返回加密后的password
encrypted_passwd = encrypt_passwd(password)
#初始化一个GuestFS实例,用于后面对虚拟机镜像的操作
handle = guestfs.GuestFS(python_return_dict=False, close_on_exit=False)
#将虚拟机镜像文件及格式加入到handle中,用于后面操作         
handle.add_drive_opts(image_file, format=image_format)                         
handle.launch()                                   
#遍历虚拟机镜像中的系统分区,并返回。一般为[/dev/sda1]或[/dev/sda],使用该方法的一个优点是,你能够不指定甚至不用指定系统分区。guestfs就把活给干了
roots = handle.inspect_os()                                                     
#遍历系统分区中的挂载点。返回值一般为[['/','/etc/sda1']]
mounts = handle.inspect_get_mountpoints(root)                                   
mounts.sort(key=lambda mount: mount[0])                                         
                                                                                
for mount in mounts:                              
    #挂载系统分区,将‘/etc/sda1’挂载到‘/’,然后就能够通过handle訪问sda1上的文件了                              
    handle.mount_options("", mount[1], mount[0])

#读取‘/etc/shadow'文件内容
shadow_data = handle.read_file(shadow_path)                           
           
s_file = shadow_data.split("
")                                            
                                                                                
new_s_file = []                                                             
for entry in s_file:                                                        
split_entry = entry.split(":")                                          
if split_entry[0] == "root":
    #将root相应的行的password设为加密password                                            
    split_entry[1] = encrypted_passwd(password)                         
                                                                             
    new_s_file.append(':'.join(split_entry))                                
                                                                                
new_shadow_data = '
'.join(new_s_file)
#写入改动过后的‘/etc/shadow’文件                                                                     
handle.write(path, new_shadow_data)
handle.shutdown()
handle.close()

      写了两个脚本,可用于改动虚拟机镜像文件的password。使用较为方便,如有须要,请查看https://github.com/xuriwuyun/change-root-passwd。

运行:

sudo bash changerootpasswd.sh ubuntu.qcow2 123456
     就可以将ubuntu.qcow2镜像的root登录password设定为123456.该脚本可支持多种镜像格式。经过验证的有rawqcow2。



參考文献

1 http://libguestfs.org/guestfs-python.3.html

2 http://libguestfs.org/guestfs.3.html

原文地址:https://www.cnblogs.com/yjbjingcha/p/6915978.html