吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 650|回复: 5
收起左侧

[Python 原创] 基于python2写的linux下磁盘挂载脚本

  [复制链接]
chenhaian228 发表于 2025-4-15 13:10
本帖最后由 chenhaian228 于 2025-4-15 13:14 编辑

近段时间折腾各个安全厂家的虚拟设备,因权限问题看不到底层文件,所以退而求其次在其他有root权限的虚机下挂载其硬盘,但每次手动挂载很繁琐,就写了个脚本,用于一键挂载/卸载 所有的磁盘。
1、脚本命令为:python disk_mounter.py -h、python disk_mounter.py -m、python disk_mounter.py -u     分别对应帮助、挂载、卸载;
2、所有磁盘挂载在 /mnt/ 下,对应磁盘名称建立相关文件夹,比如磁盘名称为  sdb,则在/mnt/ 下建立sdb目录,对应分区则在/mnt/sdb  目录下再一一对应;
3、卸载磁盘时会清空创建的目录;
4、对于加密磁盘或者其他无法正常挂载的磁盘消息直接略过,看最后的挂载结果就行了。

[Asm] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
 
import os
import sys
import argparse
import subprocess
 
MOUNT_BASE = "/mnt"
 
def get_mounted_disks():
    """获取已挂载的磁盘列表"""
    mounted = set()
    try:
        with open('/proc/mounts', 'r') as f:
            for line in f:
                parts = line.split()
                if len(parts) > 1:
                    mounted.add(parts[0])
    except Exception:
        pass
    return mounted
 
def get_unmounted_disks():
    """获取未挂载的磁盘分区"""
    mounted = get_mounted_disks()
    unmounted = []
    try:
        lsblk = subprocess.Popen(['lsblk', '-l', '-o', 'NAME,TYPE,MOUNTPOINT'], stdout=subprocess.PIPE)
        output, _ = lsblk.communicate()
         
        for line in output.splitlines()[1:]:
            parts = line.split()
            if len(parts) < 2:
                continue
                 
            name, type_, mountpoint = parts[0], parts[1], parts[2] if len(parts) > 2 else None
            if (type_ in ['part', 'disk']) and not mountpoint and not name.startswith(('cl-', 'vg0-')):
                device_path = '/dev/%s' % name
                if device_path not in mounted:
                    unmounted.append(name)
    except Exception:
        pass
    return unmounted
 
def get_lvm_volumes():
    """获取所有可用的LVM逻辑卷"""
    lvm_volumes = {}
    try:
        # 获取物理卷对应的卷组
        pvs = subprocess.Popen(['pvs', '--noheadings', '-o', 'pv_name,vg_name'], stdout=subprocess.PIPE)
        output, _ = pvs.communicate()
        for line in output.splitlines():
            parts = line.strip().split()
            if len(parts) >= 2:
                pv_name = os.path.basename(parts[0])
                lvm_volumes[pv_name] = parts[1]  # 记录物理卷对应的卷组
    except Exception:
        pass
    return lvm_volumes
 
def mount_disks():
    """挂载所有未挂载的磁盘"""
    unmounted = get_unmounted_disks()
    lvm_volumes = get_lvm_volumes()
    results = []
     
    for disk in unmounted:
        device = '/dev/%s' % disk
        is_lvm = False
        lv_path = None
         
        # 检查是否是LVM物理卷
        if disk in lvm_volumes:
            is_lvm = True
            vg_name = lvm_volumes[disk]
            # 获取该卷组下的逻辑卷
            try:
                lvs = subprocess.Popen(['lvs', '--noheadings', '-o', 'lv_path', vg_name], stdout=subprocess.PIPE)
                output, _ = lvs.communicate()
                if output.strip():
                    lv_path = output.strip().split()[0]  # 取第一个逻辑卷
            except Exception:
                pass
         
        # 创建挂载点
        parent_disk = disk[:3]  # 如sdb1 -> sdb
        mount_point = os.path.join(MOUNT_BASE, parent_disk, disk)
         
        try:
            os.makedirs(mount_point)
        except OSError:
            pass
         
        # 执行挂载
        try:
            if is_lvm and lv_path:
                mount_cmd = ['mount', lv_path, mount_point]
            else:
                mount_cmd = ['mount', device, mount_point]
             
            subprocess.check_call(mount_cmd, stderr=subprocess.PIPE)
            results.append((lv_path if is_lvm else device, mount_point, True))
        except subprocess.CalledProcessError:
            # 挂载失败则删除目录
            try:
                if os.path.exists(mount_point) and not os.listdir(mount_point):
                    os.rmdir(mount_point)
            except OSError:
                pass
            results.append((lv_path if is_lvm else device, mount_point, False))
     
    # 输出结果汇总
    print("\n挂载结果汇总:")
    print("-" * 60)
    for device, mount_point, success in results:
        if success:
            print("%-20s --->   %-40s 挂载成功" % (device, mount_point))
    print("-" * 60)
 
def unmount_disks():
    """卸载所有由脚本挂载的磁盘并清理目录"""
    mnt_mounts = []
     
    try:
        with open('/proc/mounts', 'r') as f:
            for line in f:
                parts = line.split()
                if len(parts) > 1 and parts[1].startswith(MOUNT_BASE):
                    mnt_mounts.append(parts[1])
    except Exception:
        pass
     
    if not mnt_mounts:
        print("没有需要卸载的挂载点")
        return
     
    for mount_point in mnt_mounts:
        try:
            subprocess.check_call(['umount', mount_point], stderr=subprocess.PIPE)
        except subprocess.CalledProcessError:
            pass
     
    # 清理空目录
    for root, dirs, files in os.walk(MOUNT_BASE, topdown=False):
        for name in dirs:
            dir_path = os.path.join(root, name)
            try:
                if not os.listdir(dir_path):
                    os.rmdir(dir_path)
            except OSError:
                pass
     
    print("卸载操作完成")
 
def main():
    parser = argparse.ArgumentParser(description='磁盘挂载/卸载工具')
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('-m', '--mount', action='store_true', help='挂载所有未挂载的磁盘')
    group.add_argument('-u', '--unmount', action='store_true', help='卸载所有磁盘并清理目录')
     
    args = parser.parse_args()
     
    if args.mount:
        mount_disks()
    elif args.unmount:
        unmount_disks()
 
if __name__ == '__main__':
    main()




挂载磁盘

挂载磁盘

卸载磁盘

卸载磁盘

disk_mounter.rar

1.82 KB, 下载次数: 13, 下载积分: 吾爱币 -1 CB

磁盘挂载py脚本

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

bdcpc 发表于 2025-4-15 14:31
这个好,以后宝塔就可以用这个来挂载磁盘了
cowboy56 发表于 2025-4-15 14:44
zhangyu0320 发表于 2025-4-15 15:25
63557477 发表于 2025-4-16 14:35
太有用了,谢谢分享
kaocccaa 发表于 2025-4-17 09:28
牛,收藏了,用得上
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-8-12 09:26

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表